label

Associates a text caption with a form control. Labels are essential for accessibility, enabling screen readers to announce form fields and allowing users to click the label to focus the associated input.

When to Use

  • Every input, select, and textarea needs a label
  • Checkboxes and radio buttons should have labels wrapping them
  • Any form control that requires user identification

Always provide labels. Forms without labels are inaccessible to screen reader users.

VB Styling

Vanilla Breeze styles labels as display: block with font-weight: 500 and a small bottom margin (var(--size-xs)). When a label contains a checkbox or radio input, VB auto-detects this via :has() and switches to inline-flex with centered alignment, gap spacing, and a pointer cursor — no classes needed.

Association Methods

Explicit: Using the for Attribute

The recommended method. The label's for attribute matches the input's id.

Implicit: Wrapping the Input

The label wraps the input. Useful for checkboxes and radios where text follows the control.

With Checkboxes and Radios

For checkboxes and radio buttons, wrap the input inside the label. VB auto-detects the contained checkbox or radio via label:has(input[type="checkbox"]) and switches the label to inline-flex with proper alignment and a pointer cursor. No extra classes required.

Required Field Indicator

Indicate required fields visually. The asterisk is commonly used but should be explained at the top of the form.

Visually Hidden Labels

When a label is not visually needed (e.g., a search field with a placeholder), hide it visually but keep it accessible with the .visually-hidden utility class.

The .visually-hidden class hides the label from sight while keeping it announced by screen readers.

Accessibility Notes

  • Every input needs a label: No exceptions for accessibility
  • Use for attribute: Explicit association is clearer and more robust than wrapping
  • Labels are clickable: Clicking a label focuses its associated input
  • Don't use placeholder as label: Placeholders disappear when typing
  • One label per input: A single label cannot serve multiple inputs
  • aria-labelledby alternative: Can reference multiple elements for complex labels

CSS Reference

Related

  • input - Form controls that labels associate with
  • select - Dropdown menus that need labels
  • textarea - Multi-line inputs that need labels
  • fieldset - Groups of labeled controls
  • form-field - Enhanced form field wrapper