Table of Content
HTML select tag and option tags let users pick values in a form. You use them to create dropdowns. The main keyword here is “HTML select and option.”
What Are the <select> and <option> Tags?
The HTML select tag creates a drop-down list in a form. It holds many <option> tags. Each <option> defines a single choice in that list.
You use <select> to wrap your options. The browser shows the list. The <option> tag provides the choices. When you submit the form, the value of the selected option goes to the server.
Here is the basic syntax:
<select name="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
</select>The <select> element defines the dropdown. The name attribute sets the key for form data. Each <option> has a value sent with the form. The text inside <option> is what the user sees.
You can set one option as selected by default. Use the selected attribute on an option:
<select name="fruits">
<option value="apple" selected>Apple</option>
<option value="banana">Banana</option>
</select>The browser shows “Apple” as the default choice when the page loads.
You use HTML select and option tags in forms to let users choose one or more items from a list. This helps keep forms short and user-friendly when there are many possible choices.
You can make options unselectable with the disabled attribute:
<select name="fruits">
<option value="apple">Apple</option>
<option value="banana" disabled>Banana (Unavailable)</option>
</select>“Banana” shows in the list but users cannot choose it. This is useful to show items that are not available.
Group Options with <optgroup>
The <optgroup> tag groups related options in a dropdown. This helps organize large lists.
You define a label for the group. All options inside belong to that group. Browsers show the label as a heading.
Here is the syntax:
<select name="cars">
<optgroup label="German Cars">
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
</optgroup>
<optgroup label="Japanese Cars">
<option value="toyota">Toyota</option>
<option value="honda">Honda</option>
</optgroup>
</select>The <optgroup> element uses the label attribute. Options inside it appear grouped under that label.
Here is an example:
<select name="food">
<optgroup label="Fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
</optgroup>
<optgroup label="Vegetables">
<option value="carrot">Carrot</option>
<option value="broccoli">Broccoli</option>
</optgroup>
</select>This groups fruits and vegetables to make it easy for users to find items.
Here is another example:
<select name="country">
<optgroup label="Europe">
<option value="france">France</option>
<option value="germany">Germany</option>
</optgroup>
<optgroup label="Asia">
<option value="japan">Japan</option>
<option value="china">China</option>
</optgroup>
</select>This separates countries by continent. It improves usability for long lists.
The multiple Attribute
You use the multiple attribute to let users select more than one option. The browser shows a list box instead of a single dropdown.
When you add multiple, users can hold down Ctrl (Windows) or Command (Mac) to select more items.
Here is the syntax:
<select name="colors" multiple>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>The form submits all selected values.
Here is an example for selecting skills:
<select name="skills" multiple>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="javascript">JavaScript</option>
</select>Users can choose all skills they know.
Here is another example for choosing interests:
<select name="interests" multiple>
<option value="music">Music</option>
<option value="sports">Sports</option>
<option value="reading">Reading</option>
<option value="travel">Travel</option>
</select>This lets users pick many interests to describe themselves.
How to Style Select Tag Menus with CSS
You can customize the look of select menus with CSS. Here are a few ways.
Change Width and Font
<style>
select {
width: 200px;
font-size: 16px;
}
</style>Add Background and Border
<style>
select {
background-color: #f0f0f0;
border: 2px solid #333;
padding: 5px;
}
</style>This gives a light background, strong border, and spacing to make it look modern.
Custom Arrow with Appearance
<style>
select {
appearance: none;
-webkit-appearance: none;
background: #fff url('arrow.png') no-repeat right;
padding-right: 30px;
}
</style>This removes the default arrow and adds a custom one on the right.
Examples of HTML Select Tag
Simple Dropdown
<select>
<option>Yes</option>
<option>No</option>
</select>A basic yes/no choice.
Dropdown with Values
<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>Stores value data with the form.
Default Selected Option
<select name="language">
<option value="en" selected>English</option>
<option value="fr">French</option>
</select>English shows first when the page loads.
Disabled Option
<select name="plan">
<option value="free">Free</option>
<option value="premium" disabled>Premium (Unavailable)</option>
</select>Premium is not selectable.
Grouped Options
<select name="cars">
<optgroup label="Electric">
<option value="tesla">Tesla</option>
<option value="nissan">Nissan Leaf</option>
</optgroup>
<optgroup label="Gasoline">
<option value="ford">Ford Mustang</option>
<option value="chevy">Chevy Camaro</option>
</optgroup>
</select>Shows clear categories
Multiple Selection
<select name="hobbies" multiple>
<option value="gaming">Gaming</option>
<option value="cooking">Cooking</option>
<option value="hiking">Hiking</option>
</select>Users can pick many hobbies.
Wrapping Up
In this article, you learned what the HTML select and option tags do and how to use them. You also saw how to group options, use multiple selections, and style your menus.
Here is a quick recap:
<select>holds the dropdown list.<option>provides the choices.- You can set a default option with
selected. - Disable options with
disabled. - Group items with
<optgroup>. - Let users pick many options with
multiple. - Style menus with CSS for better design.
FAQs
What is the HTML tag?
How do you add options to a dropdown?
How to set a default selected option?
Can a user select multiple options?
How to group options inside a dropdown?
How do I disable an option in a dropdown?
Similar Reads
Use the HTML b tag to make text bold without giving it extra meaning. It adds visual weight only. Understand…
This article shows you how to open a link in a new tab. Read the code and follow the examples…
The HTML class attribute helps group elements. It helps you update the page style without changing the structure of HTML.…
The DOCTYPE ensures that your HTML is readable by browsers and ensures that pages follow modern standards. Understand the DOCTYPE…
HTML code does not always show the same result on every browser. Old browsers follow outdated rules while modern browsers…
The <output> tag in HTML shows results inside a form. It helps display values from user actions or scripts. You…
HTML Boolean attributes control the behavior of elements and do not need a value. These attributes simplify logic and reduce…
The track tag in HTML adds text tracks like subtitles and captions to media. It improves video access and helps…
The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…
The header tag defines the top part of a page or a section in HTML. It usually holds a heading…