Th

Struct Th 

Source
pub struct Th;
Expand description

The <th> element - defines a header cell in a table.

§Purpose

The <th> element represents a header cell in a table. It labels a row or column of data cells, providing context for the information in the table. Header cells are typically rendered with bold, centered text by default and are crucial for table accessibility.

§Content Categories

  • None (only valid within <tr>)

§Permitted Content Model

  • Flow content (excluding header, footer, sectioning content, and heading content)

§Common Use Cases

  • Labeling table columns
  • Labeling table rows
  • Creating multi-level headers with rowspan/colspan
  • Providing context for data cells
  • Improving table accessibility for screen readers

§Key Attributes

  • scope: Specifies cells the header relates to (“row”, “col”, “rowgroup”, “colgroup”)
  • colspan: Number of columns the header spans
  • rowspan: Number of rows the header spans
  • headers: Space-separated list of other header cell IDs
  • abbr: Abbreviated description of the header

§Example

<!-- Column headers -->
<table>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Email</th>
      <th scope="col">Role</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alice</td>
      <td>alice@example.com</td>
      <td>Developer</td>
    </tr>
  </tbody>
</table>

<!-- Row headers -->
<table>
  <tbody>
    <tr>
      <th scope="row">Product A</th>
      <td>$99</td>
      <td>In Stock</td>
    </tr>
    <tr>
      <th scope="row">Product B</th>
      <td>$149</td>
      <td>Out of Stock</td>
    </tr>
  </tbody>
</table>

<!-- Multi-level headers -->
<table>
  <thead>
    <tr>
      <th rowspan="2" scope="col">Name</th>
      <th colspan="2" scope="colgroup">Scores</th>
    </tr>
    <tr>
      <th scope="col">Math</th>
      <th scope="col">Science</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Alice</th>
      <td>95</td>
      <td>92</td>
    </tr>
  </tbody>
</table>

<!-- Headers with abbreviations -->
<table>
  <thead>
    <tr>
      <th scope="col" abbr="Temp">Temperature (°F)</th>
      <th scope="col" abbr="Humid">Humidity (%)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>72</td>
      <td>65</td>
    </tr>
  </tbody>
</table>

§Accessibility

  • Always use scope attribute to clarify what the header labels
  • Use “col” for column headers, “row” for row headers
  • Use “colgroup” or “rowgroup” for headers spanning groups
  • Provide abbr for long header text to aid screen readers
  • Ensure header text is concise and descriptive

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Th

Source§

const TAG: &'static str = "th"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl<T: FlowContent> CanContain<T> for Th

Source§

impl CanContain<Text> for Th

Source§

impl CanContain<Th> for Tr

Auto Trait Implementations§

§

impl Freeze for Th

§

impl RefUnwindSafe for Th

§

impl Send for Th

§

impl Sync for Th

§

impl Unpin for Th

§

impl UnwindSafe for Th

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.