Search code examples
htmlcsshtml-table

Setting table column width


I've got a simple table that is used for an inbox as follows:

<table border="1">
     <tr>
        <th>From</th>
        <th>Subject</th>
        <th>Date</th>
    </tr>
</table>

How do I set the width so the From and Date are 15% of the page width and the Subject is 70%. I also want the table to take up the whole page width.


Solution

  • You can set the width of a table column using the CSS width property of the col element. The width value is most commonly specified in pixels (width: 200px;), or as a percentage of the width of the parent element (width: 50%;). Example with inline style attribute:

    <table style="width: 100%">
        <colgroup>
           <col span="1" style="width: 15%;">
           <col span="1" style="width: 70%;">
           <col span="1" style="width: 15%;">
        </colgroup>
        
        
        
        <!-- Put <thead>, <tbody>, and <tr>'s here! -->
        <tbody>
            <tr>
                <td style="background-color: #777">15%</td>
                <td style="background-color: #aaa">70%</td>
                <td style="background-color: #777">15%</td>
            </tr>
        </tbody>
    </table>