HTML <optgroup> Tag Documentation
The <optgroup> tag groups related options inside a <select> dropdown. It helps organize long lists of options into logical categories. The label attribute provides the group heading that users see.
Structure and Attributes
- The
<optgroup>tag must be strictly inside a<select>element. - It contains one or more
<option>elements representing selectable items. - The
labelattribute is required and displays as a non-selectable group header. - Options inside the group are indented and grouped together visually by the browser.
Common Use Cases
Common use cases include:
- Geographical Data: Organizing countries by region.
- E-commerce: Grouping products by category.
- Food & Beverage: Separating menu items by type (drinks, main courses, desserts).
Optgroups heavily improve UX by making long dropdown menus more navigable and structured.
Best Practices
- Use
<optgroup>primarily to organize lists with more than 10 options. - Always include a descriptive
labelattribute. - Group related options based on the user's expected mental model.
