Definition and Usage
The <select> tag represents a control for selecting options from a set of available options.
Depending on its attributes, it is displayed as either a list box or a drop-down box.
Basic Example
The example below shows one of the common uses of the <select> tag.
<label for="user-color">Select a Color</label>
<select id="user-color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
<option> and <optgroup> Tags
Each option of the <select> tag is represented by an <option> tag.
The <option> tags can be grouped using the <optgroup> tag.
Let's take a look at the <option> and <optgroup> tags that make up the <select> tag and their related attributes.
<option> Tag
The <option> tags within the <select> tag each represent options.
This control, represented by the <select> tag, provides a way for users to select the option they want. Users can select the desired option using this control.
Only text or escaped characters (meaning HTML entities) are valid inside the <option> tag.
If HTML tags are included, such as <a> or <span>, the browser ignores the functionality and styling associated with those tags and uses the text or escaped characters inside them as valid content.
value Attribute
Each <option> should have a value attribute that contains the data value to be submitted to the server when the option is selected. If this attribute is omitted, the value is taken from the text content of the option element.
<form>
<label for="user-color">Select a Color</label>
<select id="user-color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
</form>
selected Attribute
You can include the selected attribute in an <option> to have it selected by default when the page first loads.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-color">Select a Color</label>
<select id="user-color">
<option value="red">Red</option>
<option value="blue" selected>Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
disabled Attribute
The disabled attribute, also known as a control disabling attribute, prevents the option from receiving focus, and its value is not submitted when the form is submitted. When the <select> is enabled and a drop-down list is displayed for the control, most browsers gray out options with this attribute. Users also cannot select this option.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-color">Select a Color</label>
<select id="user-color">
<option value="red">Red</option>
<option value="blue" disabled>Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
In the example above, the "Blue" option cannot be selected at all because it includes the disabled attribute.
<optgroup> Tag
The <optgroup> tag represents a group of <option> tags with a common label (name) within the <select> tag.
By grouping related <option> tags with this tag, you can provide users with a more organized list of options.
label Attribute
The label attribute specifies the label (name) of the option group for the <optgroup>. It is required.
label Attribute Syntax
<optgroup label="text">
<!-- text: Specifies the name of the option group. -->
<optgroup> Usage Example
<form>
<label for="fruits" style="display: block;">Select a Fruit</label>
<select id="fruits">
<optgroup label="Citrus Fruits">
<option value="orange">Orange</option>
<option value="lemon">Lemon</option>
<option value="lime">Lime</option>
</optgroup>
<optgroup label="Berries">
<option value="strawberry">Strawberry</option>
<option value="blueberry">Blueberry</option>
<option value="raspberry">Raspberry</option>
</optgroup>
<optgroup label="Tropical Fruits">
<option value="mango">Mango</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</optgroup>
</select>
<button type="submit">Submit</button>
</form>
Using Related Attributes
Let's learn about the attributes related to the <select> tag.
required Attribute
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<label for="user-color" style="display: block;">Select a Color (*required)</label>
<select id="user-color" required> <!-- Required selection field -->
<option value="" disabled selected>--Select Your Favorite Color--</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
<button type="submit">Submit</button>
</form>
Code Explanation
<option value="" disabled selected>--Select Your Favorite Color--</option> implements a placeholder. For more information, see Implementing a Placeholder for the <select> Tag.
You can see that the browser displays an error message indicating that the field is required.
autofocus Attribute
You can specify that the control should receive input focus when the page loads. It should be used on only one control element in an HTML document. If used more than once, only the first autofocus attribute used is applied. This attribute is used to automatically set focus on the control element.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-color">Select a Color</label>
<select id="user-color" autofocus>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
Even if the autofocus attribute is specified, the element with the autofocus attribute loses focus (is blurred) when the user focuses on another element.
Browser Compatibility Warning!
Due to Apple's policies, the autofocus attribute is not supported in any browser on iOS.
See caniuse.com for more detailed information.
multiple Attribute
The multiple attribute is used with the <select> tag to allow multiple options to be selected at the same time. When this attribute is used, the <select> tag is displayed as a list box, allowing multiple options to be selected.
To select multiple options, the user must hold down the Ctrl (Windows) or Command (Mac) key while selecting.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-color">Select a Color</label>
<select id="user-color" multiple>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
size Attribute
The size attribute specifies the number of visible rows in the list when the <select> control is active and displaying a drop-down list. The value is specified as a positive integer greater than 0.
Browser Compatibility Warning!
Some browsers do not support the size attribute on mobile devices. You should consider browser compatibility when using this attribute. See Browser Compatibility.
<label for="user-color">Select a Color</label>
<select id="user-color" size="3">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
<option value="orange">Orange</option>
</select>
disabled Attribute
The disabled attribute, also known as a control disabling attribute, prevents the control from receiving focus, and its value is not submitted when the form is submitted.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-color">Select a Color</label>
<select id="user-color" disabled>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
<label for="user-color">Select a Color</label>
<select id="user-color" name="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
form Attribute
By associating the element with a specific <form>, it becomes an element belonging to the associated <form>.
In other words, regardless of its physical location, it works like a <select> inside the associated <form>. When the associated form is submitted, the name and the value of the selected <option> are submitted together.
Implementing a Placeholder for the <select> Tag
The <select> tag does not support the placeholder attribute used in elements such as <input>.
However, you can achieve a similar effect.
Let's implement a placeholder by distinguishing between cases where an option is selected using the selected attribute and where no option is selected. We will also look at accessibility considerations.
When an Option Is Selected Using the selected Attribute
<label for="user-color" style="display: block;">Select a Color</label>
<select id="user-color" required>
<option value="" disabled>Select Your Favorite Color</option> <!-- Implementing a placeholder -->
<option value="red">Red</option>
<option value="blue" selected>Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
When No Option Is Selected
<label for="user-color" style="display: block;">Select a Color</label>
<select id="user-color" required>
<option value="" disabled selected>Select Your Favorite Color</option> <!-- Implementing a placeholder -->
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
Accessibility Considerations
The placeholder attribute cannot replace the label of the control.
placeholder attribute cannot replace the label of the control.
<form>
<select id="user-color" required>
<option value="" disabled selected>Select Your Favorite Color</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
</select>
<button type="submit">Submit</button>
</form>
For more information, see Accessibility Considerations for the <input> Tag.
Technical Summary
| Permitted parents | Any element that accepts phrasing content. |
|---|---|
| Permitted content |
|
Implicit ARIA role |
|
Permitted ARIA roles |
|
Note!
The specification includes <hr> and <div> as permitted content, but major browsers provide only partial or no support for some of their functionality. Therefore, they are not included here due to browser compatibility considerations.
Browser Compatibility
| Desktop | ||||
|---|---|---|---|---|
| Tag |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
<select>
|
1 | 12 | 1 | 1 |
<option>
|
1 | 12 | 1 | 4 |
<optgroup>
|
1 | 12 | 1 | 4 |
name
|
1 | 12 | 1 | 3 |
disabled
|
1 | 12 | 1 | 4 |
label
|
1 | 12 | 1 | 4 |
selected
|
1 | 12 | 1 | 4 |
required
|
4 | 12 | 4 | 5 |
multiple
|
1 | 12 | 1 | 3 |
size
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
<select>
|
HTML Standard #the-select-element |