# Search

A search form in a `<search>` landmark with an `<input type="search">`.

_Diagram: Site search sits in the app bar as a search input with a Search button; a labelled search form can also head a page of results._

## Live example

### With a visible label

```html
<search aria-label="Guides">
  <form action="search.html" method="get">
    <label for="guide-search">Search the guides</label>
    <input id="guide-search" type="search" name="q" autocomplete="off" aria-describedby="guide-search-hint">
    <button type="submit">Search</button>
    <p id="guide-search-hint" data-field-hint>Try "tokens" or "dialog".</p>
  </form>
</search>
```

### Compact, label visually hidden

```html
<search aria-label="Site">
  <form action="search.html" method="get">
    <label for="site-search" class="visually-hidden">Search this site</label>
    <input id="site-search" type="search" name="q" autocomplete="off">
    <button type="submit" data-button="secondary">Search</button>
  </form>
</search>
```

## Attributes

| Attribute or value | Effect |
| --- | --- |
| `<search> > <form> with an <input type="search">` | no attribute needed: the label and any hint span the full width, the input and the submit button share the row below. |

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Enter</kbd> | in the field, submits the search |
| <kbd>Esc</kbd> | in the field, clears it (in browsers that support it) |
| <kbd>Tab</kbd> | moves from the field to the button |

## Accessibility

- `<search>` is a landmark. Label it only when there are several.
- The input needs a `<label>`, visually hidden if space is short. A placeholder is not a label ([WCAG 3.3.2](https://www.w3.org/WAI/WCAG22/Understanding/labels-or-instructions.html)).

## Specifications

- [HTML `<search>` element](https://html.spec.whatwg.org/multipage/grouping-content.html#the-search-element)
- [MDN `<input type="search">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/search)
- [MDN `<search>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/search)
- [W3C labels](https://www.w3.org/WAI/tutorials/forms/labels/)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- [Search dialog](https://domestic-ui.odonckers.dev/patterns/search-dialog.md): jump to a page or section from anywhere, with Cmd-K or Ctrl-K
- Source: [`css/components/molecules/search.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/search.css), [`examples/molecules/search.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/search.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Search fields](https://developer.apple.com/design/human-interface-guidelines/search-fields)
- [Material Design 3: Search](https://m3.material.io/components/search/overview)
- [Vercel Geist: Search Input](https://vercel.com/geist/search-input)
