HTML tagForms

<optgroup>

A named group of options inside a `<select>` list.

Display
block
Closing tag
Required
Permitted content
Elements <option>
On this page

Example

#
HTML
<label for="course">Course</label>
<select id="course" name="course">
  <optgroup label="Frontend">
    <option value="html">HTML and CSS</option>
    <option value="js">JavaScript</option>
    <option value="react">React</option>
  </optgroup>
  <optgroup label="Backend">
    <option value="python">Python</option>
    <option value="node">Node.js</option>
  </optgroup>
</select>
Preview

Definition and usage

#

The <optgroup> element groups related options under a shared heading. You give the group’s name in the label attribute, and the browser displays it as a heading that can’t be selected.

Groups bring order to long lists: courses by technology, cities by state, office-hour slots by day of the week. Groups can’t be nested, and <optgroup> doesn’t work inside <datalist>.

A disabled attribute on the group blocks all of its options at once, which is more convenient than disabling each one separately.

Syntax

#
Syntax
<optgroup label="Group name">
  <option>…</option>
</optgroup>

Attributes

#
  • label

    text

    The group’s name, displayed above its options. Without it the group has no heading, so always provide it.
  • disabled

    boolean attribute

    Disables all the options in the group. They’re visible, but can’t be selected.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

label

text

The group’s name, displayed above its options. Without it the group has no heading, so always provide it.

HTML
<select name="hour" aria-label="Office hour">
  <optgroup label="Monday">
    <option>17:00</option>
    <option>18:00</option>
  </optgroup>
  <optgroup label="Wednesday">
    <option>16:00</option>
  </optgroup>
</select>
Preview

disabled

boolean attribute

Disables all the options in the group. They’re visible, but can’t be selected.

HTML
<select name="course" aria-label="Course">
  <optgroup label="Available now">
    <option>HTML and CSS</option>
  </optgroup>
  <optgroup label="Coming soon" disabled>
    <option>Svelte</option>
    <option>Rust</option>
  </optgroup>
</select>
Preview

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • The group’s name isn’t sent with the form. If the server needs that information, store it in the option’s value, for example value="frontend-react".
  • Don’t try to nest groups. When you need several levels, split the choice into two separate lists.

Related entries

#

Spotted a mistake or missing an example? Write to us.