# Search with Recent Items

> A search field that opens a panel of recent searches while it has focus, using :focus-within and no JavaScript.

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: search, recent searches, dropdown, focus-within, suggestions, panel
- Live preview: https://copy-css.com/snippets/search-recent-dropdown
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<form class="search-recent" role="search">
  <label class="search-recent-sr" for="search-recent-q">Search</label>
  <input id="search-recent-q" type="search" name="q" placeholder="Search docs…" autocomplete="off">
  <div class="search-recent-panel">
    <p>Recent</p>
    <a href="#">container queries</a>
    <a href="#">grid template areas</a>
    <a href="#">color-mix()</a>
  </div>
</form>
```

## CSS

```css
.search-recent {
  position: relative;
  width: 260px;
  font-family: system-ui, sans-serif;
}

.search-recent-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.search-recent input {
  box-sizing: border-box;
  width: 100%;
  padding: 11px 14px 11px 40px;
  border: 1.5px solid #e2e8f0;
  border-radius: 12px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' 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 13px center / 17px;
  color: #0f172a;
  font: 15px system-ui, sans-serif;
  outline: none;
}

.search-recent input:focus {
  border-color: #6366f1;
}

.search-recent-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: 0;
  z-index: 10;
  display: grid;
  padding: 6px;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 16px 32px -12px rgba(15, 23, 42, 0.25);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
}

.search-recent:focus-within .search-recent-panel {
  opacity: 1;
  visibility: visible;
  transform: none;
}

.search-recent-panel p {
  margin: 4px 8px;
  color: #94a3b8;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.search-recent-panel a {
  padding: 7px 8px;
  border-radius: 8px;
  color: #334155;
  font-size: 14px;
  text-decoration: none;
}

.search-recent-panel a:hover,
.search-recent-panel a:focus-visible {
  background: #eef2ff;
  color: #4338ca;
  outline: none;
}
```

## Notes

- This CSS Search with Recent Items is 77 lines of pure CSS (1.7 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and clip paths.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
