Skip to content

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.

Also known as: autocomplete search bar, search suggestions dropdown, help center search, search with suggestions

Search with Autocomplete live demo

Open in new tab

Bootstrap 5 code (HTML, CSS, JS)

A Bootstrap 5 search bar with an autocomplete suggestion list that filters local data as you type, highlights the matching text and works fully with the keyboard. It fits help centers, documentation sites, dashboards and any page where users jump to a known item.

How it works

The root is section.sac-search. Inside the form.sac-form (with role="search"), .sac-field holds the search icon, the .sac-input text field, a .sac-clear button and the .sac-submit button. The suggestion popup .sac-popup is absolutely positioned under the field and contains the ul.sac-list listbox, a .sac-empty message and a row of keyboard hints.

The suggestions come from the ITEMS array at the top of script.js; each entry has a title, a category and a Bootstrap Icons class. On input the script waits for at least MIN_CHARS characters, ranks the entries (titles that start with the query first, then word starts, then any match, then category matches), keeps the first few and builds one .sac-option per result. The matching part of the title is wrapped in a mark element using text nodes only, so user input is never injected as HTML.

ArrowDown and ArrowUp move the active option (it wraps around), Enter picks it and Escape closes the list or clears the field. Clicking an option works too; a mousedown handler keeps focus in the input. Picking an option fills the field, writes a line into .sac-result and dispatches a bubbling sac:select event with the chosen item in event.detail. The .sac-chip buttons under the field fill in a popular query and open the list.

Customization

  • ITEMS in script.js: replace the sample articles with your own pages, products or contacts.
  • data-sac-limit on .sac-search: change how many suggestions are shown (default 6).
  • MIN_CHARS in script.js: set to 1 to suggest after the first keystroke.
  • --sac-accent, --sac-accent-hover and --sac-accent-rgb in .sac-search: recolor the button, focus border, chips and highlighted text.
  • --sac-highlight in .sac-search: change the background of the active suggestion.
  • --sac-radius in .sac-search: make the field and popup rounder or square.
  • max-height in .sac-list: let the list grow taller before it scrolls.
  • The submit handler in script.js: replace the demo message with a redirect or a fetch() to your search API.

Use cases

  • Help centers: suggestions filter local data as visitors type and highlight the matching text
  • Documentation sites: keyboard navigation lets users pick a suggestion without a mouse
  • Dashboards: users jump straight to a known item from the suggestion list
  • Support pages: popular search chips offer one-click shortcuts for common questions

Accessibility

The input follows the ARIA combobox pattern: role="combobox", aria-autocomplete="list", aria-expanded and aria-controls pointing at the role="listbox" list. The active option is announced through aria-activedescendant and aria-selected, so focus stays in the input. A visually hidden .sac-status live region reports how many suggestions are available, and the field has a real label plus a hidden hint. Buttons have visible focus outlines, and the popup animation is turned off with prefers-reduced-motion: reduce.

Usage notes

Requires Bootstrap 5 and Bootstrap Icons. Several instances can live on one page: the script adds a suffix to the IDs of every instance after the first. For large datasets, fetch suggestions from a server endpoint and debounce the input handler.

Search with Autocomplete at a glance

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

Questions about this template

Can I use the Search with Autocomplete 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 Bootstrap 5 and Bootstrap Icons 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):

  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  • <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
Search with Autocomplete – free Bootstrap 5 search bar template
Search with Autocomplete · free Bootstrap 5 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.

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.