I need to layout a html datatable with CSS.
The actual content of the table can differ, but there is always one main column and 2 or more other columns. I'd like to make the main column take up as MUCH width as possible, regardless of its contents, while the other columns take up as little width as possible. I can't specify exact widths for any of the columns because their contents can change.
How can I do this using a simple semantically valid html table and css only?
For example:
| Main column | Col 2 | Column 3 | <------------------ fixed width in px -------------------> <------- as wide as possible ---------> Thin as possible depending on contents: <-----> <-------->
A better solution than selected answer would be to use border-size rather than border-spacing. The main problem with using border-spacing is that even the first column would have a spacing in the front. To avoid this use: border-left: 100px solid #FFF; and set border:0px for the first column.
The CSS to make all the columns equal in width is as follows. The table-layout: fixed; rule specifies that the table is to take its dimensions from the <table> element's width and the width of the first row of cells (or the <col> elements, if you have them).
Let's say we want the first column to be 40% of the table and the two remaning columns to be 30% (as 40% + 30% +30% = 100%). In order to do this, we use the <col> element. This element is to be placed between the <table> tag and the <thead> tag and we use the style attribute to define the width of the columns.
Similar to Alexk's solution, but possibly a little easier to implement depending on your situation:
<table>
<tr>
<td>foo</td>
<td class="importantColumn">bar</td>
<td>woo</td>
<td>pah</td>
</tr>
</table>
.importantColumn{
width: 100%;
}
You might also want to apply white-space:nowrap
to the cells if you want to avoid wrapping.
I'm far from being a CSS expert but this works for me (in IE, FF, Safari and Chrome):
td.zero_width {
width: 1%;
}
Then in your HTML:
<td class="zero_width">...</td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With