CSS Command Search Bar

A docs-style search bar with a ⌘K shortcut hint that hides itself once you focus the field or start typing.

  • search
  • command palette
  • keyboard shortcut
  • kbd
  • cmd k
  • docs
HTML
<form class="cmd-search" role="search">
  <input type="search" name="q" placeholder="Search…" aria-label="Search" aria-keyshortcuts="Meta+K">
  <kbd aria-hidden="true">⌘K</kbd>
</form>
CSS
.cmd-search {
  position: relative;
  width: 240px;
  font-family: system-ui, sans-serif;
}

.cmd-search input {
  box-sizing: border-box;
  width: 100%;
  padding: 9px 52px 9px 36px;
  border: 1px solid #3f3f46;
  border-radius: 10px;
  background: #27272a url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a1a1aa' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E") no-repeat 11px center / 16px;
  color: #fafafa;
  font: 14px system-ui, sans-serif;
  outline: none;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}

.cmd-search input::placeholder {
  color: #a1a1aa;
}

.cmd-search input:hover {
  border-color: #52525b;
}

.cmd-search input:focus-visible {
  border-color: #a78bfa;
  background-color: #18181b;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.25);
}

.cmd-search kbd {
  position: absolute;
  top: 50%;
  right: 8px;
  padding: 2px 6px;
  border: 1px solid #52525b;
  border-radius: 6px;
  color: #a1a1aa;
  font: 600 11px system-ui, sans-serif;
  pointer-events: none;
  transform: translateY(-50%);
  transition: opacity 0.15s ease;
}

.cmd-search input:focus ~ kbd,
.cmd-search input:not(:placeholder-shown) ~ kbd {
  opacity: 0;
}

About this CSS Command Search Bar

This CSS Command Search Bar is 51 lines of pure CSS (1.3 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and keyboard focus styles. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #3f3f46
  • #27272a
  • #fafafa
  • #a1a1aa
  • #52525b
  • #a78bfa
  • #18181b

How to use this command search bar

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .cmd-search class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback