skip to Main Content

I want all of the columns in my table to have a width that matches their content, no more, no less. I want that width the stay the same as the screen resizes. I want one column in my table to grow to fill the remaining space. Its width should change as the window re-sizes.

My problem is that with multiple rows and a table header, I don’t know how to achieve this. I can make it work by hard coding the column widths for the columns that I want to remain static, but I don’t want that — I want their widths to be determined by the content.

This is working (based on another stack overflow response), but does not adapt to the content in columns 1 and 2

table {
  display: table;
  width: 100%;
  table-layout: fixed;
  position: absolute;
  top: 0;
}

.small {
  background: green;
  width: 80px;
}

.extend {
  background: red;
}
<table>
  <thead>
    <th class="small">Column 1</th>
    <th class="extend">Column 2</th>
    <th class="small">Column 3</th>
  </thead>
  <tbody>
    <tr>
      <td class="small">First column</td>
      <td class="extend">This column should fill the remaining space </td>
      <td class="small">Small column 2</td>
    </tr>
  </tbody>
</table>

Here is my fiddle.

3

Answers


  1. Chosen as BEST ANSWER

    Looks like I can achieve this by using table-layout: auto and setting width: 100% on the cells of the column I would like to expand

    fiddle here: https://jsfiddle.net/klister/ks1u7zf6/54/

    table {
      width: 100%;
      table-layout: auto
    }
    
    .small {
      background: green;
      white-space: nowrap;
    }
    
    .extend {
      background: red;
      width: 100%;
    }
    <table>
      <thead>
        <tr>
          <th class="small">Column 1</th>
          <th class="extend">Column 2</th>
          <th class="small">Column 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="small">First column more content</td>
          <td class="extend">expandable </td>
          <td class="small">Small</td>
        </tr>
      </tbody>
    </table>


  2. Leverage fit-content and white-space: nowrap?

    table {
      display: table;
      width: 100%;
    }
    
    .small {
      background: green;
      width: fit-content;
      white-space: nowrap;
    }
    
    .extend {
      background: red;
      width: 100%;
    }
    <table>
      <thead>
        <th class="small">Column 1</th>
        <th class="extend">Column 2</th>
        <th class="small">Column 3</th>
      </thead>
      <tbody>
        <tr>
          <td class="small">First column</td>
          <td class="extend">This column should fill the remaining space </td>
          <td class="small">Small column 2</td>
        </tr>
      </tbody>
    </table>
    Login or Signup to reply.
  3. table {  
        display: inline-table; 
        width: 99.5%; 
        table-layout: auto; 
        position: absolute; 
        top: 0; 
        left: 40 !important; 
    }
    
    .small { 
        background: green; 
        width: 104px;
        text-align: center; 
    }
    
    .extend { 
        background: red; 
        text-align: center; 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search