# Command Search Bar

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

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: search, command palette, keyboard shortcut, kbd, cmd k, docs
- Live preview: https://copy-css.com/snippets/command-search-bar
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```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

```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;
}
```

## Notes

- 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.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
