Skip to content
IndexRequest a UIAboutContact

Search Input

A search field with a clear button, keyboard shortcut and expanding focus state.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Build a search input inside a form with role="search". It contains a magnifier SVG on the left, an input of type="search" with a visible-hidden label "Search components", a clear button (×) on the right that is hidden until the input has text, and a small "/" key hint chip at the right when empty. The field is 44px tall, 12px radius; at rest 240px wide and on focus it grows to 320px over 220ms with cubic-bezier(0.4,0,0.2,1) and shows an accent ring. Pressing the "/" key anywhere on the page (when not typing in a field) focuses the input. Escape clears the text if any, otherwise blurs. The clear button fades and scales in over 140ms, refocuses the input after clearing and has aria-label "Clear search". Under prefers-reduced-motion skip animation. Hide the hint on pointer: coarse devices. Hide the browser's native cancel button.

About this pattern

Definition

A text field specialised for queries: a leading magnifier icon, a clear button that appears only when there is text, and a keyboard shortcut hint. On focus the field widens slightly to give the query room.

Purpose & UX principles

  • Search is a primary navigation method; it should be reachable from anywhere with a shortcut (/ or Cmd/Ctrl+K).
  • The clear button saves selecting and deleting text, especially on touch.
  • Use type="search" and role="search" on the form so assistive technology finds it.

Interaction states

  • Idle, focused (wider, accent ring), has text (clear button visible), cleared.
  • Escape clears the field if it has text; otherwise it blurs.

Motion specification

  • Width: 220ms cubic-bezier(0.4, 0, 0.2, 1).
  • Clear button: fade and scale from 0.8, 140ms.
  • Reduced motion: width change is instant.

Responsive behaviour

  • Full width on phones; on desktop the field grows from 240px to 320px on focus.
  • Hide the shortcut hint on touch devices.

Code

HTML
<form class="sf" role="search" onsubmit="return false">
  <svg class="mag" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 4a6 6 0 104.5 10l5 5 1.5-1.5-5-5A6 6 0 0010 4z"/></svg>
  <label for="q" class="sr">Search components</label>
  <input id="q" type="search" placeholder="Search components" autocomplete="off">
  <kbd id="kbd">/</kbd>
  <button type="button" class="clr" id="clr" aria-label="Clear search" hidden>×</button>
</form>

Related patterns

Text Input

↗
Text InputInputs & Forms

Dropdown Menu

↗
Dropdown MenuOverlays & Dialogs

Validation States

↗
Validation StatesInputs & Forms