Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html table colspan not working as expected

HTML concepts are so terrible sometimes.

This is my code using colspan in html table, and doesn't look as I expect.

<table border="1">
  <tr>
    <td colspan="3">a</td>
  </tr>
  <tr>
    <td colspan="2">b</td>
    <td>c</td>
  </tr>
</table>

What I want is: cell 'a' should look 3 cell wide, cell 'b' should look 2 cell wide, cell 'c' should look 1 cell wide.

What it is doing is: cell 'a' is 2 cells wide, cell 'b' & 'c' is 1 cell wide.

Any Suggestions thanks.

like image 312
Sushil Kumar Jain Avatar asked Feb 13 '17 08:02

Sushil Kumar Jain


People also ask

Why Colspan is not working in HTML?

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.

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.

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 does Colspan 3 mean?

colspan specifies the number of columns a cell should span. < td colspan="number"> If I have 3 columns and I want a specific row to cover all 3 columns I program the row in this way: <td colspan="3"> </td>


1 Answers

Attribute colspan determines how many columns a cell overlaps with respect to other cells, not the absolute size of those columns. In your case, span 2 has two spans. how you can say it is not? don't judge it by width of a cell. span is not the width. You have to add another smaller columns to appear it as a column of two spans.

See the solution for your expectation in code snippet last example.

<h3>Example 1</h3>

<table border="1">
    <tr><td>col1</td><td>col2</td><td>col3</td></tr>
    <tr><td colspan="3">a</td></tr>
    <tr><td colspan="2">b</td><td colspan="1">c</td></tr>
</table>

<h3>Example 2</h3>

<table border="1">
    <tr><td width="80px">wide col1</td><td>col2</td><td>col3</td></tr>
    <tr><td colspan="3">span 3</td></tr>
    <tr><td colspan="2">span 2</td><td colspan="1">span 1</td></tr>
</table>

<h3>Your case</h3>

<table border="1">
    <tr><td colspan="3">span 3</td></tr>
    <tr><td colspan="2" width="66%">span 2</td><td width="33%">span 1</td></tr>
</table>
like image 114
TRiNE Avatar answered Oct 17 '22 00:10

TRiNE