Snippets Bar

Bootstrap 5 Expanding Search Bar with Suggestions

A pill-shaped search button that expands into a full search field on hover or focus, with live highlighted suggestions and keyboard navigation.

Bootstrap 5 No jQuery Aug 2020 3.2K views

<section class="wdw-stage py-4">
  <div class="container" style="max-width:680px">
    <div class="text-center mb-4">
      <span class="wdw-eyebrow">Expanding search</span>
      <h1 class="wdw-heading display-5 mt-1 mb-2">Search the docs</h1>
      <p class="wdw-muted mb-0">A single icon that grows into a full search bar. Hover it, tap it, or press /.</p>
    </div>

    <div class="xs-wrap">
      <div class="d-flex justify-content-center">
        <form class="xs is-open" id="xsearch" role="search" onsubmit="return false">
          <label for="xsInput" class="visually-hidden">Search the docs</label>
          <input id="xsInput" class="xs-input" type="search" value="bu" placeholder="Search docs…" autocomplete="off" role="combobox" aria-controls="xsList" aria-expanded="true" aria-haspopup="listbox" aria-autocomplete="list">
          <kbd class="xs-kbd">/</kbd>
          <button class="xs-btn" type="submit" id="xsBtn" aria-label="Search">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>
          </button>
          <div class="xs-panel" id="xsPanel">
            <div class="xs-label" id="xsLabel">Suggestions</div>
            <ul class="xs-list list-unstyled m-0" id="xsList" role="listbox" aria-labelledby="xsLabel"></ul>
            <p class="xs-empty m-0" id="xsEmpty" hidden></p>
            <div class="xs-foot" aria-hidden="true">
              <span><kbd>↑</kbd> <kbd>↓</kbd> to navigate</span>
              <span><kbd>↵</kbd> to select</span>
              <span class="ms-auto"><kbd>esc</kbd> to close</span>
            </div>
          </div>
        </form>
      </div>
      <p class="text-center small wdw-muted mt-3 mb-0" id="xsStatus" role="status" aria-live="polite"></p>
    </div>
  </div>
</section>

Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.