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
impl HtmlElement for Legend
impl CanContain<Legend> for Fieldset
impl<T: PhrasingContent> CanContain<T> for Legend
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> 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