HTML <optgroup> Tag

HTML <optgroup> tag contains <option> tags combined into one group. (The <option> tag defines items in a drop-down list defined by the <select> tag).

The content of the <optgroup> tag looks like a heading in bold. The elements that the <option> tag contains shift to the right.

Syntax

The <optgroup> tag comes in pairs; the closing (</optgroup>) tag is required.

The <option> tag is generally used with the value attribute, to specify which value is sent to the server for further processing.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <select>
      <optgroup label="Books">
        <option value="html">HTML</option>
        <option value="css">CSS</option>
      </optgroup>
      <optgroup label="Snippets">
        <option value="java">Java</option>
        <option value="linux">Linux</option>
        <option value="git">Git</option>
      </optgroup>
    </select>
  </body>
</html>

Result



Let's see an example of using the disabled attribute.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <select>
      <optgroup disabled="disabled" label="Quizzes">
        <option value="html">HTML</option>
        <option value="css">CSS</option>
        <option value="javascript">JavaScript</option>
        <option value="php">PHP</option>
      </optgroup>
      <optgroup label="Snippets">
        <option value="git">Git</option>
        <option value="angularjs">AngularJS</option>
      </optgroup>
    </select>
  </body>
</html>

Result



Attributes

Attribute Value Description
disabled disabled Indicates, that the selection of the group elements is disabled.
label text Defines a label for the group, which will be displayed in a drop-down list.
Required attribute.

The <optgroup> tag supports the Global attributes and the Event Attributes.

Browser support