Home 〉 HTML Tutorials 〉 HTML select and option tag - Complete Guide with Syntax, Attributes, and Examples
HTML select and option tag - Complete Guide with Syntax, Attributes, and Examples !
Want to create dropdowns in HTML forms?
The '<select>' and '<option>' tags allow you to display a list of options from which users can choose. You've learned the syntax, attributes like 'multiple', 'required', 'selected', and how to style these elements using inline CSS. Dropdowns are commonly used for form controls like gender, country, and category. The complete blog post on HTML select and option tag is as follows.
The '<select>' and '<option>' tags are essential when you want to create dropdown lists in HTML forms. These tags allow users to select a value from a list of predefined options, which is extremely useful for form submissions such as choosing a country, selecting a category, or picking a date.
In this blog post, you will learn the purpose, syntax, usage, attributes, and examples of both the '<select>' and '<option>' tags. This guide is beginner-friendly and helps you implement clean and accessible dropdown menus.
Syntax ✍
<select name="dropdown">
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
</select>
The following are the attributes of HTML select tag :
<!DOCTYPE html>
<html>
<body>
<select name="fruit" id="fruitSelect" required style="width:200px; font-size:14px; padding:5px; border-radius:4px;">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
</select>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The following are the attributes of HTML option tag :
<!DOCTYPE html>
<html>
<body>
<select style="width:250px; font-size:16px; padding:10px; border:1px solid #ccc; border-radius:5px; background-color:#f9f9f9;">
<option value="banana">Banana</option>
<option value="mango" selected>Mango</option>
<option value="bpple">Apple</option>
</select>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'selected': Sets "Mango" as the default selected option when the page loads.
<!DOCTYPE html>
<html>
<body>
<option value="mango" selected>Mango</option>
<select style="width:250px; font-size:16px; padding:10px; border:1px solid #ccc; border-radius:5px; background-color:#f9f9f9;">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Purpose: Adds width, font-size, padding, border, and background-color for clean design.
The HTML '<select>' and '<option>' tags are powerful tools for creating dropdown menus in forms. By understanding their syntax, attributes, and styling, you can create user-friendly and functional form controls. These elements are widely used and essential in modern web development.
It is used to create a dropdown list in a form.
Add the 'selected' attribute to the desired '<option>' tag.
Yes, use the 'multiple' attribute in the '<select>' tag.
Use the 'disabled' attribute inside the '<option>' tag.