html – 带文本溢出的动态宽度跨度

我有一些问题试图得到文本溢出:省略号工作在我没有固定宽度的跨度.跨度应根据内容进行扩展,但如果内容大于可用空间,则内容应显示省略号.这是
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>

最佳答案 检查这个plunker:

https://jsfiddle.net/42khut93/3/

您需要为标签和内容类设置max-width
并以百分比调整填充.
同时将.content显示设置为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>
点赞