How do you use colspan and rowspan in HTML tables?

I do not know how to combine rows and columns inside HTML tables.

Example

Could you help me make such a table in HTML?

+72
html html-table
Mar 22 '12 at 21:05
source share
10 answers

I would suggest:

table { empty-cells: show; border: 1px solid #000; } table td, table th { min-width: 2em; min-height: 2em; border: 1px solid #000; } 
 <table> <thead> <tr> <th rowspan="2"></th> <th colspan="4">&nbsp;</th> </tr> <tr> <th>I</th> <th>II</th> <th>III</th> <th>IIII</th> </tr> </thead> <tbody> <tr> <td></td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </tbody> </table> 

Literature:

+95
Mar 22 2018-12-12T00:
source share

If you are confused about how table layouts work, they basically start with x = 0, y = 0 and make their way. Let's explain with graphics because they are so much fun!

When you start a table, you create a grid. Your first row and cell will be in the upper left corner. Think of it as an array pointer, moving to the right with each added value of x and moving down with each incremented value of y.

For your first row, you define only two cells. One spans 2 rows down and one spans 4 columns across. So when you reach the end of your first line, it looks something like this:

Preview # 0001

 <table> <tr> <td rowspan="2"></td> <td colspan="4"></td> </tr> </table> 

Now that the line has finished, the "array pointer" continues to the next line. Since x-position 0 is already occupied by the previous cell, x goes to position 1 to start filling cells. * See Note on row difference.

There are four cells in this row, which are all 1x1 blocks, filling the same row width above it.

Preview # 0002

 <table> <tr> <td rowspan="2"></td> <td colspan="4"></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table> 

The next line is all 1x1 cells. But, for example, what if you added an extra cell? Well, it just jumps from the edge to the right.

Preview # 0003

 <table> <tr> <td rowspan="2"></td> <td colspan="4"></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> 

* But what if we instead (rather than add an extra cell) made all these cells equal to 2? What you need to consider here is that even if you are not going to add more cells to the next row, the row should still exist (although this is an empty row). If you tried to add new cells to the row immediately afterwards, you will notice that it will start adding them to the end of the bottom row.

Preview # 0004

 <table> <tr> <td rowspan="2"></td> <td colspan="4"></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td rowspan="2"></td> <td rowspan="2"></td> <td rowspan="2"></td> <td rowspan="2"></td> <td rowspan="2"></td> </tr> <tr> <td></td> </tr> </table> 

Enjoy the wonderful world of creating tables!

+98
Mar 22 '12 at 21:29
source share

If someone is looking for a rowspan string left and right, here is how you can do it:

 table { border-collapse: collapse; } td { padding: 20px; border: 1px solid black; text-align: center; } 
 <table> <tbody> <tr> <td rowspan="2">LEFT</td> <td> 1 </td> <td> 2 </td> <td> 3 </td> <td> 4 </td> <td rowspan="2">RIGHT</td> </tr> <tr> <td> 5 </td> <td> 6 </td> <td> 7 </td> <td> 8 </td> </tr> <tr> <td> - </td> <td> - </td> <td> - </td> <td> - </td> <td> - </td> <td> - </td> </tr> </tbody> </table> 

Alternatively , if you want to add LEFT and RIGHT to an existing rowset, you can achieve the same result by throwing them with a folded colspan between:

 table { border-collapse: collapse; } td { padding: 20px; border: 1px solid black; text-align: center; } 
 <table> <tbody> <tr> <td rowspan="3">LEFT</td> <td colspan="4" style="padding: 0; border-bottom: solid 1px transparent;"></td> <td rowspan="3">RIGHT</td> </tr> <tr> <td> 1 </td> <td> 2 </td> <td> 3 </td> <td> 4 </td> </tr> <tr> <td> 5 </td> <td> 6 </td> <td> 7 </td> <td> 8 </td> </tr> <tr> <td> - </td> <td> - </td> <td> - </td> <td> - </td> <td> - </td> <td> - </td> </tr> </tbody> </table> 
+10
Mar 08 '14 at 20:59
source share

Use rowspan if you want to expand cells down and colspan to expand.

+5
Mar 22 2018-12-22T00:
source share

You can use rowspan="n" in the td element to make it span n rows, and colspan="m" on the td element to make it span m .

It looks like your first td needs rowspan="2" , and the next td needs colspan="4" .

+3
Mar 22 2018-12-22T00:
source share

The property you are looking for is this first td, rowspan : http://www.angelfire.com/fl5/html-tutorial/tables/tr_code.htm

 <table> <tr><td rowspan="2"></td><td colspan='4'></td></tr> <tr><td></td><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td><td></td><td></td></tr> </table> 
+2
Mar 22 2018-12-12T00:
source share
 <style type="text/css"> table { border:2px black dotted; margin: auto; width: 100%; } tr { border: 2px red dashed; } td { border: 1px green solid; } </style> <table> <tr> <td rowspan="2">x</td> <td colspan="4">y</td> </tr> <tr> <td>I</td> <td>II</td> <td>III</td> <td>IV</td> </tr> <tr> <td>nothing</td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
+1
Mar 22 '12 at 21:12
source share
 <body> <table> <tr><td colspan="2" rowspan="2">1</td><td colspan="4">2</td></tr> <tr><td>3</td><td>3</td><td>3</td><td>3</td></tr> <tr><td colspan="2">1</td><td>3</td><td>3</td><td>3</td><td>3</td></tr> </table> </body> 
0
Mar 22 '12 at 21:15
source share

It looks like your table

  <table border=1 width=50%> <tr> <td rowspan="2">x</td> <td colspan="4">y</td> </tr> <tr> <td bgcolor=#FFFF00 >I</td> <td>II</td> <td bgcolor=#FFFF00>III</td> <td>IV</td> </tr> <tr> <td>empty</td> <td bgcolor=#FFFF00>1</td> <td>2</td> <td bgcolor=#FFFF00>3</td> <td>4</td> </tr> 

0
Mar 23 '12 at 10:44
source share

Colspan and Rowspan The table is divided into rows, and each row is divided into cells. In some situations, we need table cells to span (or merge) more than one column or row. In these situations, we can use the Colspan or Rowspan attributes.

Colspan The colspan attribute determines the number of columns that a cell should span (or merge) horizontally. That is, you want to combine two or more Cells into one cell.

 <td colspan=2 > 

How to calculate?

 <html> <body > <table border=1 > <tr> <td colspan=2 > Merged </td> </tr> <tr> <td> Third Cell </td> <td> Forth Cell </td> </tr> </table> </body> </html> 

Rowspan The rowspan attribute indicates the number of rows a cell should occupy vertically. That is, you want to combine two or more Cells in the same column with one cell vertically.

 <td rowspan=2 > 

How is a Rowspan?

 <html> <body > <table border=1 > <tr> <td> First Cell </td> <td rowspan=2 > Merged </td> </tr> <tr> <td valign=middle> Third Cell </td> </tr> </table> </body> </html> 
0
Apr 17 '18 at 10:06
source share



All Articles