Back to Content

: The HTML Option element

files/en-us/web/html/reference/elements/option/index.md

latest5.4 KB
Original Source

The <option> HTML element is used to define an item contained in a {{HTMLElement("select")}}, an {{HTMLElement("optgroup")}}, or a {{HTMLElement("datalist")}} element. As such, <option> can represent menu items in popups and other lists of items in an HTML document.

{{InteractiveExample("HTML Demo: <option>", "tabbed-standard")}}

html
<label for="pet-select">Choose a pet:</label>

<select id="pet-select">
  <option value="">--Please choose an option--</option>
  <option value="dog">Dog</option>
  <option value="cat">Cat</option>
  <option value="hamster">Hamster</option>
  <option value="parrot">Parrot</option>
  <option value="spider">Spider</option>
  <option value="goldfish">Goldfish</option>
</select>
css
label {
  font-family: sans-serif;
  font-size: 1rem;
  padding-right: 10px;
}

select {
  font-size: 0.9rem;
  padding: 2px 5px;
}

Attributes

This element includes the global attributes.

  • disabled
    • : If this Boolean attribute is set, this option is not checkable. Often browsers grey out such control and it won't receive any browsing event, like mouse clicks or focus-related ones. If this attribute is not set, the element can still be disabled if one of its ancestors is a disabled {{HTMLElement("optgroup")}} element.
  • label
    • : This attribute is text for the label indicating the meaning of the option. If the label attribute isn't defined, its value is that of the element text content.
  • selected
    • : If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a {{HTMLElement("select")}} element whose multiple attribute is not set, only one single <option> of this {{HTMLElement("select")}} element may have the selected attribute.
  • value
    • : The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.

Styling with CSS

Styling <option> elements has historically been highly limited. Customizable select elements explains newer features that enable their full customization, just like any regular DOM element.

Legacy option styling

In browsers that don't support the modern customization features (or legacy codebases where they can't be used), the styling available on <option> elements depends on the browser and operating system. Depending on the operating system, the {{cssxref("font-size")}} of the owning <select> is respected in Firefox and Chromium. Chromium may additionally allow {{cssxref("color")}}, {{cssxref("background-color")}}, {{cssxref("font-family")}}, {{cssxref("font-variant")}}, and {{cssxref("text-align")}} to be set.

You can find more details about legacy <option> styling in our guide to advanced form styling.

Examples

See {{HTMLElement("select")}} for examples.

Technical summary

<table class="properties"> <tbody> <tr> <th scope="row"> <a href="/en-US/docs/Web/HTML/Guides/Content_categories" >Content categories</a > </th> <td>None.</td> </tr> <tr> <th scope="row">Permitted content</th> <td> In traditional <code>&lt;select&gt;</code> elements, only text content is permitted, possibly with escaped characters (like <code>&#x26;eacute;</code>). In <a href="/en-US/docs/Learn_web_development/Extensions/Forms/Customizable_select">customizable select elements</a>, <code>&lt;option&gt;</code> elements can have any arbitrary content. </td> </tr> <tr> <th scope="row">Tag omission</th> <td> The start tag is mandatory. The end tag is optional if this element is immediately followed by another <code>&#x3C;option></code> element or an {{HTMLElement("optgroup")}}, or if the parent element has no more content. </td> </tr> <tr> <th scope="row">Permitted parents</th> <td> A {{HTMLElement("select")}}, an {{HTMLElement("optgroup")}} or a {{HTMLElement("datalist")}} element. </td> </tr> <tr> <th scope="row">Implicit ARIA role</th> <td><a href="/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/option_role"><code>option</code></a></td> </tr> <tr> <th scope="row">Permitted ARIA roles</th> <td>No <code>role</code> permitted</td> </tr> <tr> <th scope="row">DOM interface</th> <td>{{domxref("HTMLOptionElement")}}</td> </tr> </tbody> </table>

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • Other form-related elements: {{HTMLElement("form")}}, {{HTMLElement("legend")}}, {{HTMLElement("label")}}, {{HTMLElement("button")}}, {{HTMLElement("select")}}, {{HTMLElement("datalist")}}, {{HTMLElement("optgroup")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("textarea")}}, {{HTMLElement("input")}}, {{HTMLElement("output")}}, {{HTMLElement("progress")}} and {{HTMLElement("meter")}}.
  • Customizable select elements