To "link" two rows in HTML, you're likely asking about merging table rows. You can achieve this using the rowspan
attribute within the <td>
or <th>
tags. This attribute specifies the number of rows a cell should span.
Using the rowspan
Attribute
The rowspan
attribute is used to make a table cell span multiple rows.
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td rowspan="2">Cell that spans two rows</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
</tr>
</table>
In this example, the first cell in the second row (<td>Cell that spans two rows</td>
) spans two rows because rowspan
is set to "2". This effectively merges the first cell of the second and third rows.
Explanation:
<table>
: Defines the HTML table.<tr>
: Defines a table row.<th>
: Defines a table header cell.<td>
: Defines a standard table data cell.rowspan="2"
: This attribute, when placed inside a<td>
or<th>
tag, makes that cell span two rows vertically. You can change the number ("2") to specify a different number of rows to span.
Example: Practical Insight
Imagine creating a timetable. You might have a lecture that lasts for two hours, effectively taking up two rows in your timetable. The rowspan
attribute helps represent this visually in an HTML table.
<table>
<tr>
<th>Time</th>
<th>Monday</th>
</tr>
<tr>
<td>9:00 - 10:00</td>
<td rowspan="2">Lecture A</td>
</tr>
<tr>
<td>10:00 - 11:00</td>
</tr>
</table>
Here, "Lecture A" occupies the 9:00-10:00 and 10:00-11:00 time slots.
Important Considerations:
- Table Structure: Ensure your table structure is logically sound. Using
rowspan
effectively requires planning how the data will align. - Accessibility: Consider accessibility when using
rowspan
. Screen readers may interpret complex tables with spanned rows in unexpected ways. Provide appropriate ARIA attributes if necessary to improve accessibility.