Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS to make a table column take up as much room as possible, and other cols as little

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: <-----> <--------> 
like image 756
EvilPuppetMaster Avatar asked Sep 23 '08 01:09

EvilPuppetMaster


People also ask

How do I increase the space between columns in a table CSS?

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.

How do I make table columns equal width in CSS?

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).

How do I make one column bigger than the other in HTML?

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.


2 Answers

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.

like image 125
Dan Avatar answered Sep 20 '22 21:09

Dan


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>
like image 43
Alexander Kojevnikov Avatar answered Sep 17 '22 21:09

Alexander Kojevnikov