Tables often need to merge rows or columns. It helps to combine different information. For example, if there is no product in the stock table, there is no point in constantly duplicating information - it can be merged, and you can write that the product is out of stock:
Item | Price | Amount |
---|---|---|
Tea | Out of stock |
With HTML, you can combine cells in columns or rows by using the special attributes colspan
and rowspan
. Their values are the number of cells to be merged with the current one, either to the right (for colspan
) or down (for rowspan
) Counting for the number of cells starts from the current cell that the property has been applied to. For example, if the value is colspan="2"
, the current cell will be merged with the neighboring one.
The layout for the example above, without combining cells, is as follows:
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tea</td>
<td>Out of stock</td> <!-- the cell we want to combine -->
<td></td>
</tr>
</tbody>
</table>
In order to merge cells, you need to do two things:
colspan
attribute with a value equal to the number of cells to be merged to the right<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tea</td>
<td colspan="2">Out of stock</td>
</tr>
</tbody>
</table>
The rowspan
attribute is used to combine cells vertically. The algorithm is the same as the with horizontal merging. The only difference is that you have to delete cells in neighboring rows:
<table>
<thead>
<tr>
<th>Employee</th>
<th>Salary</th>
<th>Bonus</th>
<th>Manager</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alex Primadonin</td>
<td>750$</td>
<td>63$</td>
<td rowspan="2">Cody Hexly</td>
</tr>
<tr>
<td>Eddie Vainy</td>
<td>1200$</td>
<td>0</td>
</tr>
</tbody>
</table>
In this example, there are two employees interacting with one manager. You can specify it twice, but it's clearer if you merge the cells vertically. To do this, the attribute rowspan="2"
was added, and in the second row the fourth cell was removed, because its place will be taken by the cell above.
<tbody>
<tr>
<td>Alex Primadonin</td>
<td>750$</td>
<td>63$</td>
<td rowspan="2">Cody Hexly</td>
</tr>
<tr>
<td>Eddie Vainy</td>
<td>1200$</td>
<td>0</td>
</tr>
</tbody>
Employee | Salary | Bonus | Manager |
---|
Create a table with two rows and three columns. In the second row, combine the first two cells using the attributes you learned about. The first row should be the header of the table
The number of cells in each row of the table must be the same after merging. It's important to distinguish between the number of <td>
tags and the total number of cells. For example, using the colspan="2"
attribute on a cell you will get two cells but visually combined in one. For this reason, it's necessary to remove one physical cell from the markup to compensate
If the colspan
attribute is used, then the cells in the same line will be removed from the HTML code. If the rowspan
attribute is used, the cells in the rows below are deleted
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
1const { test } = require('tests');
2
3test(({ querySelectorAll, query, expect }) => {
4 const trs = querySelectorAll(document, 'table > tbody > tr', HTMLElement);
5 expect(trs).to.have.length(1);
6
7 const trHead = querySelectorAll(document, 'table > thead > tr');
8 expect(trHead).to.have.length(1);
9
10 const tds = querySelectorAll(document, 'table > tbody > tr > td', HTMLElement);
11 expect(tds).to.have.length(2);
12
13 const tdWithColspan = query(document, 'table > tbody > tr > td:nth-child(1)', HTMLElement);
14 expect(tdWithColspan).to.have.attribute('colspan', '2');
15});
16
Teacher's solution will be available in: