A lot of “finished” pages are really just the default state. You hover a button and nothing happens. You tab to an input and you can’t tell where you are. It works, but it feels dead.
That’s what this lesson fixes.
Pseudo-classes: the states of an element
.btn { /* normal */ }
.btn:hover { /* mouse over */ }
.btn:focus-visible { /* keyboard focus */ }
.btn:active { /* being pressed */ }
.btn:disabled { /* can't be used */ }
A few more you’ll use:
:checked— a checked checkbox or radio.:invalid— a form field that fails validation.:first-child/:last-child— position in a group.:nth-child(odd)— zebra striping, for example.
Why :focus-visible instead of :focus? Because it only shows the ring when the browser thinks focus should be visible — usually keyboard navigation, not a mouse click. It’s the best of both worlds.
Transitions
A transition animates a property between two values:
.btn {
transition:
background-color 0.15s ease,
transform 0.15s ease,
box-shadow 0.15s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 18px -10px rgba(34, 28, 70, 0.5);
}
.btn:active {
transform: translateY(1px);
box-shadow: none;
}
- Keep transitions short: 120–250ms. Long transitions feel laggy.
- Animate
transformandopacitywhen you can. They’re cheap. Animatingwidth,height, ortopcan jank. - Avoid
transition: all. It transitions things you didn’t mean to, including layout properties.
Inputs and validation
input:focus-visible {
border-color: #4f46e5;
box-shadow: 0 0 0 4px #eef0ff;
outline: none; /* replaced by the ring above */
}
input:invalid:not(:placeholder-shown) {
border-color: #fb7185;
}
The :not(:placeholder-shown) part waits until the user has typed something, so the field isn’t red before they start. It’s a small trick that makes validation feel less aggressive.
Disabled and loading states
.btn:disabled {
opacity: 0.55;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
A disabled button should look disabled. Don’t just leave it looking clickable and wonder why people keep clicking it.
Reduced motion
Some people get motion sickness from animations. Respect their setting:
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
This is one of the few places !important is acceptable. You’re overriding every animation on purpose.
Try it
Hover the button, press it, tab to it, and try the disabled one. Then tab into the input.
Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.
Polish the calculator
the plan's verify stepAdd hover, active, focus-visible, and disabled states to your calculator button and inputs. Add a short transition to the button and the result card. Then tab through the form and make sure you can always see where focus is.
Turn on 'Reduce motion' in your OS accessibility settings and reload — the transitions should stop.
The mistakes everyone makes
outline: nonewith no replacement. Invisible keyboard focus.transition: all. It transitions everything, including things you didn’t plan.- Animating width/height. It forces layout. Use transform and opacity.
- Hover-only interactions. Touch screens don’t hover. Critical actions need a click/tap path.
- No disabled styling. A disabled button should look disabled.
- Too much motion. If everything moves, nothing feels special.
- Ignoring reduced motion. It’s a real accessibility setting, not a niche one.
Interactive elements need hover, focus-visible, active, and disabled states.
Never remove focus outlines without a visible replacement.
Transitions should be short (120–250ms) and animate transform/opacity when possible.
Style disabled states so they look disabled.
Respect prefers-reduced-motion.
Next: the CSS checkpoint — we’ll put the layout, tokens, states, and responsive rules together and make the calculator actually look like something.
Up next
CSS checkpoint: make it look real
Put the whole styling system together: tokens, layout, form fields, result cards, a food log table, responsive rules, and states. This is the page JavaScript will bring to life.