Why We Should Be Using the <search> Element More
- Discuss this with your agent
- Open in Claude
- Open in ChatGPT
When you consider how integral search elements to user interfaces and user experience more general, you might be surprised to learn they were only introduced to the HTML spec in 2023.
If youâve ever built a search bar in HTML, it could well have looked a little like this:
<div class="search-container" role="search">
<form action="/search" method="get">
<label for="q">Search</label>
<input type="text" id="q" name="q" placeholder="Search..." />
<button type="submit">Go</button>
</form>
</div>
Itâs not terrible, but thereâs a lot going on. The role="search" is there to communicate to screen readers what itâs for, the wrapping <div> is largely redundant but used for styling with a class or ID, and if you forget the ARIA role, your search widget is just an anonymous blob in the accessibility tree.
Handle all of these issues, and make your HTML more semantic with the <search> element, so letâs take a closer look at itâŚ
The <search> Element
The <search> element is a semantic HTML element designed specifically for search and filtering.
It maps implicitly to the search ARIA landmark role, so screen reader users can navigate to it just like they would a <nav> or <main>, with means the role attribute isnât needed.
With this in mind, we would be able to replace that redundant <div> right away:
<search>
<form action="/search" method="get">
<label for="q">Search</label>
<input type="search" id="q" name="q" placeholder="Search..." />
<button type="submit">Go</button>
</form>
</search>
Beyond Global Search
The <search> elements works anywhere users are searching or filtering content, not just a global search bar. A filter panel on a product listings page, a âfind in this listâ input, a location search in a booking flow, all of these are appropriate uses for the element, like so:
<search aria-label="Filter products">
<label for="filter">Filter by name</label>
<input type="search" id="filter" name="filter" />
</search>
If youâve got more than one <search> on a page, add an aria-label to distinguish them â just like you would with multiple <nav> elements.
Input Type Matters
Youâll also notice that inside of <search>, weâre using <input type="search"> rather than <input type="text">, which will render a clear (Ă) button once the user starts typing and assistive tools that specifically look for search inputs will identify it correctly.
<!-- just a text field -->
<input type="text" name="q" />
<!-- tells the browser this is a search input -->
<input type="search" name="q" />
We always enjoy sharing tips you can act on right away, and this one couldnât be simpler. As you can see, in addition to making your HTML a little more semantic, using <search> means your ARIA roles are handled for you, and assistive technology users get a proper landmark to navigate to.
As always, let us know if youâve got some tips and tricks youâd like to share and subscribe to the Dev Digest for more.