: The Table Row element - HTML: HyperText Markup Language | MDN (original) (raw)
Baseline
Widely available *
The <tr>
HTML element defines a row of cells in a table. The row's cells can then be established using a mix of
Try it
<table>
<caption>
Alien football stars
</caption>
<tr>
<th scope="col">Player</th>
<th scope="col">Gloobles</th>
<th scope="col">Za'taak</th>
</tr>
<tr>
<th scope="row">TR-7</th>
<td>7</td>
<td>4,569</td>
</tr>
<tr>
<th scope="row">Khiresh Odo</th>
<td>7</td>
<td>7,223</td>
</tr>
<tr>
<th scope="row">Mia Oolong</th>
<td>9</td>
<td>6,219</td>
</tr>
</table>
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
th[scope="col"] {
background-color: #505050;
color: #fff;
}
th[scope="row"] {
background-color: #d6ecd4;
}
td {
text-align: center;
}
tr:nth-of-type(even) {
background-color: #eee;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
caption {
caption-side: bottom;
padding: 10px;
}
Attributes
This element includes the global attributes.
Deprecated attributes
The following attributes are deprecated and should not be used. They are documented below for reference when updating existing code and for historical interest only.
Specifies the horizontal alignment of each row cell. The possible enumerated values are left
, center
, right
, justify
, and char
. When supported, the char
value aligns the textual content on the character defined in the char attribute and on offset defined by the charoff attribute. Use the text-align CSS property instead, as this attribute is deprecated.
Defines the background color of each row cell. The value is an HTML color; either a 6-digit hexadecimal RGB code, prefixed by a #
, or a color keyword. Other CSS values are not supported. Use the background-color CSS property instead, as this attribute is deprecated.
Specifies the alignment of the content to a character of each row cell. Typical values for this include a period (.
) when attempting to align numbers or monetary values. If align is not set to char
, this attribute is ignored.
Specifies the number of characters to offset the row cell content from the alignment character specified by the char attribute.
Specifies the vertical alignment of each row cell. The possible enumerated values are baseline
, bottom
, middle
, and top
. Use the vertical-align CSS property instead, as this attribute is deprecated.
Usage notes
- The
<tr>
element is valid as a child of a , , or element only. - If the
<tr>
is placed as a direct child of its parentelement, the
<tbody>
parent is implied and browsers will add the<tbody>
to the markup. - The implied
<tbody>
parent is only supported if the<table>
otherwise has no child<tbody>
elements, and only if the<tr>
is included after any, , and <thead>
elements. - The CSS pseudo-classes :nth-of-type, :first-of-type, and :last-of-type are often useful for selecting the desired set of rows and their data and header cells (
and elements). - When a
<tr>
is included as a direct child of the<table>
, as the browser adds a<tbody>
to the markup, CSS selectors such astable > tr
may not work as expected or at all.
Examples
Basic row setup
This example demonstrates a table with four rows and three columns, where the first column contains headers for the row data cells.
HTML
Four <tr>
elements are used to create four table rows. Each row contains three cells - one header cell (
row
.
<table>
<tbody>
<tr>
<th scope="row">A</th>
<td>Alfa</td>
<td>AL fah</td>
</tr>
<tr>
<th scope="row">B</th>
<td>Bravo</td>
<td>BRAH voh</td>
</tr>
<tr>
<th scope="row">C</th>
<td>Charlie</td>
<td>CHAR lee</td>
</tr>
<tr>
<th scope="row">D</th>
<td>Delta</td>
<td>DELL tah</td>
</tr>
</tbody>
</table>
CSS
The CSS :nth-of-type pseudo-class is used to select every odd
row and set the background-color of those rows to a slightly darker tone, creating a so-called "zebra stripe" effect. This alternating background makes the rows of data in the table easier to parse and read—imagine having many rows and columns and trying to find some data in a particular row. In addition, the row header cells (
tr:nth-of-type(odd) {
background-color: #eee;
}
tr th[scope="row"] {
background-color: #d6ecd4;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
Result
This example extends the basic table from the previous example by adding a header row as the first row of the table.
HTML
An additional table row (<tr>
) is added as the first row of the table with column header cells (
<th>
) within this head row to explicitly specify that each header cell relates to all the cells within its own column, even though those cells are in the .
CSS
The CSS is nearly unchanged from the previous example, except for some additional styling to highlight the "header row" so that the headers of the columns stand out from the other cells.
Result
Sorting rows
There are no native methods for sorting the rows (<tr>
elements) of a
sort()
function can be implemented in JavaScript to sort an HTMLCollection of <tr>
elements.
HTML
A element is used in this basic table to mark the body section of the table and to include three rows (<tr>
elements) with data (
<table>
<tbody>
<tr>
<td>3</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>1</td>
</tr>
</tbody>
</table>
JavaScript
In the JavaScript code below, the created sort()
function is attached to the element so that it sorts the table cells in order of increasing value and updates the display accordingly.
HTMLTableSectionElement.prototype.sort = function (cb) {
Array.from(this.rows)
.sort(cb)
.forEach((e) => this.appendChild(this.removeChild(e)));
};
document
.querySelector("table")
.tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
td {
border: 1px solid rgb(160 160 160);
padding: 4px 8px;
}
Result
This example extends the basic table from the previous example by making the sorting interactive and independent for multiple columns.
HTML
<tr>
element) within the table body ( element) to create a second column with letters in ascending order. Using the element, a head section is added before the body section to introduce a head row with table header cells (JavaScript
A click event handler is added to each table header (
<tr>
elements) of the based on the contents of the data cells (**Note:**This solution assumes that the
Result
**Note:**To be usable and accessible, the header cell of each sortable column must be identifiable as a sorting button and each must define whether the column is currently sorted in ascending or descending order visually and with the aria-sort attribute. See the ARIA Authoring Practices Guide's sortable table example for more information.
Technical summary
Content categories | None | ||
---|---|---|---|
Permitted content | Zero or more | and/or | elements;script-supporting elements ( |