<optgroup>
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
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
HTML CSS
Java Linux Git
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
Birmingham Cambridge
Ullswater Thirlmere
Attributes
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:
CSS text-shadow property adds shadow to text. CSS text-align-last property sets the alignment of the last line of the text. CSS line-height property specifies the height of a line. CSS letter-spacing property defines the spaces between letters/characters in a text. CSS word-spacing property sets the spacing between words.