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.

The <optgroup> tag is considered to be a form element.

Syntax

The <optgroup> tag comes in pairs. The content is written between the opening (<optgroup>) and closing (</optgroup>) tags.

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

Example of the HTML <optgroup> tag:

<!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



Example of the HTML <optgroup> tag with the disabled attribute:

<!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.

How to style <optgroup> tag?

Common properties to alter the visual weight/emphasis/size of text in <optgroup> tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <optgroup> tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for <optgroup> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <optgroup> tag:

Browser support

chrome edge firefox safari opera

Practice Your Knowledge

What is the function of the HTML 'optgroup' tag?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?