Table with table-layout: fixed; and how to make one column wider

后端 未结 4 1672
甜味超标
甜味超标 2020-12-01 03:53

So I have a table with this style:

table-layout: fixed;

Which makes all columns to be of the same width. I would like to have one column (t

相关标签:
4条回答
  • 2020-12-01 04:29

    What you could do is something like this (pseudocode):

    <container table>
      <tr>
        <td>
          <"300px" table>
        <td>
          <fixed layout table>
    

    Basically, split up the table into two tables and have it contained by another table.

    0 讨论(0)
  • 2020-12-01 04:32

    The important thing of table-layout: fixed is that the column widths are determined by the first row of the table.

    So

    if your table structure is as follow (standard table structure)

    <table>
      <thead>
          <tr>
              <th> First column </th>
              <th> Second column </th>
              <th> Third column </th>        
          </tr>
      </thead>
    
       <tbody>
          <tr>
              <td> First column </td>
              <td> Second column </td>
              <td> Third column </td>        
          </tr>
      </tbody>
    

    if you would like to give a width to second column then

    <style> 
        table{
            table-layout:fixed;
            width: 100%;
        }
    
        table tr th:nth-child(2){
           width: 60%;
         }
    
    </style>
    

    Please look that we style the th not the td.

    0 讨论(0)
  • 2020-12-01 04:33

    Are you creating a very large table (hundreds of rows and columns)? If so, table-layout: fixed; is a good idea, as the browser only needs to read the first row in order to compute and render the entire table, so it loads faster.

    But if not, I would suggest dumping table-layout: fixed; and changing your css as follows:

    table th, table td{
    border: 1px solid #000;
    width:20px;  //or something similar   
    }
    
    table td.wideRow, table th.wideRow{
    width: 300px;
    }
    

    http://jsfiddle.net/6p9K3/1/

    0 讨论(0)
  • 2020-12-01 04:43

    You could just give the first cell (therefore column) a width and have the rest default to auto

    table {
      table-layout: fixed;
      border-collapse: collapse;
      width: 100%;
    }
    td {
      border: 1px solid #000;
      width: 150px;
    }
    td+td {
      width: auto;
    }
    <table>
      <tr>
        <td>150px</td>
        <td>equal</td>
        <td>equal</td>
      </tr>
    </table>


    or alternatively the "proper way" to get column widths might be to use the col element itself

    table {
      table-layout: fixed;
      border-collapse: collapse;
      width: 100%;
    }
    td {
      border: 1px solid #000;
    }
    .wide {
      width: 150px;
    }
    <table>
      <col span="1" class="wide">
        <tr>
          <td>150px</td>
          <td>equal</td>
          <td>equal</td>
        </tr>
    </table>

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