The <select>
represents a control that provides a menu of options:
The above example shows typical <select>
usage. It is given an id
attribute to enable it to be associated with a <label>
for accessibility purposes, as well as a name
attribute to represent the name of the associated data point submitted to the server. Each menu option is defined by an <option>
element nested inside the <select>
.
Each <option>
element should have a value
attribute containing the data value to submit to the server when that option is selected; if no value
attribute is included, the value defaults to the text contained inside the element. You can include a selected
attribute on an <option>
element to make it selected by default when the page first loads.
The <select>
element has some unique attributes you can use to control it, such as multiple
to specify whether multiple options can be selected, and size to specify how many options should be shown at one. It also accepts most of the general form input attributes such as required
, disabled
, autofocus
, etc.
You can further nest <option>
elements inside <optgroup>
elements to create separate groups of options inside the dropdown.
For further examples, see The native form widgets: Drop-down content.
This element includes the global attributes.
autocomplete
DOMString
providing a hint for a user agent's autocomplete feature. See The HTML autocomplete attribute for a complete list of values and details on how to use autocomplete.autofocus
autofocus
attribute.disabled
fieldset
; if there is no containing element with the disabled
attribute set, then the control is enabled.form
id
of a form element in the same document. This enables you to place select elements anywhere within a document, not just as descendants of their form elements.multiple
multiple
is specified, most browsers will show a scrolling list box instead of a single line dropdown.name
required
size
If the control is presented as a scrolling list box (e.g. when multiple
is specified), this attribute represents the number of rows in the list that should be visible at one time. Browsers are not required to present a select element as a scrolled list box. The default value is 0.
The <select>
element is notoriously difficult to style productively with CSS. You can affect certain aspects like any element — for example, manipulating the box model, the displayed font, etc., and you can use the appearance
property to remove the default system appearance
.
However, these properties don't produce a consistent result across browsers, and it is hard to do things like line different types of form element up with one another in a column. The <select>
element's internal structure is complex, and hard to control. If you want to get full control, you should consider using a library with good facilities for styling form widgets (such as jQuery UI), or try rolling your own dropdown menu using non-semantic elements, JavaScript, and WAI-ARIA to provide semantics.
For more useful information on styling <select>
, see:
The following example creates a very simple dropdown menu, the second option of which is selected by default.
<!-- The second value will be selected initially --> <select name="choice"> <option value="first">First Value</option> <option value="second" selected>Second Value</option> <option value="third">Third Value</option> </select>
This renders as follows:
The follow example is more complex, showing off more features you can use on a <select>
element:
<label>Please choose one or more pets: <select name="pets" multiple size="4"> <optgroup label="4-legged pets"> <option value="dog">Dog</option> <option value="cat">Cat</option> <option value="hamster" disabled>Hamster</option> </optgroup> <optgroup label="Flying pets"> <option value="parrot">Parrot</option> <option value="macaw">Macaw</option> <option value="albatross">Albatross</option> </optgroup> </select> </label>
We haven't shown this as a live example on the page because it wouldn't display correctly (MDN currently strips out the multiple
attribute when rendering the final page); instead you can view our multiple-select example on GitHub to see how it renders.
You'll see that:
multiple
attribute.size
attribute causes only 4 lines to display at a time; you can scroll to view all the options.<optgroup>
elements to divide the options up into different groups. This is a purely visual grouping, its visualization generally consists of the group name being bolded, and the options being indented.disabled
attribute and therefore can't be selected at all.Note: On a desktop computer, hold the Ctrl, Command, or Shift keys while clicking to select or deselect multiple options.
Content categories | flow content, phrasing content, interactive content, listed, labelable, resettable, and submittable form-associated element |
---|---|
Permitted content | Zero or more <option> or <optgroup> elements. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | any element that accepts phrasing content |
Permitted ARIA roles | menu |
DOM interface | HTMLSelectElement |
Specification | Status | Comments |
---|---|---|
HTML Living Standard The definition of '<select>' in that specification. | Living Standard | |
HTML5 The definition of '<select>' in that specification. | Recommendation | |
HTML 4.01 Specification The definition of '<select>' in that specification. | Recommendation |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes
|
Yes | 1
|
Yes | Yes | Yes
|
autofocus |
Yes | Yes | 1 | Yes | Yes | Yes |
disabled |
Yes | Yes | 1 | Yes | Yes | Yes |
form |
Yes | Yes | 1 | Yes | Yes | Yes |
multiple |
Yes | Yes | 1 | Yes | Yes | Yes |
name |
Yes | Yes | 1 | Yes | Yes | Yes |
required |
Yes | Yes | 4 | 10 | Yes | Yes |
size |
Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes
|
Yes
|
Yes | 4
|
Yes | Yes
|
Yes |
autofocus |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
disabled |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
form |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
multiple |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
name |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
required |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
size |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
<form>
, <legend>
, <label>
, <button>
, <option>
, <datalist>
, <optgroup>
, <fieldset>
, <textarea>
, <keygen>
, <input>
, <output>
, <progress>
and <meter>
.<select>
: change, input.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select