Transitions and animations
W3C CSS Snapshot 2026
CSS can make a change happen gradually. A transition responds to a change in value. An animation follows a named sequence of steps.
On this page
Transition a changed value
Set transition on the base rule so it applies as a state starts and ends. The first value names the property; the second sets the duration. The hover or focus rule provides the new value.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Transition a changed value</title>
<style>
button { background: lightblue; transition: background-color 250ms; }
button:hover, button:focus-visible { background: gold; }
</style>
</head>
<body>
<button type="button">View product</button>
</body>
</html>
What to expect Hover or Tab to the button. The background changes gradually over a quarter of a second.
Define animation steps with @keyframes
@keyframes names a sequence and gives it values at points in time. from is the start and to is the end. animation connects the sequence to the element and sets a duration.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Define animation steps with @keyframes</title>
<style>
.notice { animation: appear 800ms; }
@keyframes appear { from { opacity: 0; } to { opacity: 1; } }
</style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>
What to expect Reload to see the first paragraph fade in once. The other paragraph is visible immediately.
Respect a reduced-motion preference
Some users set their system to reduce motion. The prefers-reduced-motion media feature can detect that preference. Remove an optional effect while leaving the content available.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Respect a reduced-motion preference</title>
<style>
.notice { animation: appear 1s; }
@keyframes appear { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .notice { animation: none; } }
</style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>
What to expect Enable reduced motion in the system or emulate it in DevTools and reload. The notice appears immediately.