Skip to content

Search Bar with Category Dropdown

Tailwind CSS search bar with an accessible category dropdown on the left, popular search chips and a simulated result count.

Also known as: search bar with categories, search with category dropdown, site search with filter, search input with chips, category search box

Search Bar with Category Dropdown live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A marketplace-style search bar built with Tailwind CSS, where a category picker sits on the left of the input and narrows the search to one department. It fits e-commerce headers, directory sites, documentation portals and any catalog where people search within a section.

How it works

The root is section.tsc-search. Inside, form.tsc-form has role="search" and a hidden input marked data-tsc-value that carries the chosen category with the form. The white bar is a flex row that wraps on phones (category on its own line) and stays on one line from sm up. It holds three parts: the .tsc-cat picker, the #tsc-q search input and the dark submit button.

The picker is a custom listbox rather than a native select, so it can match the bar's styling. The data-tsc-toggle button opens ul#tsc-cat-list (role="listbox", marked data-tsc-list), and each li is a role="option" with a data-value. The highlighted option gets a data-active attribute, which Tailwind styles through the data-active:bg-amber-50 variant; the selected option's checkmark appears through group-aria-selected:visible.

The script initializes every .tsc-search on DOMContentLoaded. Choosing an option updates data-tsc-current, the hidden value and the input placeholder (for example "Search in Books"). On submit the form calls preventDefault(), writes "Searching…" into the data-tsc-status message and, after a short timeout, a simulated result count. Replace that timeout with a fetch() call to your search endpoint. The "Popular" chips use data-tsc-term and data-tsc-cat to fill the input, switch the category and submit in one click. style.css only rotates .tsc-chevron and adds the tsc-drop entrance animation.

Customization

  • Add or remove li[role="option"] items in #tsc-cat-list: give each a unique id and a data-value that your backend understands.
  • data-tsc-term and data-tsc-cat on the chip buttons: set your own popular searches and the category each one should select.
  • bg-stone-900 and hover:bg-amber-700 on the submit button: switch to another Tailwind color to match your brand.
  • focus-within:ring-amber-500 on the bar wrapper: changes the glow shown while any part of the bar has focus.
  • sm:w-64 on #tsc-cat-list: widen the menu for longer category names.
  • The setTimeout inside the submit handler in script.js: swap it for fetch() and render real results.

Use cases

  • Ecommerce sites: shoppers pick a category on the left before typing their query
  • Marketplaces and directories: popular search chips suggest common searches in one click
  • Help centers: a simulated result count shows how many matches a query returns
  • Header search: an accessible dropdown keeps the search narrow and easy to use by keyboard

Accessibility

The form is a search landmark. The toggle uses aria-haspopup="listbox", aria-expanded and aria-controls, and is labelled by a hidden "Category" label plus the current value. Inside the list, focus stays on the ul and aria-activedescendant points at the highlighted option. Arrow keys, Home and End move the highlight, Enter or Space selects, Escape closes and returns focus, and typing a letter jumps to the next matching category. Results are announced through a role="status" region, every control has a visible focus outline and the animation is turned off under prefers-reduced-motion.

Usage notes

For several bars on one page, give each copy unique id values (tsc-q, tsc-cat-list, the option ids); the script already scopes queries to each root.

Search Bar with Category Dropdown at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Search Bars
License
Free for personal and commercial use

Questions about this template

Can I use the Search Bar with Category Dropdown code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Search Bar with Category Dropdown – free Tailwind CSS search bar template
Search Bar with Category Dropdown · free Tailwind CSS search bar template

More Search Bars

Expanding Search Bar – free Pure CSS search bar template

Expanding Search Bar

Search icon button that expands into a pill-shaped input, shown in a dark navbar and a large hero search, with Escape to close and a demo submit.

Search with Autocomplete – free Bootstrap 5 search bar template

Search with Autocomplete

Bootstrap 5 help center search bar with an accessible autocomplete list built from local data, highlighted matches, keyboard navigation and popular search chips.

Command Palette Search – free Tailwind CSS search bar template

Command Palette Search

Dark Tailwind CSS command palette opened with Ctrl+K: live filtering with match highlights, grouped results and full keyboard navigation.