Snippets Input

Bootstrap 5 Expanding Search Input on Focus

A pill-shaped search field that expands to full width on focus, with a clear button, suggestion chips and a slash shortcut.

Bootstrap 5 No jQuery Aug 2019 2.2K views

<section class="wdw-stage">
<div class="container" style="max-width:680px">
  <div class="text-center mb-4">
    <span class="wdw-eyebrow">Search</span>
    <h2 class="wdw-heading display-6 mt-1 mb-2">Find anything, fast</h2>
    <p class="wdw-muted mb-0">The field grows as you focus it and suggests matches as you type. Press <kbd>/</kbd> to jump in.</p>
  </div>
  <div class="wdw-search" id="growSearch">
    <form class="d-flex justify-content-center" role="search" id="growForm">
      <label for="growInput" class="visually-hidden">Search components</label>
      <div class="wdw-grow input-group rounded-pill" id="growField">
        <span class="input-group-text border-0 bg-transparent ps-3 pe-1"><i class="bi bi-search" aria-hidden="true"></i></span>
        <input type="search" id="growInput" class="form-control border-0 bg-transparent shadow-none px-2" placeholder="Search components" autocomplete="off" role="combobox" aria-expanded="false" aria-controls="growResults" aria-autocomplete="list">
        <span class="spinner-border spinner-border-sm text-primary align-self-center me-2 d-none" id="growSpin" aria-hidden="true"></span>
        <kbd class="align-self-center me-3 d-none d-sm-inline" id="growHint" aria-hidden="true">/</kbd>
        <button type="button" class="btn btn-sm btn-link text-body-secondary border-0 pe-3 d-none" id="growClear" aria-label="Clear search"><i class="bi bi-x-lg" aria-hidden="true"></i></button>
      </div>
    </form>
    <div class="mt-2 d-none" id="growPanel">
      <div class="list-group wdw-card-sm overflow-hidden" id="growResults" role="listbox" aria-label="Suggestions"></div>
      <div class="d-flex justify-content-between small text-body-secondary px-2 pt-2"><span id="growCount" role="status"></span><span><kbd>↑</kbd> <kbd>↓</kbd> to move · <kbd>Esc</kbd> to close</span></div>
    </div>
    <div class="d-flex flex-wrap justify-content-center gap-2 mt-4" role="group" aria-label="Suggested searches">
      <button type="button" class="wdw-chip btn btn-sm rounded-pill border bg-body-tertiary text-body-secondary" data-suggest="Pricing tables">Pricing tables</button>
      <button type="button" class="wdw-chip btn btn-sm rounded-pill border bg-body-tertiary text-body-secondary" data-suggest="Navbars">Navbars</button>
      <button type="button" class="wdw-chip btn btn-sm rounded-pill border bg-body-tertiary text-body-secondary" data-suggest="Login forms">Login forms</button>
    </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.