CSS Table with one column taking remaining space

前端 未结 6 953
北荒
北荒 2020-12-16 13:04

I have tried now several things (and looked around here) and nothing worked so far. So I am going to ask.

What I want:

I have the following

相关标签:
6条回答
  • 2020-12-16 13:19

    See this fiddle (or, alternatively), you need to set the max-width for each table cell:

    body{
        width:100%;
        padding:0;
        margin:0;
    }
    table {
        margin-top: 50px;
        width:100%;
        max-width:100%;
    }
    table td {
        white-space: nowrap;
    }
    table td:nth-child(1) {
        background-color:red;
        max-width:100px;
    }
    table td:nth-child(2) {
        background-color: green;
        overflow:hidden;
        max-width:100px;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    table td:nth-child(3) {
        background-color: orange;
        max-width:100px;
    }
    
    0 讨论(0)
  • 2020-12-16 13:23

    Here is an answer using divs instead of a table: DEMO

    HTML

    <div class="container">
        <div class="fnl first">First Baby</div>
        <div class="fnl last">Last Guy</div>
        <div class="adjust">I will adjust between both of you guys</div>
    </div>
    

    CSS

    .container{
        width: 300px;
    }
    .first{
        float:left;
        background: red;
    }
    .last{
        float:right;
        background: orange;
    }
    .adjust{
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
    }
    
    0 讨论(0)
  • 2020-12-16 13:23

    You can re-arrange the columns by moving the longest one at the end then use nested tables.

    CSS

    .extend
    {
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
        -o-text-overflow:ellipsis;
    }
    td
    {
        white-space:nowrap;
    }
    .box
    {
        width:1000px;
        border:blue solid thick;
        overflow:hidden;
    }
    

    HTML

    <div class="box">
    <table width="100%" border="1">
      <tr>
        <td class="small">First column with text</td>
         <td class="small">Small column</td>
         <td>
    <table>
         <tr>
        <td  class="extend" >This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long.</td>
        </tr>
        </table>
       </td>
      </tr>
    </table>
    </div>
    

    Except for the ellipsis it is working well. See result

    0 讨论(0)
  • 2020-12-16 13:31

    Something like this? http://jsfiddle.net/NhGsf/

    By using: display: table; width: 100%; table-layout: fixed; position: absolute; top: 0;

    And setting first and last child to fixed width the middle section will have the rest off the space

    0 讨论(0)
  • 2020-12-16 13:35

    min-width in combination with width:100% seems to work in firefox and chrome:

      tr {
        td:first-of-type {
          width: 100%;
        }
        td:last-of-type {
          min-width: 200px;
        }
      }
    
    0 讨论(0)
  • 2020-12-16 13:38

    I was facing the same challenge and I found the following solution using tables.

    The HTML needs to use a DIV in the long column. The CSS defines your small and extend classes. The hard part being the definition of the extend class.

    This gives you the behavior you describe.

    HTML

    <table>
      <tr>
        <td class="small">First column with text</td>
        <td class="extend">
          <div class="small">This column should fill the remaining space but should be truncated if the text is too long.</div>
        </td>
        <td class="small">Small column</td>
      </tr>
    </table>
    

    CSS

    table {
      margin-top: 50px;
    }
    
    table td {
      white-space: nowrap;
    }
    
    table td:nth-child(1) {
      background-color: red;
    }
    
    table td:nth-child(2) {
      background-color: green;
    }
    
    table td:nth-child(3) {
      background-color: orange;
    }
    
    .extend {
      display: table;
      table-layout: fixed;
      width: 100%
    }
    
    .small {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    0 讨论(0)
提交回复
热议问题