Search code examples
htmlcssdynamicwidthcss-tables

HTML CSS Table layout


I have a problem creating a proper table layout.

I need the table to have a specific width, with 3 columns (no problems so far). The problem is that I need the 2nd column needs to be only the width of its content, and no bigger, and that column has to dynamically adapt to that content.

The other two should take up the rest of the width of the table.

Example:

---------------------------------------------------------------------------------
|                             |Here is the main text|                           |
---------------------------------------------------------------------------------

Solution

  • DEMO FIDDLE

    HTML

    <table>
        <tr>
            <td>Row 1 Col 1</td>
            <td>Row 1 Col 2</td>
            <td>Row 1 Col 3</td>
        </tr>
    </table>
    

    CSS

    table {
        width:100%;
    }
    td {
        border:1px solid grey;
    }
    td:nth-child(2) {
        width:1px;
        white-space:nowrap;
    }