代码之家  ›  专栏  ›  技术社区  ›  andkorsh

不指定特定宽度的HTML省略号

  •  3
  • andkorsh  · 技术社区  · 7 年前

    • 如果SPAN和B都适合这个盒子,它们只是一个接一个地走。
    • 如果没有,SPAN有一个省略号,但B完全显示(它永远不会大于一个完整的块)。

    预期行为:

    enter image description here

    div {
      width: 200px;
      white-space: nowrap;
      border: 1px solid red;
      margin-top: 10px;
    }
    
    span {
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    b {
      padding-left: 10px;
    }
    <div>
      <span>test</span>
      <b>12345</b>
    </div>
    
    <div>
      <span>test test test test test test test test test test test</span>
      <b>50</b>
    </div>
    3 回复  |  直到 7 年前
        1
  •  5
  •   Michael Benjamin William Falcon    7 年前

    display: flex

    div {
      display: flex; /* new */
      width: 200px;
      white-space: nowrap;
      border: 1px solid red;
      margin-top: 10px;
    }
    
    span {
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    b {
      padding-left: 10px;
    }
    <div>
      <span>test</span>
      <b>12345</b>
    </div>
    
    <div>
      <span>test test test test test test test test test test test</span>
      <b>50</b>
    </div>

    flex默认设置的组合,包括 flex-shrink: 1 ,使省略号能够在 flex formatting context .

        2
  •  3
  •   Ramin Ahmadi    7 年前

    div {
      width: 200px;
      white-space: nowrap;
      border: 1px solid red;
      margin-top: 10px;
    }
    
    span {
      overflow: hidden;
      text-overflow: ellipsis;
      display:inline-block;
      max-width:150px
    }
    
    b {
      padding-left: 10px;
    }
    <div>
      <span>test</span>
      <b>12345</b>
    </div>
    
    <div>
      <span>test test test test test test test test test test test</span>
      <b>1234</b>
    </div>
        3
  •  0
  •   andkorsh    7 年前

    实际上,我希望解决方案在由wkhtmltopdf库生成的PDF中工作。这个库基于QT WebKit,它还不支持flex,但它支持flex的旧语法(称为flex-box)。

    以下是它在wkhtmltopdf中的工作原理(不过也适用于Chrome):

    .flexrow {
      width: 300px;
      margin-top: 30px;
      border: 1px solid green;
    
      display: -webkit-box;
    }
    
    .flexrow .fst {
      background-color: yellow;
    
      -webkit-box-flex: 0.1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 5px; /* for ellipsis */
    }
    
    .flexrow .snd {
      background-color: aqua;
    
      white-space: nowrap;
    }
    
    .flexrow .trd {
      background-color: pink;
    
      -webkit-box-flex: 2147483647; /* max int */
    }
    <div class="flexrow">
      <div class='fst'>test</div>
      <div class='snd'>12345</div>
      <div class='trd'></div>
    </div>
    
    <div class="flexrow">
      <div class='fst'>test test test test test test test test test test test test test test</div>
      <div class='snd'>50</div>
      <div class='trd'></div>
    </div>