HTML Table Headers: A Comprehensive Guide

0
13كيلو بايت

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
البحث
الأقسام
إقرأ المزيد
Technology
The VLOOKUP function in Excel
The VLOOKUP function in Excel is used to search for a value in the first column of a table and...
بواسطة Microsoft Excel Tips 2024-10-16 00:58:30 0 18كيلو بايت
Technology
SUMIFS Function
The SUMIFS function in Excel is used to sum a range of values based on multiple criteria. It's...
بواسطة Microsoft Excel Tips 2024-10-16 01:05:32 0 17كيلو بايت
Computer Programming
While Loop and For Loop
In Python, while and for loops are fundamental constructs for repeated execution of code blocks....
بواسطة Python for Everybody - Full University Python Course Code 2024-07-16 21:50:42 0 10كيلو بايت
التعليم
UNEB REPORT ON UACE CANDIDATES' WORK 2023
UNEB REPORT ON UACE CANDIDATES' WORK 2023
بواسطة Landus Mumbere Expedito 2024-08-10 13:26:00 0 12كيلو بايت
التعليم
Find scholarships and study in the United States
There are countless scholarships available in the United States, offered by various institutions,...
بواسطة Mpatswe Francis 2024-08-31 18:45:10 5 12كيلو بايت
Talksphare https://talksphare.com