How to Add a Row to a Table in HTML Easily
To add a row to a table in HTML, use the
<tr> tag inside the <tbody> or <table> element. Inside the <tr>, add cells using <td> for data or <th> for headers.Syntax
Use the <tr> tag to create a new row in a table. Inside it, use <td> tags for each cell in that row. Rows go inside <tbody> or directly inside <table>.
<table>: The whole table container.<tbody>: Groups the body rows (optional but recommended).<tr>: Defines a table row.<td>: Defines a cell in the row.
html
<table>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</tbody>
</table>Output
A table with one row and two cells labeled 'Cell 1' and 'Cell 2'.
Example
This example shows a table with two rows. The second row is added using the <tr> tag with two cells inside.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Table Row Example</title> </head> <body> <table border="1" style="border-collapse: collapse; width: 50%;"> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>30</td> </tr> <tr> <td>Bob</td> <td>25</td> </tr> </tbody> </table> </body> </html>
Output
A table with a header row (Name, Age) and two data rows: Alice 30 and Bob 25.
Common Pitfalls
Common mistakes when adding rows to tables include:
- Forgetting to use
<tr>tags for each row, which breaks the table structure. - Not matching the number of
<td>cells in each row, causing uneven columns. - Placing rows outside the
<tbody>or<table>tags, which is invalid HTML.
Always ensure each row is wrapped in <tr> and cells inside <td>.
html
<!-- Wrong: Missing <tr> --> <table border="1"> <tbody> <td>Only one cell without row</td> </tbody> </table> <!-- Right: Correct row and cell --> <table border="1"> <tbody> <tr> <td>Proper cell inside row</td> </tr> </tbody> </table>
Output
First table breaks layout because cell is outside row; second table shows one proper row with one cell.
Quick Reference
| Tag | Purpose | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Key TakeawaysUse Each row must contain the same number of cells for proper layout. | Place rows inside or directly inside
|