Skip to content

<tr> - Table Row Element

Table Element

The <tr> (table row) element defines a row of cells in a table. Each row contains one or more <th> (header) or <td> (data) cells. Rows are the fundamental building blocks of table structure, organizing data horizontally across columns.

Result
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>

The <tr> element can only appear inside:

  • <table> (directly, though not recommended)
  • <thead> (for header rows)
  • <tbody> (for body rows)
  • <tfoot> (for footer rows)

The <tr> element supports all global HTML attributes.

Result
Result
Result
Result
Result
Result
Result
Result
Result
Result
Result
/* Basic row styling */
tr {
border-bottom: 1px solid #ddd;
}
/* Alternating rows */
tr:nth-child(odd) {
background-color: #f9f9f9;
}
tr:nth-child(even) {
background-color: #ffffff;
}
/* Hover effect */
tbody tr:hover {
background-color: #e8f4f8;
cursor: pointer;
}
/* First and last row */
tbody tr:first-child {
border-top: 2px solid #333;
}
tbody tr:last-child {
border-bottom: 2px solid #333;
}
/* Selected row */
tr.selected {
background-color: #3498db;
color: white;
}
/* Row with status */
tr.active {
border-left: 4px solid #28a745;
}
tr.pending {
border-left: 4px solid #ffc107;
}
tr.inactive {
border-left: 4px solid #dc3545;
}
/* Transition effects */
tr {
transition: all 0.3s ease;
}
tr:hover {
transform: scale(1.01);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

Rows are commonly manipulated with JavaScript:

// Select all rows in tbody
const rows = document.querySelectorAll('tbody tr');
// Add click handler to each row
rows.forEach(row => {
row.addEventListener('click', () => {
// Toggle selected class
row.classList.toggle('selected');
});
});
// Add a new row
const tbody = document.querySelector('tbody');
const newRow = tbody.insertRow();
const cell1 = newRow.insertCell(0);
const cell2 = newRow.insertCell(1);
cell1.textContent = 'New Item';
cell2.textContent = 'New Value';
// Delete a row
rows[0].remove();

Use Semantic Parents

Always place <tr> inside <thead>, <tbody>, or <tfoot>, not directly in <table>.

Consistent Cell Count

Ensure each row has the same number of cells (accounting for colspan/rowspan).

Use Row Headers

Use <th scope="row"> for the first cell when it labels the row.

Consider Performance

For very long tables, consider pagination or virtual scrolling.

Add Hover States

Improve UX by highlighting rows on hover.

Test Accessibility

Ensure screen readers can navigate rows properly.

<!-- ❌ Bad: mixing header and data cells incorrectly -->
<tr>
<th>Name</th>
<td>Age</td>
<th>Location</th>
</tr>
<!-- ✅ Good: use th only for headers -->
<tr>
<th scope="row">John Doe</th>
<td>32</td>
<td>New York</td>
</tr>
<!-- ❌ Bad: tr directly in table -->
<table>
<tr><td>Data</td></tr>
</table>
<!-- ✅ Good: tr inside tbody -->
<table>
<tbody>
<tr><td>Data</td></tr>
</tbody>
</table>
<!-- ❌ Bad: inconsistent cell counts -->
<tbody>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
<!-- Missing third cell! -->
</tr>
</tbody>
<!-- ✅ Good: consistent structure -->
<tbody>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
</tbody>

The <tr> element is supported in all browsers:

  • ✅ Chrome (all versions)
  • ✅ Firefox (all versions)
  • ✅ Safari (all versions)
  • ✅ Edge (all versions)
  • ✅ Opera (all versions)
  • ✅ Internet Explorer (all versions)