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.

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



Do you find this helpful?

Related articles