optgroup
Groups related options within a select element under a visible label. Helps users scan long option lists organized into logical categories.
When to Use
- Organizing options into logical categories (countries by continent, products by type)
- Long select lists with distinct groupings
- Making it easier to scan and locate options in a large list
Examples
The shared select demo below includes an optgroup example showing countries grouped by region.
Basic Usage
The required label attribute provides the group heading displayed in bold above the options.
Basic optgroup usage
<select id="category"> <option value="">Select a category...</option> <optgroup label="Fruits"> <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="orange">Orange</option> </optgroup> <optgroup label="Vegetables"> <option value="carrot">Carrot</option> <option value="broccoli">Broccoli</option> <option value="spinach">Spinach</option> </optgroup></select>
Attributes
| Attribute | Purpose | Required |
|---|---|---|
label |
The group heading text displayed above the options | Yes |
disabled |
Disables all options in the group | No |
Disabled Optgroup
Disabling an optgroup makes every option within it unselectable. This is useful for showing choices that exist but are not currently available.
Disabled optgroup
<select> <optgroup label="Standard Shipping"> <option value="ground">Ground (5-7 days) - Free</option> <option value="priority">Priority (3-5 days) - $9.99</option> </optgroup> <optgroup label="Express Shipping" disabled> <option value="express">Express (2 days) - $19.99</option> <option value="overnight">Overnight - $34.99</option> </optgroup></select>
Limitations
- No nesting: You cannot nest an optgroup inside another optgroup. HTML does not support it
- Limited styling: Browser and OS rendering controls most of the appearance. VB sets
font-weight: 600on the group label but cannot change backgrounds, colors, or add icons - Options only: An optgroup can only contain option elements
- Label is required: The
labelattribute must be provided; there is no fallback text - Not in datalist: Optgroups are not supported inside datalist
For fully custom grouped dropdowns with arbitrary styling, consider the combo-box web component.
Accessibility
- Screen reader support: Groups are announced as categories, helping users understand the structure
- Keyboard navigation: Arrow keys move through all options across groups; groups act as visual separators
- Meaningful labels: The
labelattribute should clearly describe what the group contains - Disabled groups: All options within a disabled group are announced as unavailable
CSS Reference
Optgroup CSS styling
optgroup { font-weight: 600;} /* Note: optgroup styling is largely controlled by the browser/OS. Most browsers display the label in bold by default. VB reinforces this with font-weight: 600. */