text-overflow: ellipsis in table-cell without width

后端 未结 7 690
后悔当初
后悔当初 2020-12-04 13:18

I\'m trying to achieve a responsive table width text-overflow: ellipsis; in the middle cell that looks like this:

| Button 1 | A one-lined text th

相关标签:
7条回答
  • 2020-12-04 14:08

    Here you go

    <div class="container">
      <div>
        Button 1
      </div>
      <div class="fill">
        <div class="lockText">
          A one-lined text that is too long and has to be...
        </div>
      </div>
      <div>
        Button 2
      </div>
    </div>
    

    The trick is to set up a table layout with a cell that takes up leftover space first (css tables, mind you!).

    .container {
      display: table;
      width: 100%;
    }
    
    .container > div {
      display: table-cell;
      white-space: nowrap;
      position : relative;
      width : auto;
    
      /*make it easier to see what's going on*/
      padding : 2px;
      border : 1px solid gray;
    }
    
    .container > .fill {
      width : 100%;
    }
    

    Then add your text that constrains itself within.

    .lockText {
      position : absolute;
      left : 0;
      right : 0;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    0 讨论(0)
提交回复
热议问题