Snippets Animation

Bootstrap 5 Expandable Search Bar with Suggestions

An expandable search bar that grows from a round icon into a wide pill with live-filtered suggestions, a clear button and Esc to close.

Bootstrap 5 No jQuery Jul 2019 2.2K views

<section class="wdw-stage wdw-aurora">
  <div class="container text-center" style="max-width: 640px">
    <span class="badge rounded-pill sb-pill mb-3">Docs search</span>
    <h1 class="display-5 fw-bold mb-2" style="letter-spacing:-.03em">What are you looking for?</h1>
    <p class="opacity-75 mb-5">Tap the icon to expand the search. Press Esc to fold it back.</p>

    <form class="sb" id="sb" role="search" autocomplete="off">
      <label class="visually-hidden" for="sbInput">Search</label>
      <button type="button" class="sb-btn" id="sbToggle" aria-label="Open search" aria-expanded="true" aria-controls="sbInput">
        <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="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>
      <input type="search" id="sbInput" class="sb-input" placeholder="Search components and guides" name="q">
      <button type="reset" class="sb-clear" id="sbClear" aria-label="Clear search"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
      <ul class="sb-list list-unstyled" id="sbList" aria-label="Suggestions">
        <li><a href="#" class="sb-item"><span>Bootstrap 5 pricing table</span><small>Component</small></a></li>
        <li><a href="#" class="sb-item"><span>Responsive navbar with dropdown</span><small>Component</small></a></li>
        <li><a href="#" class="sb-item"><span>Dark mode toggle guide</span><small>Guide</small></a></li>
        <li><a href="#" class="sb-item"><span>Accessible form validation</span><small>Guide</small></a></li>
      </ul>
    </form>
  </div>
</section>

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

Free to use in personal and commercial projects under the MIT license.