Snippets Animation

Bootstrap 5 Expandable Search Bar

A pill-shaped search button that expands into a text field on click or focus and collapses when empty.

Bootstrap 5 No jQuery Jul 2019 3K views

<section class="wdw-stage">
  <div class="container" style="max-width:680px">
    <div class="mb-4 text-center">
      <span class="wdw-eyebrow">Search</span>
      <h2 class="wdw-heading h2 mb-1">What are you looking for?</h2>
      <p class="wdw-muted mb-0">Click the icon to expand the field. Press <kbd>/</kbd> to focus, <kbd>Esc</kbd> to collapse.</p>
    </div>
    <div class="wdw-aurora rounded-5 p-4 p-md-5 xs-panel">
      <div class="xs-demo">
        <p class="small fw-semibold opacity-75 mb-2">Collapsed: expands on click</p>
        <form class="xs position-relative" role="search" data-xs>
          <label for="xsInput1" class="visually-hidden">Search</label>
          <input id="xsInput1" class="xs-input" type="search" role="combobox" aria-expanded="false" aria-controls="xsList1" aria-autocomplete="list" placeholder="Search articles, docs, people" autocomplete="off" tabindex="-1">
          <button type="button" class="xs-btn" aria-label="Search" aria-expanded="false" aria-controls="xsInput1">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" 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-pop wdw-card-sm" hidden>
            <p class="wdw-eyebrow px-3 pt-3 mb-1">Popular searches</p>
            <ul class="list-group list-group-flush rounded-bottom-4" id="xsList1" role="listbox" aria-label="Suggestions">
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Bootstrap 5 navbar</li>
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Dark mode toggle</li>
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Pricing table</li>
            </ul>
          </div>
        </form>
      </div>
      <div class="xs-gap"></div>
      <div class="xs-demo">
        <p class="small fw-semibold opacity-75 mb-2">Expanded: inline search</p>
        <form class="xs is-open position-relative" role="search" data-xs>
          <label for="xsInput2" class="visually-hidden">Search</label>
          <input id="xsInput2" class="xs-input" type="search" role="combobox" aria-expanded="false" aria-controls="xsList2" aria-autocomplete="list" placeholder="Search articles, docs, people" autocomplete="off">
          <button type="button" class="xs-btn" aria-label="Search" aria-expanded="true" aria-controls="xsInput2">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" 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-pop wdw-card-sm" hidden>
            <p class="wdw-eyebrow px-3 pt-3 mb-1">Popular searches</p>
            <ul class="list-group list-group-flush rounded-bottom-4" id="xsList2" role="listbox" aria-label="Suggestions">
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Bootstrap 5 navbar</li>
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Dark mode toggle</li>
              <li class="list-group-item list-group-item-action" role="option" tabindex="-1">Pricing table</li>
            </ul>
          </div>
        </form>
      </div>
    </div>
  </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.