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.
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Product Catalog</caption>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Category</th>
<th scope="col">Price</th>
<th scope="col">Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>Wireless Mouse</td>
<td>Electronics</td>
<td>$29.99</td>
<td>45</td>
</tr>
<tr>
<td>Notebook Set</td>
<td>Stationery</td>
<td>$12.50</td>
<td>120</td>
</tr>
<tr>
<td>USB Cable</td>
<td>Electronics</td>
<td>$8.99</td>
<td>89</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead tr {
background-color: #2c3e50;
color: white;
}
tbody tr:nth-child(even) {
background-color: #f8f9fa;
}
tbody tr:hover {
background-color: #e3f2fd;
}
</style>
The <tr> element can only appear inside:
<table> (directly, though not recommended)
<thead> (for header rows)
<tbody> (for body rows)
<tfoot> (for footer rows)
Best Practice
Always place <tr> elements inside <thead>, <tbody>, or <tfoot> for proper semantic structure, even though browsers will auto-correct if you don’t.
The <tr> element supports all global HTML attributes .
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Employee List</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Department</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice Johnson</td>
<td>Engineering</td>
<td>alice@example.com</td>
</tr>
<tr>
<td>Bob Smith</td>
<td>Marketing</td>
<td>bob@example.com</td>
</tr>
<tr>
<td>Carol White</td>
<td>Sales</td>
<td>carol@example.com</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead tr {
background-color: #16a085;
color: white;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Browser Usage Statistics</caption>
<thead>
<tr>
<th scope="col">Browser</th>
<th scope="col">Desktop</th>
<th scope="col">Mobile</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Chrome</th>
<td>65.2%</td>
<td>62.8%</td>
</tr>
<tr>
<th scope="row">Safari</th>
<td>18.5%</td>
<td>24.3%</td>
</tr>
<tr>
<th scope="row">Firefox</th>
<td>8.1%</td>
<td>0.9%</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
}
thead tr {
background-color: #8e44ad;
color: white;
}
tbody th {
background-color: #ecf0f1;
text-align: left;
}
td {
text-align: right;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Task List (Click rows to select)</caption>
<thead>
<tr>
<th scope="col">Task</th>
<th scope="col">Status</th>
<th scope="col">Priority</th>
</tr>
</thead>
<tbody>
<tr class="clickable">
<td>Design homepage</td>
<td>Complete</td>
<td>High</td>
</tr>
<tr class="clickable">
<td>Write documentation</td>
<td>In Progress</td>
<td>Medium</td>
</tr>
<tr class="clickable">
<td>Update dependencies</td>
<td>Pending</td>
<td>Low</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
thead tr {
background-color: #34495e;
color: white;
}
tbody tr.clickable {
cursor: pointer;
transition: all 0.2s ease;
}
tbody tr.clickable:hover {
background-color: #3498db;
color: white;
transform: translateX(5px);
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr><td>Product A</td><td>$19.99</td></tr>
<tr><td>Product B</td><td>$24.99</td></tr>
<tr><td>Product C</td><td>$29.99</td></tr>
<tr><td>Product D</td><td>$34.99</td></tr>
<tr><td>Product E</td><td>$39.99</td></tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
thead tr {
background-color: #2c3e50;
color: white;
}
tbody tr:nth-child(odd) {
background-color: #ffffff;
}
tbody tr:nth-child(even) {
background-color: #f8f9fa;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Score</th>
</tr>
</thead>
<tbody>
<tr><td>Alice</td><td>95</td></tr>
<tr><td>Bob</td><td>87</td></tr>
<tr><td>Carol</td><td>92</td></tr>
<tr><td>David</td><td>78</td></tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 12px;
text-align: left;
border: 1px solid #ddd;
}
thead tr {
background-color: #27ae60;
color: white;
}
tbody tr {
transition: background-color 0.3s ease;
}
tbody tr:hover {
background-color: #d5f4e6;
cursor: pointer;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Order Status</caption>
<thead>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Status</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
<tr class="status-success">
<td>#1001</td>
<td>Delivered</td>
<td>$125.00</td>
</tr>
<tr class="status-warning">
<td>#1002</td>
<td>Processing</td>
<td>$89.50</td>
</tr>
<tr class="status-error">
<td>#1003</td>
<td>Failed</td>
<td>$250.00</td>
</tr>
<tr class="status-success">
<td>#1004</td>
<td>Delivered</td>
<td>$42.99</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
thead tr {
background-color: #34495e;
color: white;
}
.status-success {
background-color: #d4edda;
border-left: 4px solid #28a745;
}
.status-warning {
background-color: #fff3cd;
border-left: 4px solid #ffc107;
}
.status-error {
background-color: #f8d7da;
border-left: 4px solid #dc3545;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Selectable Data (Click to select)</caption>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>Item Alpha</td>
<td>$100</td>
</tr>
<tr class="selected">
<td>002</td>
<td>Item Beta</td>
<td>$200</td>
</tr>
<tr>
<td>003</td>
<td>Item Gamma</td>
<td>$150</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
thead tr {
background-color: #3498db;
color: white;
}
tbody tr {
cursor: pointer;
transition: all 0.2s ease;
}
tbody tr:hover {
background-color: #f0f0f0;
}
tbody tr.selected {
background-color: #3498db;
color: white;
font-weight: bold;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Details</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Project Alpha</td>
<td>Web Development</td>
<td>Active</td>
</tr>
<tr>
<td colspan="3" style="text-align: center; background-color: #ecf0f1;">
<em>— No data for this period —</em>
</td>
</tr>
<tr>
<td>Project Beta</td>
<td>Mobile App</td>
<td>Planning</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead tr {
background-color: #16a085;
color: white;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Monthly Performance Metrics</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Sales</th>
<th scope="col">Growth</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>$25,000</td>
<td>+5%</td>
</tr>
<tr>
<th scope="row">February</th>
<td>$28,000</td>
<td>+12%</td>
</tr>
<tr>
<th scope="row">March</th>
<td>$31,000</td>
<td>+11%</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
}
thead tr {
background-color: #e74c3c;
color: white;
}
tbody th {
background-color: #ecf0f1;
text-align: left;
}
tbody td {
text-align: right;
}
</style>
Screen Reader Benefit
Using <th scope="row"> allows screen readers to announce: “January: $25,000” instead of just “$25,000”, providing crucial context.
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Product Details (Expandable)</caption>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr class="main-row">
<td>▶ Laptop Pro</td>
<td>$1,299</td>
</tr>
<tr class="detail-row">
<td colspan="2">
<div style="padding: 10px; background: #f8f9fa;">
<strong>Specifications:</strong><br>
• 16GB RAM<br>
• 512GB SSD<br>
• Intel i7 Processor
</div>
</td>
</tr>
<tr class="main-row">
<td>▶ Wireless Mouse</td>
<td>$29</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead tr {
background-color: #9b59b6;
color: white;
}
.main-row {
cursor: pointer;
font-weight: bold;
}
.main-row:hover {
background-color: #f0e6f6;
}
.detail-row {
background-color: #fdfefe;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Department Breakdown</caption>
<thead>
<tr>
<th scope="col">Employee</th>
<th scope="col">Role</th>
</tr>
</thead>
<tbody>
<tr class="group-header">
<td colspan="2"><strong>Engineering</strong></td>
</tr>
<tr>
<td>Alice Chen</td>
<td>Senior Developer</td>
</tr>
<tr>
<td>Bob Martinez</td>
<td>DevOps Engineer</td>
</tr>
<tr class="group-header">
<td colspan="2"><strong>Marketing</strong></td>
</tr>
<tr>
<td>Carol White</td>
<td>Marketing Manager</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead tr {
background-color: #2c3e50;
color: white;
}
.group-header {
background-color: #95a5a6;
color: white;
}
tbody tr:not(.group-header):hover {
background-color: #ecf0f1;
}
</style>
border-bottom : 1 px solid # ddd ;
background-color : # f9f9f9 ;
background-color : # ffffff ;
background-color : # e8f4f8 ;
border-top : 2 px solid # 333 ;
border-bottom : 2 px solid # 333 ;
background-color : # 3498db ;
border-left : 4 px solid # 28a745 ;
border-left : 4 px solid # ffc107 ;
border-left : 4 px solid # dc3545 ;
transition : all 0.3 s ease ;
box-shadow : 0 2 px 4 px 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
row . addEventListener ( ' click ' , () => {
row . classList . toggle ( ' selected ' );
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 ' ;
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 -->
<!-- ✅ Good: use th only for headers -->
< th scope = " row " > John Doe </ th >
<!-- ❌ Bad: tr directly in table -->
<!-- ✅ Good: tr inside tbody -->
<!-- ❌ Bad: inconsistent cell counts -->
<!-- Missing third cell! -->
<!-- ✅ Good: consistent structure -->
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)