Search code examples
csswidthcss-tables

Combining fixed and variable width columns in one table


How can I create a table with CSS like the following (the first three columns with a fixed width in px and the following columns with a width in %):

I know how I can create a column with variable or fixed width.


Solution

  • Nest another table for the 4 columns on the right. The HTML structure would look like:

    <table class = "big first">
    <tr>
    <td class = "px10">Cell1</td>
    <td class = "px20">Cell2</td>
    <td class = "px30">Cell3</td>
    <td>
        <table class = "big">
        <td>1</td><td>2</td><td>3</td><td>4</td>
        </table>
    </td>
    </tr>
    </table>
    

    CSS:

    .big {
        width: 100%;
    }
    .first {
        table-layout: fixed;
    }
    .big, .big td {
        border: 1px sold black;
    }
    .big td {
        background: rgb(0, 162, 232);
    }
    .big .px10 {
        background: orange;
        width: 10px;
    }
    .big .px20 {
        background: green;
        width: 20px;
    }
    .big .px30 {
        background: yellow;
        width: 30px;
    }
    

    And a little demo: little link.

    Edit: it turns out there might be no need for another table: another little link.