Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is colspan not applied as expected

I have a table according to below. The second row has defined three columns, one with colspan=8 and the others with colspan=1. Still, the cells are not stretched according to the colspan, the "width" are a little bit more for second cell and widest for the third.

<table class="floating simpletable">
    <tbody>
        <tr><td colspan="10">1st row</td></tr>
        <tr><td colspan="8">Column 1 -&gt; Least wide</td><td colspan="1">2nd</td><td colspan="1">3rd</td></tr>
        <tr><td colspan="10">3rd row</td></tr>
        <tr><td colspan="1">1st cell</td><td colspan="9">4th row</td></tr>
    </tbody>
</table>

What's the problem and how to fix it?

like image 711
Nicsoft Avatar asked Feb 13 '12 08:02

Nicsoft


People also ask

Does Colspan work with TD?

Usage: It can be used with <td> and <th> element while creating an HTML Table. Attribute Values: It contains a value i.e number Which specify the number of columns that a cell should span. Note: colspan=”0″ tells the browser to span the cell to the last column of the column group (colgroup).

What is the correct use of Colspan?

Definition and Usage The colspan attribute defines the number of columns a table cell should span.

What is the default value of the colspan attribute?

colspan = number [CN] This attribute specifies the number of columns spanned by the current cell. The default value of this attribute is one ("1"). The value zero ("0") means that the cell spans all columns from the current column to the last column of the column group (COLGROUP) in which the cell is defined.

What is the purpose of Colspan in td colspan?

The colspan attribute defines the number of columns a cell should span.


2 Answers

The widths of cells depend on cell contents, HTML and CSS settings for widths, browser, and possibly phase of the moon. The colspan attribute just specifies how many columns (hence, how many slots in the grid for the table) a cell occupies.

If you see the last cell of row 2 as the widest, then the reason is probably that it has most contents (or there is a width setting for it). Your demo code does not demonstrate such behavior.

If you don’t want the column widths adjust to the size requirements of cells, set the widths explicitly in CSS (or in HTML). Before this, it is best to remove all unnecessary complications from the table structure. If your demo code reflects the entire structure, then columns 2 through 8 are an unnecessary division, i.e. they could be turned to a single column. Demonstration (with poor-style pixel widths just for definiteness):

<table class="floating simpletable" border>
    <col width=100><col width=100><col width=100>
    <tbody>
        <tr><td colspan="4">1st row</td></tr>
        <tr><td colspan="2">span 1</td><td>span 2</td><td>span 3 </td></tr>
        <tr><td colspan="4">3rd row</td></tr>
        <tr><td>span</td><td colspan="3">other span</td></tr>
    </tbody>
</table>

Without a rewrite like this, I’m afraid your table violates the table model of HTML, as currently there is no cell that starts in column 3 or column 4 or...

like image 69
Jukka K. Korpela Avatar answered Sep 20 '22 13:09

Jukka K. Korpela


colspan determines how many columns a cell overlaps, not the size of those columns. Use the CSS width property to specify the width of things.

like image 30
Quentin Avatar answered Sep 17 '22 13:09

Quentin