Back to Curriculum

Responsive Design

📚 Lesson 6 of 16 ⏱️ 35 min

Responsive Design

35 min

Responsive Web Design (RWD) is the approach that suggests that design and development should respond to the user's behavior and environment based on screen size, platform, and orientation. It's not just about shrinking content; it's about adapting the layout to provide the best experience on any device.

The core technology behind RWD is the **Media Query**. Media queries allow you to apply CSS rules only if certain conditions are true. The most common condition is `min-width` or `max-width`, which lets you set 'breakpoints' where the layout changes.

Another pillar of responsive design is fluid grids (using percentages or `fr` units instead of fixed pixels) and flexible images (setting `max-width: 100%`). This ensures that elements resize proportionally rather than overflowing or being cut off.

Mobile-First design is a popular strategy where you write styles for mobile devices first (the default), and then use `min-width` media queries to add complexity for larger screens. This often results in cleaner, more performant code.

Key Concepts

  • Adapt layouts to viewport size.
  • Media Queries are conditional CSS.
  • Breakpoints define layout changes.
  • Fluid grids and flexible images.
  • Mobile-First vs. Desktop-First strategies.

Learning Objectives

Master

  • Writing standard media query syntax
  • Choosing appropriate breakpoints
  • Making images and videos responsive
  • Implementing a mobile-first workflow

Develop

  • User-centric design thinking
  • Testing across multiple devices
  • Performance optimization for mobile

Tips

  • Use the `<meta name='viewport'>` tag in HTML; otherwise, RWD won't work on phones.
  • Design for the smallest screen first, then enhance for larger ones.
  • Avoid fixed widths (px); use relative units (%, rem, vw).
  • Test on real devices whenever possible, not just browser resizing.

Common Pitfalls

  • Forgetting the viewport meta tag.
  • Using too many breakpoints (target content needs, not specific devices).
  • Hiding important content on mobile just to save space.
  • Large images slowing down mobile data connections.

Summary

  • Responsive design adapts to the device.
  • Media queries trigger styles at specific widths.
  • Fluid layouts prevent horizontal scrolling.
  • Mobile-first is the industry standard approach.

Exercise

Write a media query that changes the background color to lightblue when the screen width is 600px or less.

@media screen and (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}

Exercise Tips

  • Try mobile-first approach: start with mobile styles, then use min-width media queries.
  • Add multiple breakpoints for tablet and desktop sizes.
  • Use browser DevTools responsive mode to test different screen sizes.
  • Combine media queries with flexbox/grid to create responsive layouts.

Code Editor

Output