Label

Struct Label 

Source
pub struct Label;
Expand description

The <label> element - represents a caption for a form control.

§Purpose

The <label> element provides a text label for a form control, creating an explicit association between the label text and the control. Clicking the label activates the associated control, improving usability and accessibility. Essential for screen readers and assistive technologies.

§Content Categories

  • Flow Content
  • Phrasing Content
  • Interactive Content
  • Palpable Content

§Permitted Content Model

  • Phrasing content (excluding other <label> elements and labelable elements other than the labeled control)

§Common Use Cases

  • Labeling text inputs and textareas
  • Labeling checkboxes and radio buttons
  • Labeling select dropdowns
  • Improving form accessibility
  • Increasing click target area for form controls

§Key Attributes

  • for: ID of the form control this label is associated with

§Example

<!-- Label with for attribute -->
<label for="username">Username:</label>
<input type="text" id="username" name="username">

<!-- Label wrapping input -->
<label>
  Email:
  <input type="email" name="email">
</label>

<!-- Checkbox with label -->
<input type="checkbox" id="terms" name="terms">
<label for="terms">I agree to the terms and conditions</label>

<!-- Radio buttons with labels -->
<fieldset>
  <legend>Choose a size:</legend>
  <input type="radio" id="small" name="size" value="small">
  <label for="small">Small</label>
   
  <input type="radio" id="medium" name="size" value="medium">
  <label for="medium">Medium</label>
   
  <input type="radio" id="large" name="size" value="large">
  <label for="large">Large</label>
</fieldset>

<!-- Label with required indicator -->
<label for="email">
  Email Address <abbr title="required" aria-label="required">*</abbr>
</label>
<input type="email" id="email" name="email" required>

<!-- Wrapping label for checkbox -->
<label>
  <input type="checkbox" name="subscribe" value="yes">
  Subscribe to newsletter
</label>

§Accessibility

  • Always associate labels with form controls using for attribute or wrapping
  • Ensure label text is descriptive and concise
  • Don’t use placeholder text as a substitute for labels
  • Use aria-label or aria-labelledby when visual labels aren’t possible
  • Indicate required fields clearly
  • Avoid nesting interactive elements within labels

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Label

Source§

const TAG: &'static str = "label"

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: PhrasingContent> CanContain<T> for Label

Source§

impl CanContain<Text> for Label

Source§

impl FlowContent for Label

Source§

impl InteractiveContent for Label

Source§

impl PalpableContent for Label

Source§

impl PhrasingContent for Label

Auto Trait Implementations§

§

impl Freeze for Label

§

impl RefUnwindSafe for Label

§

impl Send for Label

§

impl Sync for Label

§

impl Unpin for Label

§

impl UnwindSafe for Label

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.