CSS pseudo-classPseudo-classes

:active

Matches an element at the moment it’s activated, most often while the mouse button is pressed down on a button or link.

On this page

Example

#
CSS
.btn {
  box-shadow: 0 4px 0 #4c1d95;
  transition: transform 0.08s, box-shadow 0.08s;
}

.btn:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 #4c1d95;
}
Preview

Press the button and hold it: it slides down, and the shadow beneath it disappears, just like a real key.

Definition and usage

#

The :active pseudo-class lasts a very short time: from pressing the mouse button (or touching the screen) to releasing it. It’s the moment the button is pushed, which makes it perfect for a physical-press effect, such as a slight shrink or a downward shift.

It’s most often applied to buttons and links. For the user, it confirms that the click was noticed, even before the page reacts to it in any other way.

Rule order matters. :active should come after :hover, because during a click both states apply at once, and with equal specificity the later rule wins. Hence the well-known order for links: :link, :visited, :hover, :active.

Syntax

#
Syntax
selector:active { property: value; }

Press effects

#
  • scale(0.95)

    The button shrinks slightly, like a pressed key. Press it and hold.
  • inset box-shadow

    An inner shadow makes the button look like it’s sinking in.
  • brightness(0.8)

    A darkening effect at the moment of the click. Works with any background color.
  • :hover, then :active

    Hover first, then press. The :active rule sits lower, so during a click it wins over :hover.

Press effects in practice

#

scale(0.95)

The button shrinks slightly, like a pressed key. Press it and hold.
.demo:active { transform: scale(0.95); }

inset box-shadow

An inner shadow makes the button look like it’s sinking in.
.demo:active {
  box-shadow: inset 0 3px 8px rgb(0 0 0 / 0.5);
}

brightness(0.8)

A darkening effect at the moment of the click. Works with any background color.
.demo:active { filter: brightness(0.8); }

:hover, then :active

Hover first, then press. The :active rule sits lower, so during a click it wins over :hover.
.demo:hover { background: #6d28d9; }
.demo:active { background: #4c1d95; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Keep the :active effect short and subtle, with a transition of around 50 to 100 ms. A longer animation won’t have time to show, because the button is released quickly.
  • Place :active after :hover in the stylesheet; otherwise, with equal specificity, the click effect gets hidden.

Related entries

#

Spotted a mistake or missing an example? Write to us.