Lesson 7 of 7 · 5 min
Responsive design and motion
Mobile-first CSS, media and container queries, fluid widths, responsive images, and transitions and animations that respect people who prefer less motion.
On this page
A responsive page works on any screen without a separate mobile site. Most of it comes from fluid layouts (Grid, Flexbox, clamp()) and a few targeted media queries.
The viewport tag
Without this line in the <head>, phones pretend to be a desktop and shrink the page:
<meta name="viewport" content="width=device-width, initial-scale=1">
Mobile first
Write the base styles for small screens, then add rules as space grows. Small screens get the simplest CSS, and you only add complexity where it helps.
.layout {
display: grid;
gap: 2rem;
}
@media (min-width: 1000px) {
.layout {
grid-template-columns: minmax(0, 1fr) 16rem;
}
}
Pick breakpoints where your content breaks, not for specific devices.
A fluid container
.wrap {
width: min(100% - 2.5rem, 1040px);
margin-inline: auto;
}
This keeps a 1.25rem gutter on each side of a phone and caps the width at 1040px on a desktop, with no media query.
Media features beyond width
@media (prefers-color-scheme: dark) { /* the system is in dark mode */ }
@media (hover: hover) { /* a mouse or trackpad, not a touch screen */ }
@media print { .no-print { display: none; } }
Container queries
A media query asks about the screen. A container query asks about the element's parent, so a component can adapt to wherever it is placed:
.card-list { container-type: inline-size; }
@container (min-width: 32rem) {
.card { grid-template-columns: 10rem 1fr; }
}
The same card can stack in a narrow sidebar and sit side by side in a wide main column.
Responsive images
img {
max-width: 100%;
height: auto;
}
Let the browser pick the right file size with srcset:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 1000px) 50vw, 100vw"
alt="A description of the photo"
loading="lazy">
Transitions
A transition animates a property from one value to another when it changes, for example on hover:
.button {
transition: background-color 0.2s ease, transform 0.2s ease;
}
.button:hover { background-color: #0a0a0a; color: #fff; }
.button:active { transform: scale(0.98); }
Animate transform and opacity where you can. The browser can move those without recalculating the layout, so they stay smooth.
Keyframe animations
@keyframes rise {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: none; }
}
.hero h1 {
animation: rise 0.6s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}
Respect reduced motion
Some people get dizzy or distracted by movement and turn on "reduce motion" in their system settings. Honour it:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
Try it
Take the card grid from the last lesson. Add a hover transition that lifts a card with transform: translateY(-4px), add the reduced-motion rule, and test the page at 375px wide in the developer tools' device mode. You now have the core skills to build and style a full site.
More from CSS Crash Course
- Lesson 1 · 5 minHow CSS worksRules, selectors and declarations, three ways to add CSS to a page, and how the cascade and inheritance decide what you see.
- Lesson 2 · 5 minSelectors and specificityTarget exactly the elements you mean with type, class, attribute and pseudo-class selectors, and learn how the browser scores them.
- Lesson 3 · 5 minThe box modelEvery element is a box of content, padding, border and margin. Learn how they add up, how display changes the box, and the one reset every project needs.