HTML <optgroup> Tag

The HTML <optgroup> tag is used to group related options in a drop-down list, making it easier for users to navigate and select from a set of related choices. By organizing options into categories, the <optgroup> tag enhances the readability and usability of long or complex lists. This tag is commonly used within a <select> element and helps create a more intuitive user interface, particularly for forms with numerous options.

Syntax of the <optgroup> Tag

The syntax of the <optgroup> tag is simple and must always include a label attribute, which defines the name of the option group. The tag is placed inside a <select> element, wrapping a series of <option> tags.

html
<select>
    <optgroup label="Group Name">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
    </optgroup>
</select>

In this syntax:

  • <optgroup>: Represents a group of options in a drop-down list.
  • label: Defines the name of the option group that will be visible to users.
  • <option>: Contains the selectable options within the group.

Attributes of the <optgroup> Tag

The <optgroup> tag supports a couple of important attributes that enhance its functionality:

AttributeDescription
labelDefines the name of the group. This is required and visible to users in the drop-down.
disabledIf set, disables the entire group of options, preventing them from being selected.

Attribute Usage:

  • label: The label attribute is mandatory and represents the name of the group. For example, if you are grouping car brands, you might use <outgroup label ="German Cars">.
  • disabled: This attribute, when applied, disables the entire group, making all options within that group unselectable.

Examples of HTML <optgroup> Tag

Example 1: Grouping Options in a Drop-Down List

html
<!DOCTYPE html>
<html>
<head>
    <title>Option Group Example</title>
</head>
<body>
    <form>
        <label for="cars">Choose a car brand:</label>
        <select id="cars" name="cars">
            <optgroup label="German Cars">
                <option value="bmw">BMW</option>
                <option value="audi">Audi</option>
            </optgroup>
            <optgroup label="Japanese Cars">
                <option value="toyota">Toyota</option>
                <option value="honda">Honda</option>
            </optgroup>
        </select>
    </form>
</body>
</html>

In this example, two groups, "German Cars" and "Japanese Cars," are created using the <optgroup> tag. The user can select a car brand from these grouped options.

Example 2: Disabled Option Group

html
<!DOCTYPE html>
<html>
<head>
    <title>Disabled Option Group</title>
</head>
<body>
    <form>
        <label for="fruits">Choose a fruit:</label>
        <select id="fruits" name="fruits">
            <optgroup label="Citrus Fruits">
                <option value="orange">Orange</option>
                <option value="lemon">Lemon</option>
            </optgroup>
            <optgroup label="Berries" disabled>
                <option value="strawberry">Strawberry</option>
                <option value="blueberry">Blueberry</option>
            </optgroup>
        </select>
    </form>
</body>
</html>

In this example, the "Berries" group is disabled using the disabled attribute, making it unselectable in the drop-down list.

FAQs About HTML<optgroup>Tag

Q1: What is the purpose of the HTML <optgroup> tag?
A: The <optgroup> tag is used to group related options within a drop-down list, making it easier for users to understand and select from categorized options.

Q2: Is the label attribute mandatory for the <optgroup> tag?
A: Yes, the label attribute is mandatory and specifies the name of the group that will be visible in the drop-down list.

Q3: Can I disable individual options within an <outgroup>?
A: Yes, individual <option> elements within an <optgroup> can be disabled, or the entire group can be disabled using the disabled attribute in the <optgroup> tag.

Q4: Does the <optgroup> tag improve accessibility?
A: Yes, by logically grouping options, the <optgroup> tag improves the usability and accessibility of forms, making it easier for users, including those with disabilities, to navigate the options.

Q5: Is the <optgroup> tag supported in all browsers?
A: Yes, the <optgroup> tag is supported by all major browsers, including Chrome, Firefox, Safari, and Edge.

tools

HTML

Related Articles