Responsive Design Principles
Learning Objectives
- Understand responsive and adaptive design.
- Use mobile-first CSS.
- Create flexible layouts and responsive images.
- Test layouts across viewport sizes and input methods.
1. What Is Responsive Web Design?
Responsive web design allows a website's layout and content to adapt to different viewport sizes, orientations, and user needs. It combines fluid layouts, flexible media, and CSS media or container queries.
2. Add the Viewport Meta Tag
<meta name="viewport" content="width=device-width, initial-scale=1">This tells mobile browsers to use the device's CSS viewport width instead of a wider virtual layout viewport.
3. Mobile-First CSS
.container {
width: 100%;
padding: 16px;
}
.cards {
display: grid;
grid-template-columns: 1fr;
gap: 16px;
}
@media (min-width: 768px) {
.container {
max-width: 1100px;
margin-inline: auto;
}
.cards {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (min-width: 1100px) {
.cards {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}Mobile-first means writing the base styles for narrower layouts, then adding enhancements at wider viewport sizes.
4. Flexible Images
img, video {
max-width: 100%;
height: auto;
}
.hero-image {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}Flexible media should not overflow its container. Use object-fit when media must fill a defined box.
5. Avoid Fixed-Width Layouts
.content {
width: min(100% - 32px, 70rem);
margin-inline: auto;
}Prefer fluid widths and maximum constraints over hardcoded page widths that overflow on small screens.
6. Responsive Typography and Accessibility
h1 {
font-size: clamp(2rem, 5vw, 4rem);
}
button {
min-height: 44px;
}Fluid typography can scale with the viewport. Ensure text remains readable when zoomed and interactive controls are comfortably usable. The 44px example is a practical design target, not a universal CSS requirement.
7. Responsive Testing Checklist
- Test narrow and wide viewports.
- Check horizontal scrolling and clipped content.
- Test keyboard navigation, zoom and text resizing.
- Check landscape and portrait orientations.
- Test touch, mouse and keyboard interactions.
- Use browser DevTools device emulation and real devices when possible.
Practice Exercise
Build a responsive three-card page that uses one column on small screens, two columns on medium screens and three columns on larger screens. Add flexible images and fluid headings.