Responsive Web Design
Create websites that work on any device — mobile, tablet, desktop. Responsive design is not optional anymore.
Responsive design is an approach that makes web pages render well on all devices — phones, tablets, laptops, and desktops. It uses fluid grids, flexible images, and CSS media queries.
Why responsive matters: Over 60% of web traffic comes from mobile devices. Google ranks mobile-friendly sites higher. Users expect a perfect experience on any screen.
The viewport meta tag is essential for responsive design to work on mobile devices.
<!-- Required for all responsive websites -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
CSS media queries allow you to apply different styles based on device characteristics:
/* Mobile-first approach: base styles target mobile */
.grid {
display: grid;
grid-template-columns: 1fr; /* Single column on mobile */
gap: 1rem;
}
/* Tablet: 768px and above */
@media (min-width: 768px) {
.grid { grid-template-columns: repeat(2, 1fr); }
.header { font-size: 1.5rem; }
}
/* Desktop: 1024px and above */
@media (min-width: 1024px) {
.grid { grid-template-columns: repeat(3, 1fr); }
}
/* Large screens: 1200px and above */
@media (min-width: 1200px) {
.container { max-width: 1200px; margin: 0 auto; }
}
/* Common breakpoints for reference */
/* Mobile: 320px — 480px */
/* Tablet: 481px — 768px */
/* Laptop: 769px — 1024px */
/* Desktop: 1025px — 1440px */
min-width breakpoints to enhance for larger screens. This is simpler and better for performance than max-width (desktop-first).Use relative units instead of fixed pixels for flexible layouts:
%Parent elementWidth, height, paddingemParent font-sizePadding, margins relative to textremRoot font-size (16px default)Font sizes, spacing (most used!)vw1% of viewport widthFull-width sections, hero areasvh1% of viewport heightFull-height sections, overlayschWidth of "0" characterMax-width for readable text (65ch).container {
width: 90%; /* 90% of parent */
max-width: 1200px; /* But never wider than 1200px */
margin: 0 auto;
padding: 2rem; /* rem scales with user font size */
}
.hero {
min-height: 100vh; /* Full viewport height */
width: 100vw; /* Full viewport width */
display: flex;
align-items: center;
padding: 2rem;
}
/* Fluid layout with Flexbox + relative units */
@media (max-width: 768px) {
.columns { flex-direction: column; } /* Stack on mobile */
.column { width: 100%; }
}
/* Tip: Use clamp() for fluid sizes */
h1 { font-size: clamp(1.75rem, 4vw, 3rem); } /* Min 1.75rem, scales, max 3rem */Ensure images and media scale properly across devices:
/* Basic responsive image */
img { max-width: 100%; height: auto; display: block; }
/* Using srcset for different image sizes */
<img src="small.jpg"
srcset="small.jpg 480w,
medium.jpg 800w,
large.jpg 1200w"
sizes="(max-width: 600px) 480px,
(max-width: 1000px) 800px,
1200px"
alt="A responsive image">
/* Responsive video embed */
.video-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
}
/* Responsive background images */
.hero { background-image: url('hero-mobile.jpg'); }
@media (min-width: 768px) {
.hero { background-image: url('hero-tablet.jpg'); }
}
@media (min-width: 1024px) {
.hero { background-image: url('hero-desktop.jpg'); }
}Practice responsive design below:
Create responsive.html with the viewport meta tag.
Build a responsive page and resize the browser to test:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Practice</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: system-ui; }
.container { width: 90%; max-width: 1200px; margin: 0 auto; padding: 2rem 0; }
.header { background: #f97316; color: white; padding: 1.5rem; text-align: center; border-radius: 8px; margin-bottom: 1rem; }
.grid { display: grid; grid-template-columns: 1fr; gap: 1rem; }
.card { background: #f3f4f6; padding: 1.5rem; border-radius: 8px; }
@media (min-width: 768px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1024px) { .grid { grid-template-columns: repeat(3, 1fr); } }
</style>
</head>
<body>
<div class="container">
<div class="header">🍛 Responsive Page</div>
<div class="grid">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
</div>
</body>
</html>
Try these experiments:
Done — Key Points:
- ✅ Always include
<meta name="viewport" content="width=device-width, initial-scale=1.0">in your HTML. - ✅ Use mobile-first approach — base styles for mobile,
min-widthbreakpoints for larger screens. - ✅ Use relative units:
%for widths,remfor spacing/fonts,vw/vhfor full-screen sections. - ✅ Common breakpoints: 768px (tablet), 1024px (desktop), 1200px (large). Base breakpoints on content, not devices.
- ✅ Make images responsive:
max-width: 100%; height: auto;. Usesrcsetfor art direction. - ✅
clamp(min, preferred, max)creates fluid typography that scales between bounds.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login