CSS

Lesson 7: Responsive Design

Duration: 75 minutes | Difficulty: Intermediate | Prerequisites: Lessons 1-6 completed

๐Ÿ“‹ Learning Objectives

By the end of this lesson, you will be able to:

๐ŸŽฏ Responsive Design Fundamentals

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.

Core Principles

  1. Fluid Grids: Use percentages instead of fixed pixels
  2. Flexible Images: Scale images appropriately
  3. Media Queries: Apply different styles for different devices
  4. Mobile-First: Design for mobile, then enhance for larger screens

๐Ÿ”ง Flexible Units

Relative Length Units

/* 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 Units

/* 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 */

Modern CSS Functions

/* 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); }

๐Ÿ“ฑ Media Queries

Basic Syntax

/* 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;
    }
}

Common Breakpoints

/* 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;
    }
}

Media Query Features

/* 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;
    }
}

๐Ÿ› ๏ธ Hands-On Exercises

Exercise 1: Responsive Navigation

<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;
    }
}

Exercise 2: Responsive Card Grid

<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);
    }
}

Exercise 3: Responsive Typography

/* 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;
    }
}

Exercise 4: Responsive Images

/* 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;
    }
}

๐Ÿšจ Common Pitfalls

  1. Desktop-first thinking: Always start with mobile design
  2. Too many breakpoints: Keep it simple with 3-4 breakpoints
  3. Fixed pixel values: Use relative units where possible
  4. Ignoring touch targets: Make buttons at least 44px for mobile
  5. Forgetting viewport meta tag: Essential for mobile rendering

๐Ÿ” Best Practices

  1. Mobile-first approach: Design for smallest screen first
  2. Test on real devices: Emulators donโ€™t show everything
  3. Use semantic HTML: Screen readers and SEO benefit
  4. Optimize images: Use appropriate formats and sizes
  5. Consider performance: Mobile users often have slower connections

๐Ÿ“ Assessment

Quick Quiz

  1. Whatโ€™s the difference between em, rem, and px?
  2. When would you use min-width vs max-width in media queries?
  3. What does clamp(1rem, 2vw, 3rem) do?
  4. Why is mobile-first design recommended?

Practice Tasks

  1. Convert a fixed-width layout to responsive
  2. Create a responsive navigation menu
  3. Build a flexible card grid that works on all screen sizes
  4. Design responsive typography using fluid units

๐Ÿ”— Whatโ€™s Next?

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.