tags:

views:

144

answers:

5

Recently i ran into a complex table implementation, for example:

tr1: | td1 | td2 | td3 |
tr2: |  th  |   td   |
tr3: |  th  |   td   |
...

As this example shows, i want td1 and td3 are width fixed, td and td2 are width auto to expand when screen-radio/browser-width changed. I'm wondering if there is a way to do this by using CSS?

Just to be clear, i already used colspan and width value for my purpose, but how can i make td1 smaller than th by using colspan and width value? colspan doesn't work, neither width value

+2  A: 

If you don't specify a width for a td their width will automatically increase as per contents of it. Also, you can use the colspan to equalize a td to what you like...

Sarfraz
+3  A: 

From that limited example, it looks to me like you need two tables.

One for the top row (explicitly set the width of td1, td3, and the entire table. Let td2 auto expand.

And one for the rest of the table... format it as you please, just set the table width to the same as the top row so it looks like one table. You'll probably have to set 0 margins/padding.

That, or you could make it one table and do some colspan hackery.

Mark
yes i did use colspan on tr2 tr3 ..., but td1 of them are kept width same despite of width value, and i'm wondering if i make td1 of tr1 width fixed successfully, will the colspan applied on td2 of tr2/tr3 still work?
Edward
A: 

All cells in a column are equal width. To make cells span over several columns, use the colspan attribute. All cells in a row combined will expand to fill the whole width of the table.

nikc
+6  A: 

It's a simple matter of setting the colspans correctly.

Table Header Row: Each Cell gets a colspan of 2 (3 Cells * 2 colspan = 6 colspan-cells).
Table Body Rows: Each Cell gets a colspan of 3 (2 Cells * 3 colspan = 6 colspan-cells).

UPDATE

There seems to be an inconsistency which involves Columns having both colspan and width set. The Browsers seem to ignore the width value and even max-width (min-width works).

Example:

<table border="1" style="width: 50%;">
    <tr>
        <td style="width: 100px;">td1</td>
        <td colspan="2">td2</td>
        <td style="width: 100px;">td3</td>
    </tr>
    <tr>
        <th colspan="2" style="min-width: 120px; max-width: 120px; width: 120px;">th1</th>
        <td colspan="2">td2</td>
    </tr>
    <tr>
        <th colspan="2" style="min-width: 120px; max-width: 120px; width: 120px;">th1</th>
        <td colspan="2">td2</td>
    </tr>
</table>

This produces roughly the desired output but the th column th1 expands to match the same size of the following td (td2). Both always occupy 50% of the table unless the table is smaller than 240px, then th1 stays at 120px.

This seems inconsistent to me and i have no workaround, best is if you check if you can display the table differently (i.e. same number of columns in all rows including the header (which would make more sense anyways imho)).

dbemerlin
Umm, don't you mean 2 and 3, not 3 and 4?
animuson
Should be 2 and 3, but nice idea.
Kobi
learned much from your answer, but by trying as you said, the td1s (actually td1 and ths, just updated my question) are always kept the same, is it different on th ?
Edward
+3  A: 

Try these colspans:

tr1: | td1(1) | td2(2) | td3(1) |
tr2: |  td1(2)   |   td2(2)     |
tr3: |  td1(2)   |   td2(2)     |
...
pdr
just tried, but td1 are always kept width-same, btw, td1 of tr2 tr3 are actually th, i just updated my question
Edward
With that in mind, I'm wondering if you're using the correct construct for whatever you're trying to do. It doesn't overly make sense to have TH in column 1 sometimes and TD in column 1 other times. Can you give an example of the kind of data you're trying to render? Would DD, DT, DL be more appropriate, for example? http://www.benmeadowcroft.com/webdev/articles/definition-lists.shtml
pdr
not sometimes, just the first row of the table is multiple td inlined, other rows are the same as <th><td>. This problem is really a headache to me, i just gave up to use multiple td in first row, instead with multiple divs, which seems will solve my problem
Edward
Again, hard to say without a firm example, but it sounds like DIVs might be the right way to do it. Only use the table for tabular data, which it sounds like your top row isn't, given that you don't want it to align with anything else in the table.
pdr