HTML Table Headers: A Comprehensive Guide

0
3K

HTML table headers are used to define the columns or rows of a table. They are typically styled differently from data cells to visually distinguish them.

Basic Usage

HTML
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
  </tr>
</table>

In the example above, <th> tags are used to create the table headers.

Styling Headers

You can style table headers using CSS properties like:

  • font-weight: Sets the font weight (e.g., bold).
  • text-align: Sets the text alignment (e.g., center).
  • background-color: Sets the background color.
  • color: Sets the text color.

Example with Styling

HTML
<table>
  <tr>
    <th style="font-weight: bold; text-align: center; background-color: #f2f2f2;">Header 1</th>
    <th style="font-weight: bold; text-align: center; background-color: #f2f2f2;">Header 2</th>
    <th style="font-weight: bold; text-align: center; background-color: #f2f2f2;">Header 3</th>
  </tr>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
  </tr>
</table>

Header Scope

The scope attribute of the <th> element can be used to specify the scope of a header. This information is helpful for screen readers and assistive technologies.

  • col: Indicates that the header is related to a column.
  • row: Indicates that the header is related to a row.

Example with Scope

HTML
<table>
  <thead>
    <tr>
      <th scope="col">Header 1</th>
      <th scope="col">Header 2</th>
      <th scope="col">Header 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>   
    </tr>
  </tbody>
</table>

The <thead>, <tbody>, and <tfoot> Elements

  • <thead>: Contains the table header rows.
  • <tbody>: Contains the table body rows.
  • <tfoot>: Contains the table footer rows.

By using these elements and styling techniques, you can create visually appealing and informative table headers that enhance the readability and usability of your HTML tables.

Like
1
Site içinde arama yapın
Kategoriler
Read More
EĞİTİM BİLGİLERİ
Adapting to the Digital Age in Education
From Classrooms to Virtual Spaces: Adapting to the Digital Age in Education The landscape of...
By ALAGAI AUGUSTEN 2024-07-21 19:24:04 0 4K
EĞİTİM BİLGİLERİ
P.5 AND P.6 SCIENCE REVISION
P.5 AND P.6 SCIENCE REVISION
By Landus Mumbere Expedito 2024-08-04 10:33:50 0 3K
Technology
Workbook and Worksheet Basics
Workbook Think of a workbook as a digital binder. It's a container that holds one or more...
By Microsoft Excel 2024-07-26 04:35:59 0 3K
Politics
Ugandan Opposition Leader Bobi Wine Shot by Police During Thanksgiving Event
In a shocking turn of events today, Ugandan opposition leader Bobi Wine was shot in the leg...
By Mpatswe Francis 2024-09-03 19:50:37 1 6K