HTLM tr

What is the <tr> Element?

The <tr> element in HTML is used to define a row of cells in a table. Its primary purpose is to organize and structure data in a tabular format. It acts as a container for individual cells, which can be created using the <td> (table data) or <th> (table header) elements.

The <tr> element plays a key role in establishing the layout of a table. By using multiple <tr> elements, you can create multiple rows, each containing a set of cells. This arrangement allows for easy readability and clear data structure.

To create a row of cells, you enclose the individual cells within the opening <tr> and closing </tr> tags. Within the <tr> element, you use <td> to create standard data cells or <th> for header cells.

Anatomy of a Table Row

HTML tables can be enhanced for clarity by using the <thead>, <tfoot>, and <tbody> elements:

  • <thead>: Acts as a parent container for the table headings. It groups header rows together, making it easier to identify headers from the rest of the content.
  • <tfoot>: Contains summary data, such as descriptions or calculations, for each column of the table. It appears at the end of the table, after all <tbody> rows.
  • <tbody>: Holds the main content rows of the table, separating them from the header and footer. This grouping allows for better organization and easier data manipulation.

Opening and Closing Tags

In HTML, every element is marked by an opening and closing tag:

  • Opening tags: Initiate an HTML element and are enclosed in angle brackets. For example, <p> is the opening tag for a paragraph.
  • Closing tags: Signify the end of an element and are enclosed in angle brackets with a forward slash. For example, </p> closes a paragraph.

The placement of opening and closing tags around content ensures that the browser interprets the structure and formatting correctly.

Table Data Cells (<td>) and Table Header Cells (<th>)

When creating a table, it is important to identify cells as either data cells (<td>) or header cells (<th>):

— Table Data Cells (<td>): Contain the actual content or data of the table (e.g., text, numbers). For example:

<td>This is a data cell</td>

— Table Header Cells (<th>): Define headings or titles for columns or rows in the table, providing a clear structure. For example:

<th>Column 1</th>

By properly using <td> for data cells and <th> for header cells, you create a well-structured, readable table.

Creating a Simple Table Row

To add a new table row in HTML, use the <tr> tag. A table is created using the <table> tag, and each row is represented by a <tr> tag.

For example, to create a row with two cells, "Name" and "Age," use:

<tr>
  <td>Name</td>
  <td>Age</td>
</tr>

Make sure to close the table row with </tr>.

Attributes of the <tr> Element

The <tr> element can include attributes to modify its visual and functional properties:

  • bgcolor Attribute: Sets the background color of the table row. You can specify colors using CSS color codes or names.
  • style Attribute: Provides more styling options, allowing direct control over properties like font, padding, margins, and more.

By utilizing these attributes, you can fully customize the appearance of your table rows.

Overall, the <tr> element is essential for creating structured table rows in HTML. Using attributes like bgcolor and style, you can adjust the visual appearance to match your design needs.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate