Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

optgroup — grouping options in select

The `optgroup` element combines `option` elements inside `select` into a named group. It organizes a list and lets you disable an entire group.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<select><optgroup label="Group"><option value="a">A</option></optgroup></select>

optgroup represents a group of option elements with a common label inside select. The user selects an option, not the group. Do not nest optgroup groups inside one another.

The label attribute supplies the group name and is required when the group has no legend child. The current standard permits an optional legend before the remaining content; when it is the first element child, its text takes precedence over label when determining the group name. For simple lists, use label and option children.

The boolean disabled attribute disables the group's options together. Its presence matters: disabled="false" also disables the group; remove the attribute to enable it. A group label does not replace the label of the select control.

In the example, selected explicitly sets the initial choice to HTML, while the Unavailable group prevents choosing JavaScript. The list's appearance depends on the browser and operating system; verify behavior rather than exact appearance.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Option groups</title>
<label for="course">Course</label>
<select id="course" name="course">
  <optgroup label="Available">
    <option value="html" selected>HTML</option>
    <option value="css">CSS</option>
  </optgroup>
  <optgroup label="Unavailable" disabled>
    <option value="js">JavaScript</option>
  </optgroup>
</select>
</html>

Result

Initially selected: HTML (value: html). Selectable options: HTML, CSS. Disabled option: JavaScript.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources