Responsive Web Design
Build layouts that adapt to different viewport sizes, devices, input modes, and user preferences.
Responsive Design Principles
Responsive design means creating interfaces that adapt to available space and user conditions rather than designing separate fixed pages for every device.
Fluid layouts, flexible images, relative units, Grid, Flexbox, intrinsic sizing, and media queries work together to create resilient interfaces.
A good responsive design is not simply a desktop layout that becomes smaller. Content hierarchy and interaction patterns may need to change.
.container {
width: min(100% - 2rem, 1200px);
margin-inline: auto;
}- Responsive design adapts to constraints rather than specific device names.
- Fluid sizing often reduces the need for many breakpoints.
What is the main goal of responsive design?
Media Queries
Media queries apply styles based on environmental conditions such as viewport width, orientation, resolution, color scheme, reduced motion preference, and input capabilities.
Modern responsive design often uses content-driven breakpoints instead of copying popular device widths.
.navigation {
display: flex;
}
@media (max-width: 700px) {
.navigation {
flex-direction: column;
}
}- Choose breakpoints based on content needs.
- Media queries can test many environmental features beyond width.
What does a media query allow CSS to do?
Mobile-First CSS
Mobile-first CSS starts with the constrained layout and progressively enhances it for larger screens. This often results in simpler base styles and clearer media queries.
Mobile-first does not mean mobile devices are more important. It means the stylesheet starts with the smallest practical layout constraints.
.dashboard {
display: grid;
grid-template-columns: 1fr;
}
@media (min-width: 800px) {
.dashboard {
grid-template-columns: 240px 1fr;
}
}- Start with a resilient constrained layout.
- Use
min-widthqueries for progressive enhancement.
What is the typical direction of mobile-first responsive CSS?
Container Queries
Container queries allow a component to respond to the size of its containing element rather than the viewport. This is particularly valuable for reusable components that can appear in different parts of an application.
A card can therefore change its layout based on the width of the card's container rather than the entire browser window.
.card-list {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 500px) {
.card {
grid-template-columns: 160px 1fr;
}
}- Container queries make components more context-aware.
- They reduce coupling between components and viewport breakpoints.
What is the major advantage of container queries?
Responsive Images and Object Fit
CSS controls how replaced elements such as images and videos fit within their boxes. object-fit and object-position are useful for cards, avatars, thumbnails, and media galleries.
Responsive images also involve HTML features such as srcset and sizes; CSS alone cannot solve every image-delivery problem.
.avatar {
width: 64px;
aspect-ratio: 1;
object-fit: cover;
object-position: center;
border-radius: 50%;
}object-fit: cover fills the box while preserving the image's aspect ratio and cropping excess content.object-fitcontrols replaced-content fitting.- Responsive image delivery should also use appropriate HTML image attributes.
What does `object-fit: cover` do?