Duration: 75 minutes | Difficulty: Intermediate | Prerequisites: Lessons 1-6 completed
By the end of this lesson, you will be able to:
Responsive design ensures your website looks and works well on all devices - from mobile phones to desktop computers. Itโs about creating flexible layouts that adapt to different screen sizes and orientations.
/* Percentage - relative to parent element */
.container { width: 80%; }
/* em - relative to element's font size */
.text { margin: 1em; } /* 16px if font-size is 16px */
/* rem - relative to root font size */
.heading { font-size: 2rem; } /* 32px if root is 16px */
/* Viewport width/height */
.hero { height: 100vh; } /* 100% of viewport height */
.sidebar { width: 25vw; } /* 25% of viewport width */
/* Minimum/maximum of vw and vh */
.text { font-size: 4vmin; } /* 4% of smaller viewport dimension */
.banner { height: 50vmax; } /* 50% of larger viewport dimension */
/* Clamp: min, preferred, max */
h1 { font-size: clamp(1.5rem, 4vw, 3rem); }
/* Min/Max */
.container { width: min(90%, 1200px); }
.sidebar { width: max(200px, 20%); }
/* Calc for mixed units */
.content { width: calc(100% - 250px); }
/* Apply styles when screen is 768px or wider */
@media screen and (min-width: 768px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
}
/* Multiple conditions */
@media screen and (min-width: 768px) and (max-width: 1024px) {
.tablet-specific {
display: block;
}
}
/* Mobile First Approach */
/* Base styles for mobile (320px+) */
.container {
padding: 1rem;
width: 100%;
}
/* Small tablets (576px+) */
@media (min-width: 36em) {
.container {
padding: 1.5rem;
}
}
/* Tablets (768px+) */
@media (min-width: 48em) {
.container {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 2rem;
max-width: 1024px;
margin: 0 auto;
}
}
/* Desktop (1024px+) */
@media (min-width: 64em) {
.container {
max-width: 1200px;
padding: 2rem;
}
}
/* Large desktop (1200px+) */
@media (min-width: 75em) {
.container {
max-width: 1400px;
}
}
/* Orientation */
@media (orientation: landscape) {
.hero { height: 70vh; }
}
@media (orientation: portrait) {
.hero { height: 50vh; }
}
/* High DPI displays */
@media (min-resolution: 2dppx) {
.logo {
background-image: url('logo@2x.png');
background-size: contain;
}
}
/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* Dark mode preference */
@media (prefers-color-scheme: dark) {
body {
background: #1a1a1a;
color: #ffffff;
}
}
<nav class="navbar">
<div class="nav-container">
<div class="nav-brand">
<a href="#home">Brand</a>
</div>
<button class="nav-toggle" aria-label="Toggle navigation">
<span></span>
<span></span>
<span></span>
</button>
<ul class="nav-menu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</nav>
.navbar {
background: #ffffff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
padding: 0 1rem;
display: flex;
align-items: center;
justify-content: space-between;
}
.nav-brand a {
font-size: 1.5rem;
font-weight: bold;
color: #2563eb;
text-decoration: none;
}
.nav-toggle {
display: none;
flex-direction: column;
background: none;
border: none;
cursor: pointer;
padding: 0.5rem;
}
.nav-toggle span {
width: 25px;
height: 3px;
background: #374151;
margin: 3px 0;
transition: 0.3s;
}
.nav-menu {
display: flex;
list-style: none;
margin: 0;
padding: 0;
gap: 2rem;
}
.nav-menu a {
color: #374151;
text-decoration: none;
font-weight: 500;
padding: 1rem 0;
transition: color 0.2s;
}
.nav-menu a:hover {
color: #2563eb;
}
/* Mobile styles */
@media (max-width: 768px) {
.nav-toggle {
display: flex;
}
.nav-menu {
position: fixed;
left: -100%;
top: 70px;
flex-direction: column;
background-color: white;
width: 100%;
text-align: center;
transition: 0.3s;
box-shadow: 0 10px 27px rgba(0,0,0,0.05);
gap: 0;
}
.nav-menu.active {
left: 0;
}
.nav-menu li {
border-bottom: 1px solid #eee;
}
.nav-menu a {
display: block;
padding: 1.5rem 1rem;
}
}
<section class="cards-section">
<div class="container">
<h2>Our Services</h2>
<div class="cards-grid">
<div class="card">
<div class="card-icon">๐จ</div>
<h3>Design</h3>
<p>Beautiful, user-centered design solutions.</p>
</div>
<div class="card">
<div class="card-icon">๐ป</div>
<h3>Development</h3>
<p>Modern, responsive web development.</p>
</div>
<div class="card">
<div class="card-icon">๐ฑ</div>
<h3>Mobile</h3>
<p>Native and progressive web apps.</p>
</div>
<div class="card">
<div class="card-icon">๐</div>
<h3>Performance</h3>
<p>Fast, optimized user experiences.</p>
</div>
</div>
</div>
</section>
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 1rem;
}
.cards-section {
padding: 4rem 0;
}
.cards-section h2 {
text-align: center;
margin-bottom: 3rem;
font-size: clamp(1.5rem, 4vw, 2.5rem);
}
.cards-grid {
display: grid;
gap: 2rem;
/* Default: 1 column for mobile */
grid-template-columns: 1fr;
}
.card {
background: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.07);
text-align: center;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}
.card-icon {
font-size: 3rem;
margin-bottom: 1rem;
}
.card h3 {
margin-bottom: 1rem;
color: #1f2937;
}
.card p {
color: #6b7280;
line-height: 1.6;
}
/* Tablet: 2 columns */
@media (min-width: 640px) {
.cards-grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop: 4 columns */
@media (min-width: 1024px) {
.cards-grid {
grid-template-columns: repeat(4, 1fr);
}
}
/* Fluid typography system */
:root {
--font-size-sm: clamp(0.875rem, 0.8rem + 0.2vw, 1rem);
--font-size-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
--font-size-lg: clamp(1.125rem, 1rem + 0.625vw, 1.5rem);
--font-size-xl: clamp(1.25rem, 1.1rem + 0.75vw, 1.875rem);
--font-size-2xl: clamp(1.5rem, 1.2rem + 1.5vw, 2.25rem);
--font-size-3xl: clamp(1.875rem, 1.5rem + 1.875vw, 3rem);
}
body {
font-size: var(--font-size-base);
line-height: 1.6;
}
h1 { font-size: var(--font-size-3xl); }
h2 { font-size: var(--font-size-2xl); }
h3 { font-size: var(--font-size-xl); }
h4 { font-size: var(--font-size-lg); }
.lead {
font-size: var(--font-size-lg);
}
.small {
font-size: var(--font-size-sm);
}
/* Responsive line heights */
@media (max-width: 640px) {
h1, h2, h3 {
line-height: 1.2;
}
p {
line-height: 1.7;
}
}
/* Responsive images */
img {
max-width: 100%;
height: auto;
}
/* Picture element for art direction */
.hero-image {
width: 100%;
height: 50vh;
object-fit: cover;
}
@media (min-width: 768px) {
.hero-image {
height: 70vh;
}
}
/* CSS Grid responsive image gallery */
.image-gallery {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
.gallery-item img {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 8px;
}
/* Container queries (when supported) */
@container (min-width: 400px) {
.card {
display: flex;
align-items: center;
}
.card-content {
flex: 1;
}
}
em, rem, and px?min-width vs max-width in media queries?clamp(1rem, 2vw, 3rem) do?In Lesson 8, weโll explore CSS Transitions & Animations, learning how to add smooth transitions, keyframe animations, and interactive effects that enhance user experience.
Responsive design is essential in todayโs multi-device world. Master these techniques to create websites that work beautifully everywhere.