Table

Struct Table 

Source
pub struct Table;
Expand description

The <table> element - represents tabular data in rows and columns.

§Purpose

The <table> element represents data with more than one dimension in the form of a table. Tables should be used for tabular data, not for layout purposes (use CSS for layout). Provides semantic structure for organizing related information in rows and columns.

§Content Categories

  • Flow Content
  • Palpable Content

§Permitted Content Model

  • Optional <caption> element
  • Zero or more <colgroup> elements
  • Optional <thead> element
  • Either: zero or more <tbody> elements, or one or more <tr> elements
  • Optional <tfoot> element

§Common Use Cases

  • Displaying datasets and spreadsheet-like data
  • Pricing tables and comparison charts
  • Financial reports and statistics
  • Schedules and calendars
  • Product specifications and feature comparisons

§Key Attributes

  • Global attributes only (older attributes like border are obsolete)

§Example

<!-- Basic table -->
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>City</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alice</td>
      <td>30</td>
      <td>New York</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td>25</td>
      <td>Los Angeles</td>
    </tr>
  </tbody>
</table>

<!-- Table with caption and footer -->
<table>
  <caption>Quarterly Sales Report</caption>
  <thead>
    <tr>
      <th>Quarter</th>
      <th>Revenue</th>
      <th>Growth</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Q1</td>
      <td>$100,000</td>
      <td>5%</td>
    </tr>
    <tr>
      <td>Q2</td>
      <td>$120,000</td>
      <td>20%</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>$220,000</td>
      <td>12.5%</td>
    </tr>
  </tfoot>
</table>

<!-- Complex table with column groups -->
<table>
  <colgroup>
    <col>
    <col span="2" class="financial">
  </colgroup>
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Stock</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Widget</td>
      <td>$10</td>
      <td>50</td>
    </tr>
  </tbody>
</table>

§Accessibility

  • Use <th> elements for headers with scope attribute
  • Provide a <caption> for table context
  • Use headers attribute for complex tables
  • Ensure proper header association for screen readers
  • Consider using aria-describedby for additional context
  • Make tables responsive for mobile devices

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Table

Source§

const TAG: &'static str = "table"

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 CanContain<Caption> for Table

Source§

impl CanContain<Colgroup> for Table

Source§

impl CanContain<Script> for Table

Source§

impl CanContain<Tbody> for Table

Source§

impl CanContain<Template> for Table

Source§

impl CanContain<Tfoot> for Table

Source§

impl CanContain<Thead> for Table

Source§

impl CanContain<Tr> for Table

Source§

impl FlowContent for Table

Source§

impl PalpableContent for Table

Auto Trait Implementations§

§

impl Freeze for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnwindSafe for Table

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.