| by Arround The Web | No comments

HTML tr tag

There are different ways to represent data and information, such as charts, graphs, pictographs, or tabular forms. The table organizes data in rows and columns, making the data more readable and clear. The tabulated data also saves time and space. To make tables in HTML, we use <table> tag with <tr> tag, <th> tag for heading, and <td> for table data.

The learning outcomes of this article will be:

What is HTML <tr> Tag?

The HTML <tr> tag specifies a row in a table, the <th> tag is used for table heading, and <td> tag is for table data.

Syntax

<tr>... </tr>

The HTML tr tag has a starting tag <tr> and a closing tag </tr>.

How to Create a Table in HTML?

The below example will demonstrate how we can create a table in HTML. To do so, first, add a <table> tag and then specify a caption using the <caption> tag:

<table>
<caption>Students Information</caption>
</table>

Within the table element, add <tr> tag after the caption. The first row will contain the heading of each column. For this <th> tags are used:

<tr>
    <th>Roll no</th>
    <th>Student name</th>
    <th>DOB</th>
</tr>

After creating three columns, let’s add data to the columns. The data will be added to each row in correspondence to the headings in the next row. So, to add data, the <td> tag is utilized:

<tr>
    <td>1</td>
    <td>John</td>
    <td>23-05-1997</td>
</tr>

Similarly, create two more rows as shown in the code snippet below:

<tr>
     <td>2</td>
     <td>William</td>
     <td>02-9-1995</td>
</tr>
<tr>
      <td>3</td>
      <td>Daisy</td>
      <td>12-03-1998</td>
</tr>

The corresponding output of the code mentioned above will look like this:

Applying Styles to Table with CSS

In this section, we will apply some styling properties to the table we have created above.

Style caption Tag

caption {
      font-size: 30px;
      padding: 7px;
      text-decoration: 2px underline #182747;
}

The caption tag used in the HTML file will have the properties listed below:

  • font-size” property sets the size of the font.
  • padding” property is utilized to apply space around the element’s content.
  • text-decoration” property applies decoration to the text.

Style <table>, <th>, <td> Tags

table, th, td {
    background-color: #D8D9CF;
    border: 1px solid #5d6462;
    font-size: 25px;
}

The <table>, <th>, and <td> tags are styled with the CSS properties described below:

  • background-color” property is utilized to give background color to the table.
  • border” property sets a border around the table having a width, line type, and color.
  • font-size” property is utilized to set the size of the font as 25px.

Style table Element

table {
       margin: auto;
       width: 60%;
       height: 150px;
}

Here:

  • margin” property with value “auto” places the table in the center spacing equal from each side.
  • width” property is utilized to set the table’s width.
  • height” property sets the height of the table.

Output

What are the Attributes of <tr> Tag?

There are many attributes associated with the HTML <tr> tag, such as:

  • rowspan
  • colspan

Attribute: rowspan

This attribute contains a non-negative value that specifies the number of rows the cell extends.
Its default value is 1.

Example: How to Add rowspan Attribute to a table row?
So, from the above example, we have taken the second row. Add the attribute rowspan with value 2 to the third table data:

<tr>
    <td>2</td>
    <td>William</td>
    <td rowspan="2">02-9-1995</td>
</tr>

It can be seen from the below image the second row’s third data has taken space of two rows:

Attribute: colspan

This attribute contains a non-negative value that specifies the number of columns the cell extends. The default value of the colspan is 1. Greater than 1000 values will be set as default because they are considered incorrect.

Example: How to Add Colspan Attribute to Table Row?
In the HTML file, the <th> tag is added below the <caption> tag and it is provided with the attribute colspan with a value of 3:

 <th colspan="3">Linuxhint School</th>

From the below image, it can be seen that the table header has taken space for three columns:

How to Create Vertical Header Table?

In HTML, add three <tr> tags. Inside each, other tags are placed, such as <th>, then <td> tags. In such a way, a table with vertical headings can be created:

<tr>
    <th>Name</th>
    <td>Tom</td>
    <td>Andrew</td>
</tr>
<tr>
    <th>City</th>
    <td>United States</td>
    <td>China</td>
</tr>
<tr>
    <th>Profession</th>
    <td>Instructor</td>
    <td>Doctor</td>
</tr>

Save the HTML file and open the file in a web browser:

It can be observed that the header of the table is placed vertically.

Conclusion

To create a table for our web application, HTML provides us with different tags for tables, rows, and columns as <table>, <tr>, and <td>, respectively. So, this blog has explained the HTML <tr> tag, its attribute, and the method to create a simple and vertical header table.

Share Button

Source: linuxhint.com

Leave a Reply