Efficient visual effects and motion
Some visual changes require more browser work than others. Understanding the likely cost helps you build smooth interactions and then verify them with profiling.
✓ What you will learn in this topic
- Explain the key idea: Prefer measured, restrained effects and avoid speculative performance hacks.
- Apply the concept: The card moves slightly and changes shadow without animating its dimensions.
- Avoid this common mistake:
Concept explanation
Animating width or top can trigger layout work. Simple transforms and opacity often avoid repeated layout recalculation, though actual performance depends on the page and device. Large blur filters and many layered shadows can also be costly.
Do not add will-change as a blanket fix; it can consume resources. Profile a real interaction and use the smallest effective change.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
transition: transform 160ms ease, opacity 160ms ease;
}
.demo:hover {
transform: translateY(-2px);
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
transition: transform 160ms ease, opacity 160ms ease;
}
.demo:hover {
transform: translateY(-2px);
}Expected result: The card moves slightly and changes shadow without animating its dimensions.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card {
transition: transform 180ms ease, box-shadow 180ms ease;
}
.card:hover {
transform: translateY(-2px);
box-shadow: 0 0.75rem 1.5rem rgb(15 23 42 / 14%);
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Optimizing selector speed based on myths while ignoring oversized assets, layout shifts, and expensive visual effects.
Best practice
Measure real user impact, remove unused CSS, reserve media dimensions, and prefer efficient motion properties.
Practice exercise
Audit a page for unused styles, unreserved image space, and animations of layout properties; propose measured improvements. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Prefer measured, restrained effects and avoid speculative performance hacks.
- Measure real user impact, remove unused CSS, reserve media dimensions, and prefer efficient motion properties.
Knowledge check
Which is a better initial animation candidate than changing width?
Reveal the explanation
These properties often avoid layout recalculation; always verify the actual page on target devices.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
transition: transform 160ms ease, opacity 160ms ease;
}
.demo:hover {
transform: translateY(-2px);
}
Expected result:
The card moves slightly and changes shadow without animating its dimensions.
Hands-on practice task
Audit a page for unused styles, unreserved image space, and animations of layout properties; propose measured improvements.
In this section
Other lessons in Performance Optimization
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.