Cell merging helps combine two or more table cells into one bigger cell. It makes tables easier to read and organize.
Cell merging in HTML
<td colspan="number"> ... </td> <td rowspan="number"> ... </td>
<td colspan="2">Merged across 2 columns</td>
<td rowspan="3">Merged down 3 rows</td>
<th colspan="4">Header spanning 4 columns</th>
This table shows how to merge cells horizontally and vertically. The header spans all three columns using colspan="3". The "Apples" cell merges two rows vertically with rowspan="2". The "Oranges" row merges two columns horizontally with colspan="2".
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cell Merging Example</title> <style> table { border-collapse: collapse; width: 80%; margin: 1rem auto; font-family: Arial, sans-serif; } th, td { border: 1px solid #333; padding: 0.75rem; text-align: center; } th { background-color: #f0f0f0; } </style> </head> <body> <main> <h1 style="text-align:center;">Cell Merging in Tables</h1> <table aria-label="Example table showing cell merging"> <thead> <tr> <th colspan="3">Monthly Sales Report</th> </tr> <tr> <th>Product</th> <th>January</th> <th>February</th> </tr> </thead> <tbody> <tr> <td rowspan="2">Apples</td> <td>120</td> <td>135</td> </tr> <tr> <td>130</td> <td>140</td> </tr> <tr> <td>Oranges</td> <td colspan="2">200 (combined sales)</td> </tr> </tbody> </table> </main> </body> </html>
Always make sure the total number of columns in each row matches the table structure after merging.
Use colspan for horizontal merging and rowspan for vertical merging.
Screen readers understand merged cells better when you use semantic tags like <th> for headers.
Cell merging combines multiple table cells into one bigger cell.
Use colspan to merge cells across columns.
Use rowspan to merge cells across rows.
Practice
colspan attribute do in an HTML table?Solution
Step 1: Understand the purpose of
Thecolspancolspanattribute is used in a table cell to make it span across multiple columns horizontally.Step 2: Compare with
rowspanrowspanmerges cells vertically across rows, so it is different fromcolspan.Final Answer:
It merges cells horizontally across columns. -> Option AQuick Check:
colspan= horizontal merge [OK]
- Confusing colspan with rowspan
- Thinking colspan changes cell style
- Assuming colspan merges rows
Solution
Step 1: Identify the correct attribute for column merging
The attribute to merge columns iscolspan, so it should be used ascolspan='2'.Step 2: Check syntax correctness
<td colspan='2'>Merged</td>uses the correct attribute and syntax:<td colspan='2'>Merged</td>. Other options use wrong attributes or invalid syntax.Final Answer:
<td colspan='2'>Merged</td> -> Option CQuick Check:
Correct attribute for columns = colspan [OK]
- Using rowspan instead of colspan
- Using non-existent attributes like merge or span
- Missing quotes around attribute value
<table border='1'>
<tr>
<td rowspan='2'>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
</tr>
</table>Solution
Step 1: Understand rowspan effect
Therowspan='2'on cell 'A' makes it cover two rows vertically in the first column.Step 2: Analyze placement of cells 'B' and 'C'
Cell 'B' is in the first row, second column; cell 'C' is in the second row, second column. They stack vertically next to the tall 'A' cell.Final Answer:
Cell 'A' spans two rows vertically, cells 'B' and 'C' are stacked vertically next to 'A'. -> Option DQuick Check:
rowspan = vertical merge [OK]
- Thinking rowspan merges columns horizontally
- Assuming cells B and C are side by side horizontally
- Confusing rowspan with colspan
<table border='1'>
<tr>
<td colspan='2'>Merged</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
</table>Solution
Step 1: Count columns in first row
The first row has a cell withcolspan='2'plus one more cell, so total columns = 2 + 1 = 3.Step 2: Count columns in second row
The second row has only two cells, so only 2 columns, which causes a mismatch in table layout.Final Answer:
Second row has only two cells but first row has three columns, causing layout mismatch. -> Option AQuick Check:
Column count must match in all rows [OK]
- Ignoring column count mismatch
- Thinking colspan value must match total columns
- Believing colspan is invalid on <td>
Solution
Step 1: Identify correct rowspan and colspan values
The cell should span 2 rows vertically and 3 columns horizontally, sorowspan='2'andcolspan='3'are needed.Step 2: Check attribute order and correctness
Attributes can be in any order, but<td rowspan='2' colspan='3'>Top Left</td>correctly usesrowspan='2'andcolspan='3'. Other options have swapped or incorrect values.Final Answer:
<td rowspan='2' colspan='3'>Top Left</td> -> Option BQuick Check:
Rowspan = rows, colspan = columns [OK]
- Swapping rowspan and colspan values
- Using wrong numbers for spans
- Omitting one of the attributes
Ready to prove it?
Pass the test and this pattern moves unknown
