How to get the in HTML tables to fit content, and let a specific fill in the rest

后端 未结 4 1146
一向
一向 2020-12-23 13:23

This is a hypothetical example:

相关标签:
4条回答
  • 2020-12-23 13:49

    use overflow:

    overflow: visible;
    
    0 讨论(0)
  • 2020-12-23 13:56

    demo - http://jsfiddle.net/victor_007/ywevz8ra/

    added border for better view (testing)

    more info about white-space

    table{
        width:100%;
    }
    table td{
        white-space: nowrap;  /** added **/
    }
    table td:last-child{
        width:100%;
    }
    

        table {
          width: 100%;
        }
        table td {
          white-space: nowrap;
        }
        table td:last-child {
          width: 100%;
        }
    <table border="1">
      <thead>
        <tr>
          <th>Column A</th>
          <th>Column B</th>
          <th>Column C</th>
          <th class="absorbing-column">Column D</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Data A.1 lorem</td>
          <td>Data B.1 ip</td>
          <td>Data C.1 sum l</td>
          <td>Data D.1</td>
        </tr>
        <tr>
          <td>Data A.2 ipsum</td>
          <td>Data B.2 lorem</td>
          <td>Data C.2 some data</td>
          <td>Data D.2 a long line of text that is long</td>
        </tr>
        <tr>
          <td>Data A.3</td>
          <td>Data B.3</td>
          <td>Data C.3</td>
          <td>Data D.3</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-23 13:57

    Define width of .absorbing-column

    Set table-layout to auto and define an extreme width on .absorbing-column.

    Here I have set the width to 100% because it ensures that this column will take the maximum amount of space allowed, while the columns with no defined width will reduce to fit their content and no further.

    This is one of the quirky benefits of how tables behave. The table-layout: auto algorithm is mathematically forgiving.

    You may even choose to define a min-width on all td elements to prevent them from becoming too narrow and the table will behave nicely.

    table {
        table-layout: auto;
        border-collapse: collapse;
        width: 100%;
    }
    table td {
        border: 1px solid #ccc;
    }
    table .absorbing-column {
        width: 100%;
    }
    <table>
      <thead>
        <tr>
          <th>Column A</th>
          <th>Column B</th>
          <th>Column C</th>
          <th class="absorbing-column">Column D</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Data A.1 lorem</td>
          <td>Data B.1 ip</td>
          <td>Data C.1 sum l</td>
          <td>Data D.1</td>
        </tr>
        <tr>
          <td>Data A.2 ipsum</td>
          <td>Data B.2 lorem</td>
          <td>Data C.2 some data</td>
          <td>Data D.2 a long line of text that is long</td>
        </tr>
        <tr>
          <td>Data A.3</td>
          <td>Data B.3</td>
          <td>Data C.3</td>
          <td>Data D.3</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-23 13:59

    Setting CSS width to 1% or 100% of an element according to all specs I could find out is related to the parent. Although Blink Rendering Engine (Chrome) and Gecko (Firefox) at the moment of writing seems to handle that 1% or 100% (make a columns shrink or a column to fill available space) well, it is not guaranteed according to all CSS specifications I could find to render it properly.

    One option is to replace table with CSS4 flex divs:

    https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    That works in new browsers i.e. IE11+ see table at the bottom of the article.

    0 讨论(0)
提交回复
热议问题