我在尝试让 text-overflow: ellipsis
在没有固定宽度的跨度上工作时遇到了一些问题。跨度应根据内容扩展,但如果内容大于可用空间,则内容应显示省略号。这是一个链接 JSFiddle .
CSS 和 HTML:
.row {
width: 50%;
border: 1px solid black;
}
.wrapper {
text-align: left;
}
.label {
display: inline-block;
margin-top: 10px;
margin-right: 4px;
padding: 0 8px;
border-radius: 4px;
color: white;
background-color: grey;
height: 20px;
vertical-align: middle;
font-size: 14px;
}
.content {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="row">
<div class="wrapper">
<div class="label">
<span class="content">text</span>
</div>
<div class="label">
<span class="content">longer text label</span>
</div>
<div class="label">
<span class="content">Another label</span>
</div>
<div class="label">
<span class="content">A very very very very very very long text label that should fit inside row and cut the text and show ellipsis...</span>
</div>
</div>
</div>
最佳答案
检查这个插件:
https://jsfiddle.net/42khut93/3/
您需要为 label
和 content
类设置 max-width
并以百分比调整填充。
同时将 .content
display
设置为 block
或 inline-block
.row {
width: 50%;
border: 1px solid black;
}
.wrapper {
text-align: left;
}
.label {
display: inline-block;
margin-top: 10px;
margin-right: 4px;
padding: 0 1%; //in percentage
border-radius: 4px;
color: white;
background-color: grey;
height: 20px;
vertical-align: middle;
font-size: 14px;
max-width: 98%; //max-width 100% - 1% padding + 1% padding
}
.content {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 100%; // set max-width
}
<div class="row">
<div class="wrapper">
<div class="label">
<span class="content">text</span>
</div>
<div class="label">
<span class="content">longer text label</span>
</div>
<div class="label">
<span class="content">Another label</span>
</div>
<div class="label">
<span class="content">A very very very very very very long text label that should fit inside row and cut the text and show ellipsis...</span>
</div>
</div>
</div>
关于html - 文本溢出的动态宽度跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394360/