HTML - scope Attribute



The scope is an HTML attribute that is used to define the header cell. It is used for the header row, column, colgroup, or rowgroup.

The <th> element specifies a cell as the header of a group of table cells. The scope and headers attributes specify the nature of this group.

If the scope attribute is not defined or its value for row, col, rowgroup, or colgroup, then browsers automatically select the group of cells to which the header cell applies.

Syntax

Following is the syntax of the scope attribute −

<th scope=" col | row | colgroup | rowgroup " >

Attribute values

  • row − the header relates to all cells of the row it belongs to.
  • col − the header relates to all cells of the column it belongs to.
  • rowgroup − the header belongs to a rowgroup and relates to all of its cells.
  • colgroup − the header belongs to a colgroup and relates to all its cells.

Example

In the following example, let’s illustrate the usage of the scope attribute within the <th> tag, as follows −

<!DOCTYPE html>
<html>
<head>
   <title>HTML th scope Attribute</title>
   <style>
      td,
      th {
         border: 1px solid rgb(190, 190, 190);
         padding: 10px;
      }

      td {
         text-align: center;
      }

      tr:nth-child(even) {
         background-color: #eee;
      }

      th[scope='col'] {
         background-color: #696969;
         color: #fff;
      }

      th[scope='row'] {
         background-color: #d7d9f2;
      }

      caption {
         padding: 10px;
         caption-side: bottom;
      }

      table {
         border-collapse: collapse;
         border: 2px solid rgb(200, 200, 200);
         letter-spacing: 1px;
         font-family: sans-serif;
         font-size: 0.8rem;
      }
   </style>
</head>
<body>
   <h1 style="color: green;"> tutorials <span style="color: black">point</span>
   </h1>
   <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>
</body>
</html>

On running the above code, the output window will pop up displaying the table on the webpage.

Example

Considering the another scenario, where we are going to use the scope attribute with its value col.

<!DOCTYPE html>
<html>
<head>
   <title>HTML th scope Attribute</title>
</head>
<body>
   <h1 style="color: green;"> tutorials <span style="color: black">point</span>
   </h1>
   <table border="1" width="500">
      <tr>
         <th scope="col">Name</th>
         <th scope="col">Company</th>
         <th scope="col">Roles</th>
      </tr>
      <tr>
         <td>Aman</td>
         <td>tutorialspoint</td>
         <td>Technical content Engineer</td>
      </tr>
      <tr>
         <td>Vivek</td>
         <td>tutorialspoint</td>
         <td>Developer</td>
      </tr>
   </table>
</body>
</html>

When we run the above code, it will generate an output consisting of the text along with a table displayed on the webpage.

Example

Let's look into the following example, where we are going to use the two types of scope attribute one is of colgroup and another is rowgroup.

<!DOCTYPE html>
<html>
<head>
   <title>HTML th scope Attribute</title>
</head>
<body>
   <h1 style="color: green;"> tutorials <span style="color: black">point</span>
   </h1>
   <table border="1">
      <tr>
         <th scope="colgroup">Name</th>
         <th scope="colgroup">Company</th>
         <th scope="colgroup">Roles</th>
      </tr>
      <tr>
         <th scope="rowgroup">Aman</th>
         <td>tutorialspoint</td>
         <td>Technical content Engineer</td>
      </tr>
      <tr>
         <th scope="rowgroup">Vivek</th>
         <td>tutorialspoint</td>
         <td>Developer</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up displaying the table on the webpage.

html_attributes_reference.htm
Advertisements