October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Build a Free Animated Search Bar with HTML and CSS

Build a compact, animated search form with an accessible label, keyboard-visible focus, reduced-motion support, and a clear path to connect it to your site’s search endpoint.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can animate a search bar with CSS, but the animation alone does not make it search. The example below includes a labeled search form that submits a query to a site endpoint; change the form’s action to your own search URL. Its focus animation is decorative, and it reduces that motion for visitors who request reduced motion.

What this search bar does—and what you need to change

This is a front-end form with an animated visual state. When submitted, it sends the entered query as a GET parameter named q to the URL in the form’s action. The sample uses /search as a placeholder endpoint, so it will only produce search results if your site implements that route and accepts that parameter.

As an Amazon Associate I earn from qualifying purchases.

CSS controls the appearance, not the search itself. If your site uses a different query parameter or URL pattern, update the form accordingly. If you want to filter content on the current page instead, you will need JavaScript to read the input and filter that content.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build the search form

The form uses the native <search> landmark, a real search input, and a visible label. The label remains available to assistive technology and gives all users a clear name for the field; the placeholder is only an example query, not a replacement for that label. MDN documents both the native search element and the search input type.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
<search>
  <form class="search-form" action="/search" method="get">
    <label class="search-label" for="site-query">Search this site</label>
    <input
      class="search-input"
      id="site-query"
      name="q"
      type="search"
      placeholder="Try a keyword"
    >
    <button class="search-button" type="submit">
      <span aria-hidden="true">⌕</span>
      <span>Search</span>
    </button>
  </form>
</search>

The icon is decorative, so it is hidden from assistive technology; the button’s text supplies its accessible name. Keep the button text visible or provide an equivalent accessible name if you redesign it as an icon-only control.

Add the focus animation and reduced-motion behavior

This CSS expands the input when the form receives keyboard focus or pointer interaction. The transition is short and tied to the user’s interaction rather than running continuously. MDN describes prefers-reduced-motion as a way to detect when a user has enabled a device setting to minimize nonessential motion.

.search-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: 32rem;
  padding: 0.5rem;
  border: 1px solid #8a94a6;
  border-radius: 999px;
  background: #fff;
}

.search-label {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.search-input {
  box-sizing: border-box;
  flex: 1 1 12rem;
  min-width: 8rem;
  padding: 0.75rem 1rem;
  border: 0;
  border-radius: 999px;
  font: inherit;
  transition: flex-basis 220ms ease, background-color 220ms ease;
}

.search-input:focus {
  flex-basis: 20rem;
  background: #f0f4ff;
}

.search-form:focus-within {
  outline: 3px solid #3157c8;
  outline-offset: 3px;
}

.search-button {
  padding: 0.7rem 1rem;
  border: 0;
  border-radius: 999px;
  background: #193b9b;
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.search-button:focus-visible {
  outline: 3px solid #111;
  outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
  .search-input {
    transition: none;
  }
}

The label is visually hidden here to keep the compact pill shape; remove the clipping styles if you want it displayed above or beside the input. The focus outline remains visible so keyboard users can track their position. W3C WAI describes the reduced-motion media query as one technique for preventing motion, while noting that its techniques are examples rather than the only way to meet accessibility guidelines: WAI technique C39.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose an animation that fits the interaction

  • Focus-triggered transition: useful when you want a compact field to expand only while someone interacts with it, as in this example.
  • Always-running keyframes: can draw attention, but they keep moving without user input. Avoid unnecessary continuous movement; provide a pause or disable control if it is essential to the experience. MDN’s animation reference advises considering a way to pause or disable animations.
  • JavaScript-controlled behavior: appropriate when the search must filter results dynamically or when animation depends on application state. It adds behavior beyond CSS and should not replace form submission unless that is intentional.

Reduced-motion styling and an animation pause control address related but different situations: the media query respects a device preference, while a control lets a user stop motion directly. For a short focus transition, the reduced-motion rule shown above removes the transition altogether.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Check the result before publishing

  1. Replace /search with your site’s actual search endpoint and confirm it accepts the parameter named q.
  2. Submit a query with the keyboard and with the button. Verify the browser navigates to a results page that uses the entered term.
  3. Use the Tab key to reach the input and button. Confirm the focus outline is visible and that the label and button have clear accessible names.
  4. Turn on your operating system’s reduced-motion preference, reload the page, and verify the input no longer animates.
  5. If you add continuous or longer-running motion, include a way to pause or disable it rather than relying only on the operating-system preference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.