Lesson 16 / Motion
CSS Transitions
Use CSS transitions to create smooth state changes, hover effects, menu movement, and polished interface feedback.
Teacher Notes / In-Class Use
Demo Live
- Walk through the interactive demo before students start changing their own project files.
- Connect the demo back to the first goal: Explain the core properties that make a CSS transition work
Try In Class
- Create a button hover state using color and transform transitions.
- Have students make one visible change, save, refresh, and explain what changed.
Submit Or Check
- Ask students to show the work in the browser, not only in the editor.
- Have students commit their progress with a clear message when the checkpoint is stable.
Watch For
- Students copying code without checking file paths, spelling, or capitalization.
- Visual changes that work locally but break when the project is published.
Learning Goals
- Explain the core properties that make a CSS transition work
- Create transitions with shorthand and individual properties
- Apply transitions in ways that respect accessibility and performance
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Change the transitioned property, duration, delay, and easing.
- Trigger the demo after each setting change and compare the feel.
What Changes
- The motion changes without changing the layout structure.
- The code sample updates with the transition settings.
What To Notice
- Short, focused transitions usually feel better than slow decorative motion.
- Opacity and transform are safer animation targets than layout-heavy properties.
Apply It
- Add one small hover or focus transition to a button or link in your project.
Interactive Demo
Transition Timing Playground
Change the timing values, then trigger the preview. Transitions are about how a change feels between two states.
duration: 400ms / delay: 0ms / easing: ease
.box {
transition: transform 400ms ease 0ms;
}
.box.is-active {
transform: translateX(120px) scale(1.1);
} What to notice
- Duration changes how long the transition takes.
- Delay changes when the transition begins.
- Easing changes the personality of the motion.
Try this
- Compare linear with ease-out.
- Add a delay and notice how the interface feels less immediate.
- Use transform or opacity for smoother motion.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).
Introduction
CSS transitions provide a smooth and visually appealing way to change the properties of elements over time.
By understanding the fundamentals of transitions, you can create dynamic and interactive user interfaces.
This guide explores the key concepts of CSS transitions, provides practical examples, and offers strategies for creating effective and polished effects.
Benefits of Using CSS Transitions
- Improved aesthetics: A button that subtly changes color on hover provides instant feedback and enhances design.
- Enhanced user experience: A navigation menu that slides into view feels more intuitive than an abrupt appearance.
- Improved perceived performance: Smooth fade-ins and fade-outs for loading indicators can make interactions feel faster.
Understanding CSS Transitions
Transitions rely on several properties that define what changes, how long the change takes, how it moves, and when it begins.
- transition-property: Specifies the CSS property to transition.
- transition-duration: Sets how long the transition takes.
- transition-timing-function: Defines the speed curve of the transition, such as ease-in-out.
- transition-delay: Adds a delay before the transition begins.
Creating Transitions
The transition shorthand is the most common way to define a transition.
.transition-example {
transition: opacity 2s ease-in-out;
} Individual Properties
You can also write each transition property separately when you want the pieces to be more explicit.
.transition-example {
transition-property: opacity;
transition-duration: 2s;
transition-timing-function: ease-in-out;
} Fading In and Out an Element
Opacity transitions are useful for showing and hiding interface elements without an abrupt visual jump.
.fade {
opacity: 0;
transition: opacity 200ms ease;
}
.fade.is-visible {
opacity: 1;
} Hover Effects
Hover transitions can make links, buttons, and cards feel responsive. Keep them quick and focused.
.button {
background-color: #2f9e44;
transform: translateY(0);
transition: background-color 180ms ease, transform 180ms ease;
}
.button:hover {
background-color: #237a35;
transform: translateY(-2px);
} Menu Animations
Menus can use transform and opacity to slide or fade into view while avoiding expensive layout changes.
.menu {
opacity: 0;
transform: translateY(-0.5rem);
transition: opacity 200ms ease, transform 200ms ease;
}
.menu.is-open {
opacity: 1;
transform: translateY(0);
} Accordion Effects
Accordions are common UI patterns for revealing content. Transitions can help the change feel connected to the user action.
.accordion-panel {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 240ms ease;
}
.accordion-panel.is-open {
grid-template-rows: 1fr;
}
.accordion-content {
overflow: hidden;
} Advanced Techniques
Transitions can be applied to multiple properties, used with JavaScript, or managed using transition groups in frameworks.
.element {
transition: transform 0.3s ease-in-out, background-color 0.3s ease-in-out;
}
.element:hover {
transform: scale(1.2);
background-color: #f0f0f0;
} Transitions with JavaScript
JavaScript can change classes or inline styles that trigger CSS transitions. In most projects, toggling a class is easier to maintain than writing many inline styles.
const element = document.getElementById('myElement');
element.addEventListener('click', () => {
element.style.transform = 'scale(1.2)';
element.style.transition = 'transform 0.2s ease-in-out';
});
element.addEventListener('mouseout', () => {
element.style.transform = 'scale(1)';
}); Accessibility and Performance
Use @media (prefers-reduced-motion: reduce) to respect user motion preferences.
Limit transition use for performance, favoring properties like opacity and transform.
@media (prefers-reduced-motion: reduce) {
.element {
transition: none;
}
} Custom Timing Functions
Custom timing functions using cubic-bezier() provide control over transition speed curves.
Use online tools to create and visualize Bezier curves.
.hover-element {
transition: background-color 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.element {
transition: transform 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
} CSS Transition Libraries
- Hover.css: Pre-built hover effects using transitions.
- Animate.css: A wide range of animation classes, many of which include transitions.
- Animista: Visual tool for generating custom CSS animations and transitions.
Conclusion
CSS transitions offer a powerful tool for enhancing user experience.
By understanding their mechanics and thoughtfully applying them, you can craft responsive and engaging interfaces that delight users without sacrificing performance or accessibility.
Checkpoint
Before moving on, make sure these feel true.
- I can explain the main concept in my own words.
- I can apply this lesson to my current project.
- I can verify the result in the browser.
- I can commit the change with a clear message.
Project Connection
This lesson supports current class projects.
Practice
- Create a button hover state using color and transform transitions.
- Build a menu or accordion that opens with a transition.
- Add a prefers-reduced-motion fallback.
- Try one custom cubic-bezier timing function and explain how it changes the feel of the interaction.
Resources
- MDN CSS Transitions Web Docs
- W3Schools CSS transitions