Explain Codes LogoExplain Codes Logo

Default text which won't be shown in drop-down list

html
best-practices
responsive-design
aria-labels
Alex KataevbyAlex Kataev·Oct 29, 2024
TLDR

In order to display a non-selectable default text in a dropdown, you can use both the disabled and selected attributes on a placeholder option.

<select name="options"> <!-- This is not the option you're looking for. --> <option value="" disabled selected>Choose an option</option> <!-- These are the options you're looking for. --> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select>

The placeholder option "Choose an option" will not be selectable and will prompt users to choose one of the available options.

Completing forms successfully

Preventing unintentional form submissions

If you don't want the form to submit unless the user makes an explicit choice, you can set the placeholder option value to an empty string. This minor change can bring a major improvement in your form's data handling.

Making the placeholder option work for you

The placeholder can appear by default and remain non-selectable, serving as a gentle nudge to the users that they need to make a choice. Use <option disabled="disabled" selected="selected"> to make this possible.

Implementing CSS for better visibility

You can hide the placeholder from the dropdown list of options using this CSS snippet:

/* "I can't see you, you can't see me." - Placeholder Option */ select option[disabled] { display: none; }

This keeps the HTML clean and the non-selectable placeholder out of sight when not required.

Ensuring user input is deliberate

You can include the required attribute on the select element to ensure a user choice is made, enhancing the overall usability of your form.

Working magic with CSS

Staying compatible with browsers

If you choose to hide options using CSS, remember that browser compatibility could affect whether select options are visible. In such cases, you might need additional solutions or polyfills.

Adhering to HTML5

Stick to HTML5 standards to keep your dropdowns predictable across various user agents and browsers, avoiding the unexpected quirks and bugs that can sneak in otherwise.

Adjusting the appearance

Wrapping the select element

Wrap the select element inside a div container, which allows you to target it with CSS while keeping the core functionality intact.

<div class="dropdown"> <select required> <option value="" disabled selected>Select an option</option> <!-- Other options, also known as the popular guys --> </select> </div>
.dropdown select option[disabled] { display: none; /* "I'm the option behind the curtain!" - Placeholder Option */ }

Additional thoughts

In non-JavaScript environments

Consider progressive enhancement strategies for JavaScript-disabled environments. This ensures your select element works with HTML basics and enhances functionalities and appearances with CSS and JavaScript when available.

Accessibility matters

Add in aria-labels or other ARIA roles for the screen readers to interpret the placeholder accurately. This approach respects accessibility best practices and helps all users navigate the dropdown efficiently.

Going beyond the basics

Create interactive visual cues using :hover and :focus styles to guide users more effectively. For an enhanced user experience, consider custom dropdowns using JavaScript or libraries like Select2.

Key takeaway

Use semantic HTML to communicate the desired state of the placeholder. Add CSS with care, keeping in mind standards and cross-browser behavior. Finally, ensure a consistent UX regardless of the client's capabilities or settings.