The <select>
tag in HTML is used to create a drop-down list that allows users to choose one (or multiple) options from a predefined list. It is a powerful form element commonly used in surveys, settings panels, and input forms.
📌 What Is the <select>
Tag?
- The
<select>
element creates a control that lets users select options. - It is always used in combination with one or more
<option>
tags. - Optionally, it can support multiple selections using the
multiple
attribute.
✅ Basic Syntax
<select name="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
Rendered:
A dropdown list with 3 options.
🧩 Visual Breakdown
<select name="...">
├─ <option>Apple</option>
├─ <option>Banana</option>
└─ <option>Cherry</option>
</select>
🧪 Example 1: Basic Single-Select Menu
<label for="car">Choose a car:</label>
<select name="car" id="car">
<option value="tesla">Tesla</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</select>
🧪 Example 2: Multi-Select Dropdown
<select name="colors" multiple size="4">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
</select>
Hold
Ctrl
(orCmd
on Mac) to select multiple options.
🧪 Example 3: Using <optgroup>
for Grouped Options
<select name="country">
<optgroup label="Europe">
<option value="fr">France</option>
<option value="de">Germany</option>
</optgroup>
<optgroup label="Asia">
<option value="jp">Japan</option>
<option value="cn">China</option>
</optgroup>
</select>
🎨 Styling with CSS
select {
padding: 0.5em;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 1em;
}
⚙️ Useful Attributes
Attribute | Description |
---|---|
name | Name of the dropdown for form submission |
id | ID for label reference or scripting |
multiple | Allows multiple options to be selected |
size | Number of visible options at once |
disabled | Disables the dropdown |
required | Makes selection mandatory in forms |
⚠️ Accessibility Tip
Always use a <label>
with the for
attribute connected to the id
of the <select>
element for better screen reader support.
🔁 <select>
vs Other Input Types
Tag | Use Case |
---|---|
<select> | Choose from fixed set of options |
<input type="text"> | Free-form input |
<input type="radio"> | Select one of few |
<input type="checkbox"> | Select multiple |
🏁 Summary
The <select>
element is ideal when you want users to pick from a list of options. It’s lightweight, customizable, and easy to use in both simple and complex forms.