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
forattribute or wrapping - Ensure label text is descriptive and concise
- Don’t use placeholder text as a substitute for labels
- Use
aria-labeloraria-labelledbywhen visual labels aren’t possible - Indicate required fields clearly
- Avoid nesting interactive elements within labels
§WHATWG Specification
Trait Implementations§
Source§impl HtmlElement for Label
impl HtmlElement for Label
impl<T: PhrasingContent> CanContain<T> for Label
impl CanContain<Text> for Label
impl FlowContent for Label
impl InteractiveContent for Label
impl PalpableContent for Label
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more