HTML Optgroup
In this page:
<select name="name">
<optgroup label="Group label">
<option value="value">Label</option>
</optgroup>
</select>
Grouping Options with optgroup
optgroup wraps a set of related <option> elements inside a <select> dropdown, and its label attribute supplies a bold heading rendered above that group of options, visually separating it from other groups in the same dropdown without changing how the dropdown behaves.
- Reach for optgroup once a select dropdown grows past roughly 10-15 options, since that is typically when unlabeled scanning starts to feel tedious.
- Accepted attributes:
- label — required text shown for the group
- disabled — boolean
Example: Grouping Options with optgroup
<select>
<optgroup label="Fruits">
<option>Apple</option>
<option>Banana</option>
</optgroup>
</select>
Disabling an Entire Group
Adding the disabled attribute to an optgroup grays out and disables every option inside that group at once, which is useful for temporarily hiding an entire category (like out-of-stock product types) without needing to disable each individual option one by one.
- Use a disabled optgroup to gray out a whole unavailable category at once instead of disabling each option inside it individually.
- Accepted values:
- disabled — boolean attribute on optgroup
- Disables all contained options
Example: Disabling an Entire Group
<select>
<optgroup label="Out of Stock" disabled>
<option>Grapes</option>
</optgroup>
</select>
Pre-selecting an Option Within a Group
The selected attribute still works normally on an <option> nested inside an optgroup, exactly as it would on an ungrouped option -- grouping options does not change how default selection, form submission, or the option's value attribute behave.
- Test that your pre-selected default still makes sense visually once options are grouped -- a selected option buried in the third group can be easy for users to overlook.
- Accepted values:
- selected — boolean attribute on option
- Only one selected option in a single-select
Example: Pre-selecting an Option Within a Group
<select>
<optgroup label="Sizes">
<option>Small</option>
<option selected>Medium</option>
</optgroup>
</select>
Multiple Groups in One Dropdown
A single select can contain any number of optgroup elements one after another, and can even mix grouped and ungrouped options -- options placed directly inside the select without a wrapping optgroup simply appear without a heading, alongside the labeled groups.
Example: Multiple Groups in One Dropdown
<select>
<optgroup label="Fruits">
<option>Apple</option>
</optgroup>
<optgroup label="Vegetables">
<option>Carrot</option>
</optgroup>
</select>
Styling Optgroup Labels
Browsers apply a default bold style to the optgroup label automatically, and while dropdown internals are notoriously hard to style with CSS across browsers, the label text itself can usually be customized with basic font-weight and color properties targeting the optgroup selector, though rendering still varies by operating system.
Example: Styling Optgroup Labels
<style>
optgroup {
font-weight: bold;
color: navy;
}
</style>
<select>
<optgroup label="Fruits">
<option>Apple</option>
</optgroup>
</select>
- Nesting an optgroup inside another optgroup, which is not supported -- optgroup groups can only be one level deep.
- Forgetting the label attribute on optgroup, which leaves the group heading blank and defeats the purpose of grouping at all.
- Trying to make the optgroup label itself selectable -- it is a heading only, never a selectable option in the dropdown.
- optgroup wraps a set of related option elements inside a select dropdown, adding a bold, non-selectable label above them.
- The label attribute on optgroup is required and is what actually displays as the group heading.
- optgroup is purely visual organization -- it does not change the name/value pair submitted when a user picks an option inside it.
optgroup has been supported in every browser since the earliest days of HTML forms.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: