Skip to main content

HTML Tags

HTML <tr> Tag

Written by Updated HTML <tr> Tag exercises

The tr tag is one row of a table. Everything in a table sits inside rows: each tr holds the cells of that row, th for headers and td for data. There is no tag for columns; columns appear because every row has the same number of cells.

Syntax

html

<tr>
  <td>Cell 1</td>
  <td>Cell 2</td>
</tr>

Attributes

<tr> has no attributes of its own today. Old attributes like align, bgcolor and valign are obsolete; use CSS instead, for example tr:nth-child(even) { background: #f5f5f5; } for striped rows.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML tr Tag Example</title>
</head>
<body>
  <h2>Example of the &lt;tr&gt; Tag</h2>
  <table border="1">
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$150</td>
    </tr>
  </table>
</body>
</html>

The table has two rows, each with a month and an amount. Each <tr> becomes one horizontal line of the table.

Where tr goes

A <tr> sits inside <table>, or inside one of the row groups: <thead> for header rows, <tbody> for the data and <tfoot> for totals. Inside a <tr> you can only put <th> and <td> cells, never text directly.

If you leave out <tbody>, the browser adds one for you. That matters for CSS: table > tr matches nothing, because the rows are really inside the invisible <tbody>. Write table tr or tbody tr instead.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Rows that span

A cell with rowspan="2" reaches down into the next row, so that next <tr> needs one cell fewer. When a table looks broken, count the cells in each row, including spans: they must add up to the same number. More on this in the td tag.

Styling rows

Readable rows

css

tbody tr:nth-child(even) { background: #f8fafc; }
tbody tr:hover { background: #e0f2fe; }

Borders on <tr> only work with border-collapse: collapse on the table. Usually it is easier to put borders on the cells.

HTML <tr> Tag exercises

Add a row

Make a table with one row of three cells: Red, Green, Blue.

Result

Check your answer to see it here.