Open original
Info
Demo 2.11

Basic interactivity using :hover and :active states

A pseudo-class is a condition you attach to a selector with a colon. The element gets those styles only while the condition is true ~ no JavaScript involved.

The ones you’ll use

  • :hover ~ the pointer is over it.
  • :active ~ the mouse button is currently held down.
  • :focus-visible ~ the element has keyboard focus.
  • :disabled, :checked ~ for form controls.

Make it a fade, not a snap

.btn {
	transition: background 0.15s;
}
.btn:hover {
	background: crimson;
}

The transition goes on the normal state, not the hover state. Then it animates in both directions ~ on the way in and on the way out.

Please keep your focus styles

Browsers draw an outline around whatever has keyboard focus. It is very tempting to write outline: none because it doesn’t match your design.

If you do that, anybody navigating with a keyboard ~ which includes people who can’t use a mouse ~ has no idea where they are on your page. Style it instead:

.btn:focus-visible {
	outline: 3px solid navy;
	outline-offset: 2px;
}

:focus-visible only fires for keyboard focus, so mouse users never see it. There’s no reason left to remove it.

Hover doesn’t exist on phones

There’s no pointer on a touchscreen. Never hide anything important behind :hover ~ treat it as a bonus for mouse users, not a way to reveal content.

Next demoAnimation using <code>@keyframes</code>
Keyboard Shortcuts
⌘ Z / ⌘ ⇧ ZCtrl Z / Ctrl ⇧ Z Undo / redo
Tab / ⇧ TabTab / ⇧ Tab Indent / unindent selection
⌘ ] / ⌘ [Ctrl ] / Ctrl [ Indent / unindent lines
⌘ ACtrl A Select all
⌘ DCtrl D Select next occurrence of selection
⌘ /Ctrl / Comment / uncomment lines
Ctrl SpaceCtrl Space Autocomplete
Ctrl EnterCtrl Enter Reset layout
⌘ ⌥ [ / ⌘ ⌥ ]Ctrl ⇧ [ / Ctrl ⇧ ] Fold / unfold block
Code
The editor didn't load — try refreshing the page. Navigation, shortcuts and the info panel still work in the meantime.
Preview
Dimensions0 × 0
Delay
Console