totn HTML

HTML: <optgroup> tag

This HTML tutorial explains how to use the HTML element called the <optgroup> tag with syntax and examples.

Description

The HTML <optgroup> tag creates a group of options within a dropdown list of values in the <select> control. These dropdown values are defined by a series of <option> tags and grouped using the <optgroup> tag. The label value for the <optgroup> will appear as a gray heading above the grouped items within the <select> control. This tag is also commonly referred to as the <optgroup> element.

Syntax

In HTML, the syntax for the <optgroup> tag is:

<body>
  <label for="tutorial_choice">Tutorials: </label>

  <select id="tutorial_choice">
    <optgroup label="Web">
      <option value="html">HTML</option>
      <option value="css">CSS</option>
    </optgroup>

    <optgroup label="Database">
      <option value="sql">SQL</option>
      <option value="oracle">Oracle</option>
    </optgroup>
  </select>
</body>

Sample Output


Attributes

In addition to the Global Attributes, the following is a list of attributes that are specific to the <optgroup> tag:

Attribute Description HTML Compatibility
disabled Boolean value indicating that the user cannot interact with the <select> control. HTML 4.01, HTML5
label The heading that appears above the grouped options in the <select> control HTML 4.01, HTML5

Note

  • The HTML <optgroup> element is found within the <body> tag. The <optgroup> tag is usually found within the <select> tag.
  • The <optgroup> tag organizes a set of <option> tags into an option group with a group heading.
  • The label value for the <optgroup> will appear as a gray heading. All of the grouped items will appear below the heading as an indented list of options.
  • By default, the first option in the <select> tag will appear as the selected item. The user can select another option in the dropdown list.

Browser Compatibility

The <optgroup> tag has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • Edge Mobile
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the <optgroup> tag below, exploring examples of how to use the <optgroup> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.