Interactive controls often change color or position when a pointer moves over them. A CSS transition makes that change gradual, giving a button a brief response to hover or keyboard focus without a keyframe animation.
The transition declaration controls how a property moves between two values; it does not create the changed state itself. Keeping it on the base selector lets the browser animate both entering and leaving the state, while naming individual properties avoids accidentally animating later layout changes.
Keyboard focus needs a visible indicator even when motion is unavailable. A separate outline identifies the focused button, and the prefers-reduced-motion media query can remove movement while retaining the change in color. Local HTML and CSS files are enough to try this behavior without JavaScript or a web server.
Steps to add a CSS transition:
- Create index.html in your text editor with the button markup and stylesheet link.
- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS transition</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>CSS transition</h1> <p>Hover over the button or focus it with Tab.</p> <button class="transition-button" type="button">Preview transition</button> </body> </html>
- Create styles.css in the same folder with the button's resting appearance and transition.
- styles.css
body { margin: 3rem; font-family: system-ui, sans-serif; } .transition-button { padding: 1rem 1.5rem; border: 0; border-radius: 0.5rem; color: white; background-color: #0f766e; font: inherit; transform: translateY(0); transition: background-color 300ms ease, transform 300ms ease; }
Each comma-separated transition names a property, its duration, and its timing function. Here, background-color and transform each take 300ms, or 0.3 seconds, with ease controlling the speed through the change. The declaration belongs on .transition-button so it also applies when hover or focus ends.
- Append the hover and keyboard-focus rules below the .transition-button rule in styles.css.
- styles.css
.transition-button:hover, .transition-button:focus-visible { background-color: #115e59; transform: translateY(-4px); } .transition-button:focus-visible { outline: 3px solid #b45309; outline-offset: 4px; }
The new background color and vertical offset are the transition's destination values. The outline appears immediately for keyboard focus because outline is not in the transition list.
Related: How to style keyboard focus with CSS - Append a reduced-motion override at the end of styles.css.
- styles.css
@media (prefers-reduced-motion: reduce) { .transition-button { transition: none; } .transition-button:hover, .transition-button:focus-visible { transform: none; } }
When the operating system requests reduced motion, the color changes instantly and the button stays in place. The keyboard outline remains visible.
Related: How to respect reduced motion with CSS - Save index.html and styles.css in your text editor.
- Open the saved index.html file in your browser.
The address starts with file:/// followed by the location of index.html on your computer.
- Move the pointer onto Preview transition.
The teal button darkens and rises by four pixels over 0.3 seconds. No click is needed.
- Move the pointer away from the button.
The darker, raised button returns smoothly to its original color and position. An immediate jump back usually means the transition was placed only on the hover rule.
- Press Tab to focus Preview transition.

- Confirm that the focused button has an amber outline and the darker, raised appearance.
The same transitioned values apply to hover and keyboard focus. If reduced motion is enabled, the button stays in place and changes color immediately instead.