CSS Search with Recent Items
A search field that opens a panel of recent searches while it has focus, using :focus-within and no JavaScript.
<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>
.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;
}
About this CSS Search with Recent Items
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. 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:
#e2e8f0#fff#0f172a#6366f1#94a3b8#334155#eef2ff#4338ca
How to use this search with recent items
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.search-recentclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein 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.