Setting table column width
Solution 1:
<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>
Solution 2:
table {
width: 100%;
border: 1px solid #000;
}
th.from, th.date {
width: 15%
}
th.subject {
width: 70%; /* Not necessary, since only 70% width remains */
}
<table>
<thead>
<tr>
<th class="from">From</th>
<th class="subject">Subject</th>
<th class="date">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>[from]</td>
<td>[subject]</td>
<td>[date]</td>
</tr>
</tbody>
</table>
The best practice is to keep your HTML
and CSS
separate for less code duplication, and for separation of concerns (HTML
for structure and semantics, and CSS
for presentation).
Note that, for this to work in older versions of Internet Explorer, you may have to give your table a specific width (e.g., 900px). That browser has some problems rendering an element with percentage dimensions if its wrapper doesn't have exact dimensions.
Solution 3:
Use the CSS below, the first declaration will ensure your table sticks to the widths you provide (you'll need to add the classes in your HTML):
table{
table-layout:fixed;
}
th.from, th.date {
width: 15%;
}
th.subject{
width: 70%;
}
Solution 4:
Alternative way with just one class while keeping your styles in a CSS file, which even works in IE7:
<table class="mytable">
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
<style>
.mytable td, .mytable th { width:15%; }
.mytable td + td, .mytable th + th { width:70%; }
.mytable td + td + td, .mytable th + th + th { width:15%; }
</style>
More recently, you can also use the nth-child()
selector from CSS3 (IE9+), where you'd just put the nr. of the respective column into the parenthesis instead of stringing them together with the adjacent selector. Like this, for example:
<style>
.mytable tr > *:nth-child(1) { width:15%; }
.mytable tr > *:nth-child(2) { width:70%; }
.mytable tr > *:nth-child(3) { width:15%; }
</style>
Solution 5:
These are my two suggestions.
-
Using classes. There is no need to specify width of the two other columns as they will be set to 15% each automatically by the browser.
table { table-layout: fixed; } .subject { width: 70%; }
<table> <tr> <th>From</th> <th class="subject">Subject</th> <th>Date</th> </tr> </table>
-
Without using classes. Three different methods but the result is identical.
a)
table { table-layout: fixed; } th+th { width: 70%; } th+th+th { width: 15%; }
<table> <tr> <th>From</th> <th>Subject</th> <th>Date</th> </tr> </table>
b)
table { table-layout: fixed; } th:nth-of-type(2) { width: 70%; }
<table> <tr> <th>From</th> <th>Subject</th> <th>Date</th> </tr> </table>
c) This one is my favourite. Same as b) but with better browser support.
table { table-layout: fixed; } th:first-child+th { width: 70%; }
<table> <tr> <th>From</th> <th>Subject</th> <th>Date</th> </tr> </table>