HTML - scope Attribute



HTML 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.

Note:This attribute is used by screen readers but has no visible effect on the browser.

If the scope attribute is not defined, then browsers automatically select the group of cells to which the header cell applies.

Syntax

<th scope=" col | row | colgroup | rowgroup " >
  • 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.

Applies on

Below-listed element allows the use of the HTML scope attribute.

Element Description
<th> HTML <th> tag designates the header cell for HTML tables.

Examples of HTML scope attribute

Below examples will illustrate the HTML scope attribute, where and how we should use this attribute!

Using scope='col' and scope='row' in "th" tag

Output window displays a table and scope applied for the 'th' elements are highlighted.

<!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;
      }
      th[scope='col'] {
         background-color: #696969;
         color: #fff;
      }
      th[scope='row'] {
         background-color: #d7d9f2;
      }
   </style>
</head>

<body>
   <h3>HTML scope attribute in 'th'</h3>
   <table>
      <caption>Alien football stars</caption>
      <tr>
         <td>S.NO</td>
         <th scope="col">Player</th>
         <th scope="col">Gloobles</th>
         <th scope="col">Za'taak</th>
      </tr>
      <tr>
         <td>1.</td>
         <th scope="row">TR-7</th>
         <td>7</td>
         <td>4,569</td>
      </tr>
      <tr>
         <td>2.</td>
         <th scope="row">Khiresh Odo</th>
         <td>7</td>
         <td>7,223</td>
      </tr>
      <tr>
         <td>3.</td>
         <th scope="row">Mia Oolong</th>
         <td>9</td>
         <td>6,219</td>
      </tr>
   </table>
</body>

</html>

Using scope='colgroup' and scope='rowgroup' in "th" tag

Output window displays a table and scope applied for the 'th' elements are in bold.

<!DOCTYPE html>
<html>

<head>
   <title>HTML th scope Attribute</title>
</head>

<body>
   <h3>HTML scope attribute in 'th'</h3>
   <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>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
scope Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements