TD column width value not working with fixed table-layout

后端 未结 7 1424
北恋
北恋 2021-01-17 18:52

I have following table structure:

col1 col
相关标签:
7条回答
  • 2021-01-17 19:10

    You should the attribute width without the unit px. Probably there are some modern browsers that accept the attribute with the units, but is not the correct way!

    You have a similar issue in this another Stackoverflow case:

    0 讨论(0)
  • 2021-01-17 19:10

    suggest such an option

    HTML

    <table class="tableStyle">
    <tr>
        <td>col1</td>
        <td>col2</td>
        <td>col3</td>
        <td>col4</td>
        <td>col5</td>
        <td>col6</td>
        <td>col7</td>
        <td>col8</td>
    </tr>
    </table>
    

    CSS

    .tableStyle{
    table-layout:fixed;
    margin: 0 auto; width: 960px;
      background: #ffffd;
    }
    td:nth-child(1n) {
      width: 20px;
      background: #876342;
    }
    td:nth-child(3n+1) {
      width: 100px;
    }
    

    demo

    0 讨论(0)
  • 2021-01-17 19:12

    Seems like works as intended for me. please check the below fiddle.

    .tableStyle{
    table-layout:fixed;
    margin: 0 auto; width: 960px;
    }
    

    http://jsfiddle.net/9x56E/

    0 讨论(0)
  • 2021-01-17 19:17

    The "archaic" width attribute does not take a unit, it expects something like width="20".

    However, the "most correct" way to define a table is like so:

    <table>
        <colgroup>
            <col style="width:20px" />
            <col style="width:50px" span="2" />
            <col style="width:15px" />
            <col style="width:25px" />
            <col style="width:20px" span="3" />
        </colgroup>
        <tbody>
            <tr>
                <td>col1</td>
                <td>col2</td>
                <td>col3</td>
                <td>col4</td>
                <td>col5</td>
                <td>col6</td>
                <td>col7</td>
                <td>col8</td>
            </tr>
        </tbody>
    </table>
    

    This works especially well for large tables, because the browser only needs to read the <colgroup> element to know exactly how the entire table should be laid out, without needing to calculate widths based on individual cell styles.

    0 讨论(0)
  • 2021-01-17 19:20

    Instead of putting the width on the td, try adding it to the th using css.

    For example,

    HTML

    <table>
        <tr>
            <th class="column-1">Heading 1</th>
            <th class="column-2">Heading 2</th>
            <th class="column-3">Heading 3</th>
        </tr>
        <tr>
            <td>TD 1</td>
            <td>TD 2</td>
            <td>TD 3</td>
        </tr>
    </table>
    

    CSS

    .column-1 {
        width: 50%;
    }
    .column-2 {
        width: 25%;
    }
    .column-3 {
        width: 25%;
    }
    

    I had the exact same problem and found this resource helpful:

    https://css-tricks.com/fixing-tables-long-strings/

    0 讨论(0)
  • 2021-01-17 19:23

    You have to use:

    <td width="20">
    

    or

    <td style="width: 20px">
    
    0 讨论(0)
提交回复
热议问题