Stage 2: Frontend Fundamentals

CSS · Lesson 07

Responsive Design

Media queries, mobile-first thinking, and relative units that adapt to any screen.

CSS2 min readFree lesson

Responsive Web Design is a web design approach that provides the optimal layout
across a variety of screen sizes, such as mobile, tablet, and PC.


1. Viewport Meta Tag (Required)

<!-- Must be included in the HTML <head> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • width=device-width: Makes the viewport width equal to the device screen width
  • initial-scale=1.0: Initial zoom scale of 1x
  • Without this tag, @media queries do not work properly.

2. @media Queries

Apply different CSS depending on screen width (or other conditions).

/* Default: mobile styles */
.container {
  padding: 16px;
  font-size: 14px;
}

/* Tablet and up (768px and up) */
@media (min-width: 768px) {
  .container {
    padding: 24px;
    font-size: 16px;
  }
}

/* PC (1024px and up) */
@media (min-width: 1024px) {
  .container {
    max-width: 1200px;
    margin: 0 auto;
  }
}

3. Mobile-First vs Desktop-First

Mobile-First (Recommended)

Write the base styles for mobile, then add larger screens with min-width.

/* Default = mobile */
.cards { display: block; }

/* Tablet */
@media (min-width: 768px) {
  .cards { display: flex; flex-wrap: wrap; }
}

/* PC */
@media (min-width: 1024px) {
  .cards { grid-template-columns: repeat(3, 1fr); }
}

Desktop-First

Write the base styles for PC, then handle smaller screens with max-width.

/* Default = PC */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); }

/* Tablet and below */
@media (max-width: 1023px) {
  .cards { grid-template-columns: repeat(2, 1fr); }
}

/* Mobile and below */
@media (max-width: 767px) {
  .cards { display: block; }
}

Mobile-First is recommended for performance and accessibility.


4. Common Breakpoints

Name Width Target Device
Small ~ 480px Small smartphones
Mobile 480px ~ 768px Standard smartphones
Tablet 768px ~ 1024px Tablets, small laptops
Desktop 1024px ~ PCs, large laptops

5. Responsive Units

Unit Description Example
px Fixed pixels font-size: 16px
% Ratio relative to the parent element width: 50%
em Based on the current element's font-size padding: 1.5em
rem Based on the root (html) font-size font-size: 1rem
vw % of the viewport width width: 100vw
vh % of the viewport height height: 100vh
/* Using rem: based on the html font-size */
html { font-size: 16px; } /* default value */
h1 { font-size: 2rem; }   /* 16 × 2 = 32px */
p  { font-size: 1rem; }   /* 16 × 1 = 16px */

6. clamp() — Fluid Sizing

With clamp(minimum, preferred, maximum), the size changes smoothly without steps.

h1 {
  /* Minimum 1.5rem, maximum 3rem, based on 5% of the viewport width */
  font-size: clamp(1.5rem, 5vw, 3rem);
}

.container {
  /* Minimum 300px, maximum 1200px */
  width: clamp(300px, 90%, 1200px);
}

Example Files

Open responsive.html in the examples/ folder in your browser and
adjust the window width to see the layout change from 1 column to 2 columns to 3 columns.

Assignment

Read assignment.md and complete Applying Responsive Design to Your Portfolio.

Example files

Complete, runnable files used in this lesson. Copy them into your project and experiment.

responsive.htmlHTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <!-- Viewport meta tag: a mandatory requirement for responsive web -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Web — Mobile → Tablet → PC</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

  <!-- Responsive navigation -->
  <header class="header">
    <div class="header-inner">
      <a href="#" class="logo">Responsive</a>
      <!-- Turns into a hamburger menu on mobile -->
      <nav class="nav">
        <a href="#" class="nav-link">Home</a>
        <a href="#" class="nav-link">Portfolio</a>
        <a href="#" class="nav-link">About</a>
        <a href="#" class="nav-link">Contact</a>
      </nav>
    </div>
  </header>

  <main>

    <!-- Hero section -->
    <section class="hero">
      <h1 class="hero-title">Responsive Web Design</h1>
      <p class="hero-desc">Try adjusting the window width.<br>It switches automatically from mobile (1 column) → tablet (2 columns) → PC (3 columns).</p>
      <div class="breakpoint-indicator">
        <span class="bp-mobile">Mobile</span>
        <span class="bp-tablet">Tablet</span>
        <span class="bp-desktop">Desktop</span>
      </div>
    </section>

    <div class="container">

      <!-- Responsive card grid -->
      <section class="section">
        <h2>Card Grid — Automatic Switching with @media</h2>
        <p class="section-desc">
          Mobile: 1 column | Tablet (768px and up): 2 columns | PC (1024px and up): 3 columns
        </p>
        <div class="card-grid">
          <article class="card">
            <div class="card-img img-1"></div>
            <div class="card-body">
              <h3>Project A</h3>
              <p>A portfolio site built with responsive web design.</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
          <article class="card">
            <div class="card-img img-2"></div>
            <div class="card-body">
              <h3>Project B</h3>
              <p>A shopping mall landing page built with a mobile-first approach.</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
          <article class="card">
            <div class="card-img img-3"></div>
            <div class="card-body">
              <h3>Project C</h3>
              <p>A dashboard UI that uses CSS Grid and Flexbox together.</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
          <article class="card">
            <div class="card-img img-4"></div>
            <div class="card-body">
              <h3>Project D</h3>
              <p>An experimental page with fluid typography built using clamp().</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
          <article class="card">
            <div class="card-img img-5"></div>
            <div class="card-body">
              <h3>Project E</h3>
              <p>A case study that separates mobile and desktop layouts with media queries.</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
          <article class="card">
            <div class="card-img img-6"></div>
            <div class="card-body">
              <h3>Project F</h3>
              <p>Fully responsive typography implemented with vw/vh units and clamp().</p>
              <a href="#" class="card-link">View details →</a>
            </div>
          </article>
        </div>
      </section>

      <!-- Responsive layout comparison -->
      <section class="section">
        <h2>Unit Comparison Demo</h2>
        <div class="units-demo">
          <div class="unit-box px-box">
            <strong>px</strong>
            <p>Fixed size (always 200px)</p>
          </div>
          <div class="unit-box percent-box">
            <strong>%</strong>
            <p>50% of the parent</p>
          </div>
          <div class="unit-box vw-box">
            <strong>vw</strong>
            <p>40% of the viewport width</p>
          </div>
        </div>
      </section>

      <!-- clamp() demo -->
      <section class="section">
        <h2>clamp() Fluid Typography</h2>
        <p class="clamp-demo-text">When you change the window width, this text size changes smoothly.</p>
        <p class="section-desc">
          <code>font-size: clamp(1.2rem, 4vw, 2.5rem)</code>
        </p>
      </section>

    </div>
  </main>

  <!-- Responsive footer -->
  <footer class="footer">
    <div class="footer-inner">
      <div class="footer-brand">
        <strong>Responsive</strong>
        <p>A learning site for responsive web design</p>
      </div>
      <nav class="footer-nav">
        <a href="#">Home</a>
        <a href="#">Portfolio</a>
        <a href="#">About</a>
        <a href="#">Contact</a>
      </nav>
    </div>
    <div class="footer-copy">
      <p>&copy; 2025 Responsive Web Example</p>
    </div>
  </footer>

</body>
</html>
style.cssCSS
/* =============================================
   Responsive Web Example
   Lesson 07: @media queries / Mobile-First / breakpoints / clamp()
   ============================================= */

/* =============================================
   Basic Reset
   ============================================= */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* body: Mobile-First base styles (mobile first) */
body {
  font-family: 'Segoe UI', Arial, sans-serif;
  background-color: #f5f6fa;
  color: #333;
  line-height: 1.6;
  font-size: 15px;             /* Mobile base font size */
}

/* =============================================
   Header: Mobile-First
   Stacked vertically on mobile, horizontal on PC
   ============================================= */
.header {
  background-color: #2c3e50;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  display: flex;
  flex-direction: column;       /* Mobile: vertical layout */
  padding: 12px 16px;
}

.logo {
  color: #3498db;
  font-size: 1.3rem;
  font-weight: 900;
  text-decoration: none;
  margin-bottom: 8px;           /* Mobile: bottom margin */
}

.nav {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;              /* Wrap when there are many menu items */
}

.nav-link {
  color: #aab7b8;
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 0.85rem;
  transition: background-color 0.2s, color 0.2s;
}

.nav-link:hover {
  background-color: rgba(255,255,255,0.1);
  color: white;
}

/* Horizontal header layout on tablet and up */
@media (min-width: 768px) {
  .header-inner {
    flex-direction: row;        /* Tablet+: horizontal layout */
    justify-content: space-between;
    align-items: center;
    padding: 0 24px;
    height: 60px;
  }

  .logo {
    margin-bottom: 0;           /* Remove bottom margin in horizontal layout */
  }
}

/* =============================================
   Hero section
   ============================================= */
.hero {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  text-align: center;
  padding: 60px 16px;           /* Mobile: 16px left and right padding */
}

/* Fluid title size with clamp() */
.hero-title {
  font-size: clamp(1.8rem, 5vw, 3rem); /* Minimum 1.8rem, maximum 3rem */
  font-weight: 900;
  margin-bottom: 16px;
  line-height: 1.2;
}

.hero-desc {
  font-size: clamp(0.9rem, 2.5vw, 1.1rem); /* Fluid with clamp */
  opacity: 0.9;
  margin-bottom: 28px;
  line-height: 1.7;
}

/* Breakpoint indicator */
.breakpoint-indicator {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.bp-mobile, .bp-tablet, .bp-desktop {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 700;
  border: 2px solid rgba(255,255,255,0.3);
  opacity: 0.4;                 /* Default: dimmed */
}

/* On mobile, only .bp-mobile is highlighted */
.bp-mobile { opacity: 1; background-color: rgba(255,255,255,0.2); }

/* Highlight .bp-tablet on tablet */
@media (min-width: 768px) {
  .bp-mobile  { opacity: 0.4; background-color: transparent; }
  .bp-tablet  { opacity: 1; background-color: rgba(255,255,255,0.2); }
}

/* Highlight .bp-desktop on PC */
@media (min-width: 1024px) {
  .bp-tablet   { opacity: 0.4; background-color: transparent; }
  .bp-desktop  { opacity: 1; background-color: rgba(255,255,255,0.2); }
}

/* =============================================
   Container
   ============================================= */
.container {
  padding: 24px 16px;          /* Mobile: narrow padding */
  max-width: 1200px;
  margin: 0 auto;
}

/* Tablet and up */
@media (min-width: 768px) {
  .container {
    padding: 32px 24px;        /* Tablet: larger padding */
  }
}

/* PC and up */
@media (min-width: 1024px) {
  .container {
    padding: 40px 32px;        /* PC: even wider padding */
  }
}

/* =============================================
   Section
   ============================================= */
.section {
  background: white;
  border-radius: 12px;
  padding: 20px;               /* Mobile: small padding */
  margin-bottom: 24px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.07);
}

/* Tablet and up: larger padding */
@media (min-width: 768px) {
  .section { padding: 28px; }
}

.section h2 {
  font-size: 1.2rem;
  color: #2c3e50;
  margin-bottom: 8px;
  padding-bottom: 10px;
  border-bottom: 2px solid #667eea;
}

.section-desc {
  color: #777;
  font-size: 0.85rem;
  margin-bottom: 20px;
}

/* =============================================
   Card grid: Mobile-First responsive
   ============================================= */
.card-grid {
  display: grid;
  /* Mobile default: 1 column */
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Tablet and up (768px): 2 columns */
@media (min-width: 768px) {
  .card-grid {
    grid-template-columns: repeat(2, 1fr); /* 2 columns */
    gap: 20px;
  }
}

/* PC and up (1024px): 3 columns */
@media (min-width: 1024px) {
  .card-grid {
    grid-template-columns: repeat(3, 1fr); /* 3 columns */
    gap: 24px;
  }
}

/* Card */
.card {
  border-radius: 10px;
  overflow: hidden;
  background: white;
  border: 1px solid #eee;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  transition: transform 0.25s, box-shadow 0.25s;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.12);
}

/* Card image area */
.card-img {
  height: 140px;               /* Mobile: shorter image */
}

/* Tablet and up: taller image */
@media (min-width: 768px) {
  .card-img { height: 160px; }
}

/* Card image backgrounds */
.img-1 { background: linear-gradient(135deg, #667eea, #764ba2); }
.img-2 { background: linear-gradient(135deg, #f093fb, #f5576c); }
.img-3 { background: linear-gradient(135deg, #4facfe, #00f2fe); }
.img-4 { background: linear-gradient(135deg, #43e97b, #38f9d7); }
.img-5 { background: linear-gradient(135deg, #fa709a, #fee140); }
.img-6 { background: linear-gradient(135deg, #a18cd1, #fbc2eb); }

.card-body {
  padding: 16px;
}

.card-body h3 {
  font-size: 1rem;
  color: #2c3e50;
  margin-bottom: 6px;
  font-weight: 700;
}

.card-body p {
  font-size: 0.83rem;
  color: #777;
  line-height: 1.5;
  margin-bottom: 12px;
}

.card-link {
  font-size: 0.82rem;
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  transition: opacity 0.2s;
}

.card-link:hover { opacity: 0.7; }

/* =============================================
   Unit comparison demo
   ============================================= */
.units-demo {
  display: flex;
  flex-direction: column;      /* Mobile: vertical layout */
  gap: 12px;
}

/* Tablet and up: horizontal layout */
@media (min-width: 768px) {
  .units-demo {
    flex-direction: row;       /* Horizontal layout */
    align-items: flex-start;
  }
}

.unit-box {
  padding: 16px;
  border-radius: 8px;
  color: white;
  font-size: 0.88rem;
}

.unit-box strong {
  display: block;
  font-size: 1.3rem;
  margin-bottom: 6px;
}

/* px: fixed size */
.px-box {
  width: 200px;                /* Fixed 200px — always the same size */
  background-color: #e74c3c;
}

/* %: ratio of the parent */
.percent-box {
  width: 50%;                  /* 50% of the parent width */
  background-color: #3498db;
}

/* vw: based on the viewport */
.vw-box {
  width: 40vw;                 /* 40% of the viewport width */
  max-width: 400px;            /* Maximum width limit */
  background-color: #2ecc71;
}

/* =============================================
   clamp() demo
   ============================================= */
.clamp-demo-text {
  /* clamp(minimum, preferred, maximum) */
  font-size: clamp(1.2rem, 4vw, 2.5rem); /* Fluid between 1.2rem and 2.5rem */
  font-weight: 700;
  color: #667eea;
  margin-bottom: 12px;
  line-height: 1.3;
}

code {
  background: #f4f4f4;
  color: #c0392b;
  padding: 3px 8px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.85em;
}

/* =============================================
   Responsive footer
   ============================================= */
.footer {
  background-color: #2c3e50;
  color: #aab7b8;
  padding: 30px 16px 16px;    /* Mobile: small padding */
}

.footer-inner {
  display: flex;
  flex-direction: column;     /* Mobile: vertical layout */
  gap: 20px;
  margin-bottom: 20px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

/* Tablet and up: horizontal layout */
@media (min-width: 768px) {
  .footer-inner {
    flex-direction: row;      /* Horizontal layout */
    justify-content: space-between;
    align-items: flex-start;
  }
}

.footer-brand strong {
  display: block;
  color: white;
  font-size: 1.1rem;
  margin-bottom: 6px;
}

.footer-brand p {
  font-size: 0.82rem;
}

.footer-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.footer-nav a {
  color: #aab7b8;
  text-decoration: none;
  font-size: 0.85rem;
  transition: color 0.2s;
}

.footer-nav a:hover { color: white; }

.footer-copy {
  border-top: 1px solid rgba(255,255,255,0.1);
  padding-top: 16px;
  text-align: center;
  font-size: 0.8rem;
  max-width: 1200px;
  margin: 0 auto;
}

Assignment

Assignment — Applying Responsive Design to My Portfolio

Goal

Use @media queries to build a responsive portfolio page that
separates mobile and desktop layouts.


Requirements

1. Required Components

  • Header (name + navigation)
  • Hero section (introduction)
  • Work/skills card section (at least 3)
  • Footer

2. Responsive Conditions

Screen Condition Layout
Mobile ~ 767px 1 column, navigation stacked vertically
Desktop 768px and up 2–3 columns, navigation laid out horizontally

3. Required @media Queries

/* Write the mobile base styles first */
.cards {
  display: grid;
  grid-template-columns: 1fr;   /* Mobile: 1 column */
}

/* Desktop */
@media (min-width: 768px) {
  .cards {
    grid-template-columns: repeat(3, 1fr); /* PC: 3 columns */
  }
}

4. Required Elements

  • <meta name="viewport" ...> tag
  • At least 2 @media (min-width: ...) queries
  • At least one clamp() or vw unit
  • Use rem units
  • Mobile and PC layouts must be clearly distinct

Checklist

  • A viewport meta tag is present
  • Mobile base styles were written first (Mobile-First)
  • PC styles were added with @media (min-width: ...) queries
  • Cards are 1 column on mobile and 2 or more columns on PC
  • rem or clamp() is used for font sizes
  • No inline CSS was used

Hints

Mobile-First Order

/* 1. Mobile base styles */
.nav { flex-direction: column; }
.cards { grid-template-columns: 1fr; }

/* 2. Add tablet */
@media (min-width: 768px) {
  .nav { flex-direction: row; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}

/* 3. Add PC */
@media (min-width: 1024px) {
  .cards { grid-template-columns: repeat(3, 1fr); }
}

If you are curious about the answer, see the solution/ folder.

Learn this lesson with a mentor

Work through the Build Your Homepage series on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now