Create your own
Lesson illustration

Responsive Design with Media Queries

Hello! Welcome to your next lesson on building for the front end.

In our last session, we covered relative units like rem, em, and percentages. You learned how to create fluid elements that scale gracefully based on font size or parent containers. This is the first pillar of responsive design: fluidity.

Today, we'll build on that by introducing the second pillar: adaptation. As a product designer, you're used to creating different mockups for mobile, tablet, and desktop. Media queries are the CSS mechanism that lets you apply different styles for each of those screen sizes, effectively bringing your adaptive design vision to life in the browser.

This lesson directly addresses the learning outcome: Implement responsive design using media queries to adapt layouts for different screen sizes. We'll cover the syntax for writing media queries, the strategy for choosing where your layout should change, and practical examples of how to adjust a design for different screens.

1. What is Responsive Design?

Responsive web design ensures that a website looks and works well on a variety of devices and screen sizes. Instead of building separate sites for mobile and desktop, you build one flexible site that adapts its layout. Media queries are the primary tool for this.

Let's start with a short video that introduces the concept of responsive design and the role media queries play.

Become a CSS Media Queries & Responsive Design Pro! Learn EVERYTHING You Need to Know In Detail 🔍

This video from the Future Fullstack channel provides a great visual introduction to why responsive design is necessary and how it works in practice.

Watch from the beginning until 02:24. Pay attention to how the example website's layout, font sizes, and spacing all change as the screen width is reduced.

2. The Viewport Meta Tag: A Critical First Step

Before we write our first media query, there's a crucial piece of HTML we must add to every responsive page. It's the viewport meta tag.

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Without this tag, mobile browsers will pretend to be a desktop browser and simply "zoom out" to fit your desktop layout onto their small screen. This meta tag tells the browser to render the page at its actual device width and not to zoom, which allows our media queries to work correctly.

Let's get a quick explanation of this tag's importance.

CSS Media Queries & Responsive Web Design tutorial for Beginners

This clip from Dave Gray's tutorial clearly explains what the viewport meta tag is and why it's essential. He also shows how code editors like VS Code can add it for you automatically.

Watch from 00:29 to 02:48. The key takeaway is that this tag is non-negotiable for responsive design.

You can also refer to the MDN documentation for a written explanation.

Media query fundamentals

The MDN docs provide a concise explanation of the viewport meta tag. It's a good idea to read this to solidify your understanding.

Read the short section titled viewport meta. This reinforces what you just saw in the video.

3. Media Query Syntax

A media query is a CSS @ rule that applies a block of CSS styles only if a certain condition is true. The most common condition is the width of the viewport.

The basic syntax looks like this:

@media screen and (max-width: 600px) {
  /* CSS rules to apply when the screen is 600px wide or less */
  body {
    background-color: lightblue;
  }
}

This consists of three parts:

  1. @media: The at-rule that initiates the media query block.
  2. screen: The media type. This specifies the type of media. screen is for computer screens, tablets, phones, etc. Another common one is print for when a user prints the page.
  3. (max-width: 600px): The media feature and condition. This is the test that must pass.
    • max-width: Applies styles to screens up to the specified width.
    • min-width: Applies styles to screens from the specified width and up.

The following video explains this syntax visually and also introduces a critical concept: the order of your media queries matters due to the CSS cascade.

Become a CSS Media Queries & Responsive Design Pro! Learn EVERYTHING You Need to Know In Detail 🔍

Let's return to the Future Fullstack video for a detailed breakdown of the syntax and the importance of ordering your media queries correctly.

Watch from 02:24 to 06:33. The diagrams are particularly helpful for understanding how max-width works and why you should order your media queries from largest to smallest max-width values.

4. Choosing Your Breakpoints

A breakpoint is the screen width at which your layout changes. A common question is: "What are the right breakpoints?" Should you target the exact width of an iPhone 14 or a Samsung Galaxy?

The answer is no. It's impractical to target specific devices. Instead, you should let your content determine the breakpoints. As you resize your browser window, find the point where your design starts to look awkward—maybe text lines become too long, or elements get too squished. That's your breakpoint!

While breakpoints should be content-driven, it's helpful to have a general framework based on common device categories.

Become a CSS Media Queries & Responsive Design Pro! Learn EVERYTHING You Need to Know In Detail 🔍

This segment provides an excellent framework for thinking about breakpoints, categorizing them into ranges like phone, tablet, and laptop, without being overly prescriptive.

Watch from 40:06 to 47:55. Focus on the idea that you don't design for specific devices, but rather for ranges, and that the ultimate decision for a breakpoint is where your unique design starts to break.

5. Putting It All Together: A Practical Example

Now let's apply these concepts. We'll see how to take a multi-column layout and adapt it for smaller screens.

First, you need to know how to use your browser's developer tools to simulate different screen sizes. This is an essential skill for any front-end developer.

CSS Media Queries & Responsive Web Design tutorial for Beginners

Dave Gray's tutorial has a great, quick demonstration of how to use the device toolbar in Chrome DevTools to test your responsive designs.

Watch from 17:47 to 20:48. Practice opening the dev tools on any webpage and toggling the device toolbar as shown.

Now, let's watch how media queries are used to solve common responsive layout problems.

Become a CSS Media Queries & Responsive Design Pro! Learn EVERYTHING You Need to Know In Detail 🔍

This final video demonstrates how to apply media queries to a real project. You'll see how to adjust spacing and font sizes, and most importantly, how to change a grid from multiple columns to a single column on mobile.

First, watch from 14:38 to 17:30 to see how font sizes and padding are adjusted at a breakpoint. Then, jump to 01:07:21 and watch until 01:12:40 to see the key technique of changing a two-column grid layout to a single-column stack for small screens.

The key pattern you just saw—changing grid-template-columns from 1fr 1fr (two columns) to 1fr (one column) inside a media query—is one of the most common and powerful techniques in responsive design.

Conclusion

You've now learned the fundamental tool for creating adaptive layouts. By combining the fluid techniques from the last lesson with the adaptive power of media queries, you can build designs that are truly responsive.

Key Takeaways:

  • Media Queries apply CSS rules based on conditions, most commonly the viewport width.
  • Always include the viewport meta tag in your HTML to ensure media queries work on mobile devices.
  • Use max-width to apply styles for screens up to a certain size, and min-width for screens from a certain size up.
  • Breakpoints are the widths where your layout changes. They should be determined by your content, not by specific device dimensions.
  • A common responsive pattern is to change a multi-column layout (using Flexbox or Grid) into a single-column layout on smaller screens.

In our next lesson, we'll discuss mobile-first design. This is a strategic approach where you design and code the mobile layout first, then use min-width media queries to add complexity and enhance the layout for larger screens. It's the modern standard for responsive development and will streamline your workflow significantly.

Can't find a good explanation? Sign up and we'll make it for you

Sign up