Legend

Struct Legend 

Source
pub struct Legend;
Expand description

The <legend> element - represents a caption for a <fieldset>.

§Purpose

The <legend> element provides a caption or title for its parent <fieldset> element. It describes the group of form controls contained within the fieldset. Must be the first child of the fieldset if present. Typically displayed as a title positioned on the border of the fieldset.

§Content Categories

  • None (only valid as first child of <fieldset>)

§Permitted Content Model

  • Phrasing content
  • Optionally intermixed with heading content

§Common Use Cases

  • Labeling form sections
  • Describing groups of radio buttons or checkboxes
  • Titling address or contact information groups
  • Naming configuration sections
  • Providing context for related form fields

§Key Attributes

  • Global attributes only

§Example

<!-- Basic legend -->
<fieldset>
  <legend>Personal Details</legend>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
</fieldset>

<!-- Legend for radio button group -->
<fieldset>
  <legend>Select your subscription plan:</legend>
  <input type="radio" id="basic" name="plan" value="basic">
  <label for="basic">Basic - $9.99/mo</label>
   
  <input type="radio" id="premium" name="plan" value="premium">
  <label for="premium">Premium - $19.99/mo</label>
</fieldset>

<!-- Legend with emphasis -->
<fieldset>
  <legend><strong>Required Information</strong></legend>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>
</fieldset>

<!-- Legend with additional context -->
<fieldset>
  <legend>
    Shipping Address
    <small>(where should we deliver your order?)</small>
  </legend>
  <label for="address">Address:</label>
  <input type="text" id="address" name="address">
</fieldset>

<!-- Legend for checkbox group -->
<fieldset>
  <legend>Select your interests:</legend>
  <input type="checkbox" id="sports" name="interests" value="sports">
  <label for="sports">Sports</label>
   
  <input type="checkbox" id="music" name="interests" value="music">
  <label for="music">Music</label>
   
  <input type="checkbox" id="reading" name="interests" value="reading">
  <label for="reading">Reading</label>
</fieldset>

<!-- Legend for payment information -->
<fieldset>
  <legend>Payment Information</legend>
  <label for="card">Card Number:</label>
  <input type="text" id="card" name="card-number">
   
  <label for="expiry">Expiry Date:</label>
  <input type="text" id="expiry" name="expiry">
</fieldset>

§Accessibility

  • Legends are announced by screen readers when users enter the fieldset
  • Provides important context for form field groups
  • Keep legend text concise and descriptive
  • Use heading elements within legend sparingly
  • Helps users understand the purpose of grouped fields

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Legend

Source§

const TAG: &'static str = "legend"

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<Legend> for Fieldset

Source§

impl<T: PhrasingContent> CanContain<T> for Legend

Source§

impl CanContain<Text> for Legend

Auto Trait Implementations§

§

impl Freeze for Legend

§

impl RefUnwindSafe for Legend

§

impl Send for Legend

§

impl Sync for Legend

§

impl Unpin for Legend

§

impl UnwindSafe for Legend

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.