HTML - <tr> Tag



In an HTML table, a row is defined with the <tr> tag. The row's cells can then be established using a mix of <td> and <th> elements.

Each <tr> tag can have one or more <th> tag that specify the table's header cells or one or more <td> tag that define the table's standard cells. The <tr> tag can either be a nested child of the <thead>, <tbody>, and <tfoot> elements or a direct child of the <table> element.

Syntax

<tr>
   ....
</tr>

Attribute

HTML tr tag supports Global and Event attributes of HTML. Bellow mentioned attributes are deprecated so use CSS properties rather using these attributes.

Attribute Value Description
align left
right
center
justify
Specifies the alignment of text content(Deprecated).
bgcolor color Specifies the background color of each column cell(Deprecated).
char character Specifies the alignment of the content to a character of each column cell(Deprecated).
charoff number Specifies the number of characters to offset the column cell content from the alignment character specified by the char attribute(Deprecated).
valign baseline
bottom
middle
top
Specifies the vertical alignment of each column cell(Deprecated).

Examples of HTML tr Tag

Bellow examples will illustrate the usage of tr tag. Where, when and how to use tr tag to create table row.

Creating Simple HTML Table

Let's consider the following example, where we are going to use the <tr> tag to create three rows, one header row and two data rows.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.25px solid #DE3163;
      }
   </style>
<body>
   <table>
      <tr>
         <th>University</th>
         <th>Place</th>
      </tr>
      <tr>
         <td>LPU</td>
         <td>Punjab</td>
      </tr>
      <tr>
         <td>Amity</td>
         <td>Noida</td>
      </tr>
      <tr>
      <td>Amrutha</td>
      <td>Coimbatore</td>
      </tr>
   </table>
</body>
</html>

Adding Background Color on Table Row

Considering another scenario where we are going to apply th background-color to the <tr> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1px solid black;
      }
   </style>
<body>
   <table>
      <tr>
         <th>Bike</th>
         <th>Owner</th>
      </tr>
      <tr style="background-color:#D5F5E3;">
         <td>RX100</td>
         <td>Ravi</td>
      </tr>
      <tr style="background-color:#D2B4DE ;">
         <td>Continental</td>
         <td>Arjun</td>
      </tr>
   </table>
</body>
</html>

Align Table row Content

In the following example, we are going to align the <tr> tag to the center using the CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
         th,
         td {
            border: 1.5px solid #A569BD;
         }
   </style>
<body>
   <table style="width:100%">
      <tr>
         <th>Country</th>
         <th>Capital</th>
      </tr>
      <tr style="text-align:center">
         <td>Afghanistan</td>
         <td>Kabul</td>
      </tr>
      <tr style="text-align:center">
         <td>Albania</td>
         <td>Tirane</td>
      </tr>
   </table>
</body>
</html>

Spanning Multiple Rows and Columns

Following is an example where we are going to use the rowspan and colspan on the tr tag to manupulate the area of table row.

<!DOCTYPE html>
<html>
   <style>
      th,
      td {
         border: 1.5px solid #A569BD;
      }
      table {
         border-collapse: collapse;
      }
   </style>
<body>
   <table>
      <tr>
         <th rowspan=2>Name</th>
         <th colspan=2>Contact Details</th>
      </tr>
      <tr>
         <th>Mobile</th>
         <th>Landline</th>
      </tr>
      <tr>
         <td>Suresh</td>
         <td>0987654321</td>
         <td>123456</td>
      </tr>
      <tr>
         <td>Ramesh</td>
         <td>1256789543</td>
         <td>234562</td>
      </tr>
      <tr>
         <td>Kamal</td>
         <td>88976765432</td>
         <td>009875242</td>
      </tr>
   </table>
</body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
tr Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements