Techoceanhub logo

Select menus and dropdown lists in HTML

To create select menus and dropdown lists in HTML, you can use the <select> element along with the <option> elements. The <select> element defines a dropdown list, and the <option> elements define the available options within the dropdown. Here’s an example:

<select>
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
  <option value="option3">Option 3</option>
</select>

In the example above, we have a basic select menu with three options: “Option 1”, “Option 2”, and “Option 3”. Each <option> element represents a single option within the dropdown. The value attribute is used to specify the value associated with each option.

To set a default selected option, you can add the selected attribute to the desired <option> element:

<select>
  <option value="option1">Option 1</option>
  <option value="option2" selected>Option 2</option>
  <option value="option3">Option 3</option>
</select>

In this updated example, “Option 2” will be selected by default when the page loads.

You can also group related options together using the <optgroup> element:

<select>
  <optgroup label="Group 1">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
  </optgroup>
  <optgroup label="Group 2">
    <option value="option3">Option 3</option>
    <option value="option4">Option 4</option>
  </optgroup>
</select>

In this case, the options are grouped into two sections: “Group 1” and “Group 2”. The label attribute is used to specify the label for each group.

These are the basics of creating select menus and dropdown lists in HTML. You can further customize their appearance and behavior using CSS and JavaScript if needed.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .