Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

CSS Animations and Transitions: Smooth, Fast and Accessible Motion

5 min read Updated By the SkillNewton team

What are CSS animations?

CSS animations change an element's styles over time without JavaScript. A transition animates between two states when a property changes, for example on hover. A @keyframes animation runs a defined sequence of steps that can start on its own, repeat and alternate. Animating transform and opacity keeps motion smooth.

Transitions versus @keyframes animations

Transition@keyframes animation
Starts whenA property value changes (hover, focus, class added)The animation is applied, or on load
StepsTwo states: from and toAny number of keyframes
RepeatsNoYes, with animation-iteration-count
Typical useHover effects, menus opening, colour changesLoading spinners, attention cues, entrance effects

Reach for a transition first. Most interface motion is a response to a state change, and a transition handles it with one line. Use keyframes when the motion needs more than two states or must run without a trigger.

How transitions work

A transition is declared on the element in its normal state. Whenever a listed property changes, the browser animates from the old value to the new one.

.button {
  background: #0b57d0;
  transform: translateY(0);
  transition: background-color 200ms ease, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
  background: #0842a0;
  transform: translateY(-2px);
}
PropertyControls
transition-propertyWhich properties animate; list them rather than using all
transition-durationHow long it takes, in s or ms
transition-timing-functionThe speed curve: ease, linear, ease-in-out, cubic-bezier(), steps()
transition-delayWait before starting

Listing properties explicitly avoids surprises: with transition: all, any later change, such as a width change at a breakpoint, animates too. Apply the same effect to :focus-visible as to :hover so keyboard users get the same feedback.

Building @keyframes animations

Define the steps once with @keyframes, then attach them to any element with the animation properties.

@keyframes spin {
  to { transform: rotate(360deg); }
}
@keyframes fade-up {
  from { opacity: 0; transform: translateY(1rem); }
  to   { opacity: 1; transform: translateY(0); }
}

.spinner {
  width: 2rem; height: 2rem;
  border: 3px solid #ccc; border-top-color: #0b57d0; border-radius: 50%;
  animation: spin 800ms linear infinite;
}
.card {
  animation: fade-up 400ms ease-out both;
}
.card:nth-child(2) { animation-delay: 100ms; }
.card:nth-child(3) { animation-delay: 200ms; }
PropertyControls
animation-nameWhich @keyframes to run
animation-durationLength of one cycle
animation-iteration-countA number or infinite
animation-directionnormal, reverse, alternate
animation-fill-modeWhether the first or last keyframe styles apply before or after running; both covers both
animation-play-staterunning or paused

Without animation-fill-mode: both, the delayed cards above would show at full opacity, then jump to invisible when their animation starts. To start or restart an animation from JavaScript, add or remove a class, as shown in the JavaScript events guide.

Performance: why transform and opacity are fast

Each frame, the browser may need to do three kinds of work: layout (working out sizes and positions), paint (drawing pixels) and composite (combining layers). Changing width, height, top, left or margin triggers layout for the element and often its neighbours, on every frame. Changing transform or opacity can usually be handled at the compositing stage, often on the GPU, so the animation stays smooth even when the main thread is busy.

Instead of animatingAnimate
left / toptransform: translate()
width / heighttransform: scale()
visibility flicker or displayopacity
box-shadow size on hoveropacity of a pseudo-element that already has the larger shadow

will-change: transform hints that an element is about to animate so the browser can prepare a layer. Use it sparingly on the few elements that need it, because each extra layer uses memory. Watch the Performance panel in dev tools for long frames and layout work during an animation. The web performance guide explains how this links to responsiveness metrics.

Accessible motion and reduced motion settings

Motion can cause dizziness, nausea or headaches for people with vestibular disorders, and constant movement distracts many readers. Operating systems offer a "reduce motion" setting, and CSS exposes it through the prefers-reduced-motion media feature.

/* Motion only for people who have not asked to reduce it */
@media (prefers-reduced-motion: no-preference) {
  .card { animation: fade-up 400ms ease-out both; }
  html  { scroll-behavior: smooth; }
}

/* Or tone existing motion down */
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }   /* slower, but still shows progress */
  .hero-slide { animation: none; }
}
  • Reduced motion does not mean no feedback: keep colour and opacity changes, and remove large movement, zooming and parallax.
  • WCAG requires a way to pause, stop or hide anything that moves automatically for more than five seconds alongside other content.
  • Never make content flash more than three times in any one second.
  • Keep interface transitions short, roughly 150 to 300 milliseconds, so the interface feels responsive rather than slow.

The media queries guide covers this and other preference features in more detail.

Adding an animation step by step

  1. Decide what the motion communicates, such as feedback, a state change or progress; skip motion that has no purpose.
  2. Use a transition if there are two states, or @keyframes if there are more or it must repeat.
  3. Animate transform and opacity wherever possible.
  4. Choose a short duration and an easing curve; ease-out suits elements entering the screen.
  5. Wrap large or decorative motion in a prefers-reduced-motion query.
  6. Test with reduced motion emulated in dev tools and check for dropped frames in the Performance panel.

Practical exercise: animate a notification toast

Build a small "Saved" notification that slides in from the bottom of the screen, using the styling basics from our CSS guide.

  1. Position the toast at the bottom of the viewport and hide it with opacity: 0 and transform: translateY(150px).
  2. Add a .is-visible class that sets opacity: 1 and transform: none, with a 250ms transition on both properties.
  3. Add a button that toggles the class with JavaScript, and give the toast role="status" so screen readers announce it.
  4. Create a @keyframes pulse for an icon inside the toast that runs twice and stops.
  5. Under prefers-reduced-motion: reduce, keep the fade but remove the slide and the pulse.

You are done when the toast moves smoothly with no layout work in the Performance panel, fades without sliding when reduced motion is emulated, and is announced by a screen reader. Adding polish like this to real project pages is part of SkillNewton's PHP foundation course.

Common mistakes

  • Animating width, height, top or left, which forces layout on every frame and causes jank.
  • Using transition: all, so unrelated property changes animate unexpectedly.
  • Ignoring prefers-reduced-motion for large, looping or parallax effects.
  • Adding will-change to many elements, which wastes memory instead of helping.
  • Making interface transitions so long that the site feels sluggish to use.
  • Forgetting animation-fill-mode, so elements jump before a delayed animation starts or after it ends.

Frequently asked questions

Can CSS transitions animate display: none?

Traditionally no, because display is not interpolated. Current browsers support transition-behavior: allow-discrete together with @starting-style for this, but fading opacity and toggling visibility remains a simple, well-supported approach.

Should I use CSS or JavaScript for animation?

Use CSS for state changes and simple sequences. Use JavaScript, such as the Web Animations API with element.animate(), when motion depends on user input, must be paused or reversed precisely, or needs values calculated at runtime.

What does the steps() timing function do?

It splits the animation into a fixed number of jumps instead of a smooth curve, which suits sprite-sheet animations or a typewriter effect where characters appear one at a time.

Do animations affect accessibility audits?

Yes. Auto-playing motion without a pause control, or flashing content, can fail WCAG success criteria. Our accessibility guide explains how to audit a page.

Want to Learn This Properly?

The PHP Web Development: Foundation & Practical Implementation (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.