html optgroup tag | optgroup in html #html #htmlcss

HTML OPTGROUP TAG

The <optgroup> tag is an HTML element used to group related <option> elements within a <select> element. It provides a way to organize and categorize options in a dropdown list. The <optgroup> tag can be used to create nested groups as well.



Here's the basic syntax for using the <optgroup> tag:

<select>
  <optgroup label="Group 1">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
  </optgroup>
  <optgroup label="Group 2">
    <option value="option4">Option 4</option>
    <option value="option5">Option 5</option>
    <option value="option6">Option 6</option>
  </optgroup>
</select>

In the example above, we have two groups defined within the <select> element. The label attribute is used to provide a label or name for each group. Within each <optgroup>, we have individual <option> elements that represent the selectable options.

When rendered in a browser, the options will be grouped together visually, providing a hierarchical structure to the dropdown list. The exact appearance may vary depending on the browser and operating system.

It's important to note that not all browsers support the visual grouping of options. In unsupported browsers, the options will still be displayed as a flat list without any grouping. Therefore, it's a good practice to ensure the options make sense even without visual grouping.

HTML OPTGROUP ATTRIBUTES


The <optgroup> element in HTML supports the following attributes:

label: This attribute specifies the label or name for the group. It provides a textual description for the group of options within the <optgroup>. For example:

<optgroup label="Group 1">
  <!-- options here -->
</optgroup>

disabled: When the disabled attribute is present, it disables the entire group of options, making them unselectable. This attribute is useful when you want to indicate that certain options are not currently available. Example:

<optgroup label="Group 2" disabled>
  <!-- options here -->
</optgroup>


Both the label and disabled attributes are optional. You can use either of them or both together in an <optgroup> element to define the desired behavior and appearance for the grouped options.

Comments

Popular posts from this blog

html audio tag | audio in html | how to add audio in html #html #htmlcss