Codectionary / Developer documentation / HTML

<select>

The <select> element creates a dropdown list of options, letting users pick one or more predefined values. Each choice is defined with an <option> element inside it. Related options can be grouped visually using <optgroup>. It is a compact way to offer a constrained set of choices without the space a list of radio buttons would need.

Syntax

<select name="choice">
  <option value="1">Option One</option>
  <option value="2">Option Two</option>
</select>

Examples

Basic Dropdown

A simple select with a few options.

<label for="country">Country:</label>
<select id="country" name="country">
  <option value="uk">United Kingdom</option>
  <option value="us">United States</option>
  <option value="ca">Canada</option>
</select>

Select with Default and Disabled Option

Showing a placeholder option that cannot be selected as a real value.

<select name="role" required>
  <option value="" disabled selected>Choose a role...</option>
  <option value="student">Student</option>
  <option value="developer">Developer</option>
</select>

Grouped Options

Organizing options into labeled groups with optgroup.

<select name="language">
  <optgroup label="Frontend">
    <option value="js">JavaScript</option>
    <option value="ts">TypeScript</option>
  </optgroup>
  <optgroup label="Backend">
    <option value="php">PHP</option>
    <option value="python">Python</option>
  </optgroup>
</select>

Attributes

  • name (string): Identifies the field in submitted form data
  • multiple (boolean): Allows selecting more than one option
  • required (boolean): Requires a non-empty selection before submission
  • disabled (boolean): Disables the entire dropdown

Best practices

  • Use a disabled, selected placeholder option instead of leaving the first real option selected by default
  • Pair with a <label> for accessibility, same as any other form control
  • Use optgroup to organize long lists of options into logical categories
  • For a handful of mutually exclusive options, consider whether radio buttons might actually be clearer than a dropdown

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation