HTML <datalist> Element

Publish in HTML Tutorial el 23/05/2025 19:37

The <datalist> HTML element provides an "autocomplete" feature for <input> elements. It contains a set of <option> elements that represent predefined values for the input. Unlike <select>, users can still enter custom values while having suggestions available.


Key features:

  • Works with text, search, url, tel, email, date, and number input types
  • Provides dropdown suggestions while allowing free-form input
  • Improves user experience by reducing typing effort
  • Accessible by default with keyboard navigation


Basic Syntax

Here's how to define a basic datalist:


Rendered Example

Here's how the above code renders:

Play Code


Examples

1. Basic Color Picker

A simple color suggestion list with common color names:

Play Code


2. Number Range with Steps

Using datalist to suggest number values with a specific step:

Play Code


3. Email Suggestions

Common email domain suggestions for user convenience:

Play Code


4. Dynamic Datalist with JavaScript

Populating a datalist dynamically based on user input:

Play Code


5. Fetching Options from API

Loading datalist options from an external API:

Play Code


Tips and Tricks

  • Accessibility: Always pair your input with a proper <label> element for better accessibility.
  • Mobile Experience: Datalist works well on mobile devices, providing a native-like picker experience.
  • Fallback: For browsers that don't support datalist, consider adding JavaScript polyfills.
  • Performance: For large datasets, consider lazy-loading options or implementing server-side filtering.
  • Styling: While the dropdown itself can't be styled, you can style the input element to match your design.


Browser Support

The <datalist> element is supported in all modern browsers, including Chrome, Firefox, Edge, and Safari. However, some older browsers (like IE) may have limited or no support.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...