Dropdown menus, often called select boxes, allow users to choose one or more options from a list. These are crucial for forms, navigation, and filtering content, improving user experience on web pages.
Example 1: Basic HTML Dropdown with <select>
and <option>
<label for="fruits">Choose a fruit:</label>
<select id="fruits" name="fruits">
<option value="apple">Apple</option> <option value="banana">Banana</option>
<option value="orange" selected>Orange</option> <option value="grape">Grape</option>
</select>
Explanation This code creates a simple dropdown menu. The <label>
tag provides an accessible name for the select box, while <select>
defines the dropdown itself. Each <option>
tag represents an individual selectable item within the dropdown.
Example 2: HTML Dropdown with disabled
and value
attributes
<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
<option value="">--Please choose an option--</option> <option value="volvo">Volvo</option>
<option value="saab" disabled>Saab (Unavailable)</option> <option value="mercedes">Mercedes</option>
</select>
Explanation This example demonstrates how to disable specific options using the disabled
attribute, making them unselectable. The first option with an empty value
serves as a placeholder prompt for the user.
The <optgroup>
tag in HTML is used to group related options within a <select>
dropdown list. This enhances usability by visually organizing long lists of choices, making them easier for users to navigate.
Example 3: HTML Dropdown with <optgroup>
for Better Organization
<label for="web_dev_languages">Choose a web language:</label>
<select id="web_dev_languages" name="web_dev_languages">
<optgroup label="Frontend"> <option value="html">HTML</option>
<option value="css">CSS</option>
<option value="javascript">JavaScript</option>
</optgroup>
<optgroup label="Backend"> <option value="python">Python</option>
<option value="php">PHP</option>
<option value="ruby">Ruby</option>
</optgroup>
</select>
Explanation This code utilizes <optgroup>
to categorize options, making long lists more manageable. The label
attribute for <optgroup>
provides a heading for each group, improving the dropdown's readability.
Example 4: HTML Dropdown with multiple
attribute for multi-selection
<label for="favorite_colors">Select your favorite colors (hold Ctrl/Cmd to select multiple):</label>
<select id="favorite_colors" name="favorite_colors" multiple size="4"> <option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="purple">Purple</option>
</select>
Explanation Adding the multiple
attribute to the <select>
tag allows users to choose more than one option from the list (typically by holding Ctrl/Cmd). The size
attribute determines how many options are visible without scrolling.
Example 5: HTML Dropdown with required
attribute for form validation
<form action="/submit-form" method="post">
<label for="country">Select your country:</label>
<select id="country" name="country" required> <option value="">-- Please select --</option>
<option value="usa">United States</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
<button type="submit">Submit</button>
</form>
Explanation The required
attribute is used within the <select>
tag to ensure that the user makes a selection before submitting a form. This provides basic client-side validation for essential form fields.