HTML Reference ยท Forms
The <optgroup> element
Reference for the HTML <optgroup> element: grouping the options of a select under a heading, the label and disabled attributes, accessibility notes and technical details.
The <optgroup> element groups related options inside a <select>. Its label attribute is shown as a heading above the group, and the heading itself can't be selected. Groups make long lists easier to scan: cities by country, products by category, time slots by day.
<label for="train">Train</label> <select id="train" name="train"> <optgroup label="Morning"> <option value="t0712">07:12</option> <option value="t0948">09:48</option> </optgroup> <optgroup label="Evening"> <option value="t1830">18:30</option> </optgroup> </select>
Try it
The disabled group's options are visible but can't be chosen. Only the option's value is submitted; the group label is not.
Attributes
| Attribute | Values | Description |
|---|---|---|
label |
Text | The group's heading. Required in classic selects. |
disabled |
Boolean | Disables every option in the group. |
It also accepts the global attributes.
Usage notes
- No nesting. An optgroup can't contain another optgroup; groups are one level deep. Use clear labels instead of a hierarchy.
- Only in
<select>. Groups are not supported inside a<datalist>. - Write meaningful labels. Screen readers announce the group label with the options, so "Brass" helps, "Group 2" doesn't.
- Group headings are not values. If users need to pick a whole category, add a real option for it.
- Limited styling. In a classic select the open list is drawn by the system, so group headings look different in every browser. A customizable select (newer, limited support) allows more control.
Technical summary
| Content categories | None |
| Permitted content | Zero or more <option> and script-supporting elements (customizable select also allows a <legend> as the first child) |
| Tag omission | The end tag can be omitted if the optgroup is immediately followed by another <optgroup> or an <hr>, or if there is no more content in the parent element |
| Permitted parents | A <select> element |
| Implicit ARIA role | group |
| DOM interface | HTMLOptGroupElement |
| Default display | block |
Browser support
Supported in every browser.
Related
<select>โ the list the group belongs to<option>โ each choice<fieldset>โ grouping other form controls- Lesson: Drop-downs and Suggestions