Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Mobile-First Responsive Storefront Design

Recommended resources

Welcome to your seventh lesson. In our last session, you did an excellent job using modern CSS Flexbox and Grid to build a responsive storefront layout. You learned how the auto-fit keyword in CSS Grid can create a product catalog that adapts to screen size without any media queries. While powerful, this automatic approach sometimes doesn't offer the fine-grained control we need for a polished, professional design.

Today, we'll take that control. Our learning outcome is to apply mobile-first media queries to adapt a storefront across common screen sizes. You'll learn the "mobile-first" philosophy, a modern standard for building responsive websites. We'll start by styling our components for the smallest screens and then use media queries to progressively add complexity and rearrange elements for larger screens like tablets and desktops. This ensures a great experience on any device.

What are Media Queries?

Media queries are the cornerstone of responsive design. They are a feature in CSS that allows you to apply styles only when certain conditions about the user's device or viewport are met. The most common condition we test for is the viewport width.

Let's begin by understanding the fundamental syntax and concepts.

Media query fundamentals - Learn web development | MDN

This article from the MDN Web Docs provides a clear and concise introduction to media queries.

Please read the introduction and the first three subsections: the sections covering basics, media types, and media feature rules. Pay close attention to the syntax of the @media rule and the difference between min-width and max-width. These are the tools we'll be using throughout the lesson.


The "Mobile-First" Philosophy

With media queries, you can approach responsive design in two ways:

  1. Desktop-first: Design for a large screen and use max-width media queries to remove columns, hide elements, and simplify the layout for smaller screens.
  2. Mobile-first: Design for a small screen (often a simple, single-column layout) and use min-width media queries to add columns, show more elements, and create more complex layouts as the screen gets wider.

The industry standard and the approach we'll be taking is mobile-first. It encourages simpler, more performant base styles and aligns with the principle of progressive enhancement—starting with a core experience that works everywhere and adding features as more screen space becomes available.

Media query fundamentals - Learn web development | MDN

Let's return to the MDN article to understand this philosophy better.

Read the two sections titled Mobile-first responsive design and How to choose breakpoints. The key idea is to let your content, not a list of device sizes, determine where your layout needs to change.

Before we write any code, let's hear a great summary of this approach from instructor Kevin Powell.

Responsive design made easy

Kevin Powell is a well-respected CSS educator, and his explanation here is excellent.

Watch the segment from this part of the video. He summarizes the core principle: build a solid base design first (mobile), and then use media queries to add complexity for larger screens.


Applying Mobile-First to Our Storefront Header

Now, let's apply this to our catalog.html and styles.css. Our current header layout works well on wide screens but is not ideal for mobile. On a narrow screen, justify-content: space-between can push the logo and cart icon too far apart, leaving the navigation links awkwardly wrapped in the middle.

We'll fix this using a mobile-first approach.

Step 1: The Mobile Base Style

First, we will modify our existing CSS to be mobile-friendly by default. This means these styles will apply to all screen sizes unless overridden later.

Open your styles.css file. Find the .site-header and .nav-list rules you wrote in the previous lesson.

Delete the existing .site-header and .nav-list rules and replace them with the following code. This will become our new base style for mobile.

/* --- Header Styles (Mobile-First) --- */

.site-header {
  display: flex;
  flex-wrap: wrap; /* Allow items to wrap to the next line */
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #f8f9fa;
  border-bottom: 1px solid #dee2e6;
  gap: 1rem; /* Add space between all items */
}

.nav-list {
  /* On mobile, we want the nav to take the full width if it wraps */
  display: flex;
  gap: 1.5rem;
  width: 100%;
  justify-content: center; /* Center the nav items if they wrap */
}

If you refresh your browser and make the window narrow, you'll see the navigation now wraps onto a new line and centers itself. The logo and cart icon remain at the top. This is a much better mobile experience.

Step 2: Adding a Breakpoint for Larger Screens

Now, our layout looks good on mobile, but it's broken on desktop (the navigation stays on its own line). This is where our first media query comes in. We will add a breakpoint—a point where the design adapts. We'll choose a breakpoint of 768px, a common width for tablets.

At the very bottom of your styles.css file, add the following media query:

/* --- Media Queries --- */

/* Apply these styles when the viewport is 768px or WIDER */
@media (min-width: 768px) {
  .site-header {
    flex-wrap: nowrap; /* Prevent items from wrapping on larger screens */
  }

  .nav-list {
    /* Reset to be part of the main flex layout */
    width: auto; 
    justify-content: flex-start;
  }
}

Now, resize your browser window. Below 768px, you see our mobile-friendly wrapped layout. Above 768px, the media query kicks in, the navigation no longer wraps, and we get the clean, single-row header we originally designed. We started with a simple mobile base and added complexity for larger screens. That's mobile-first in action!


The Essential Viewport Meta Tag

There's one crucial piece of HTML that makes all of this work on actual mobile devices. Your catalog.html already has it from the last lesson, but it's vital to understand its purpose.

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

Without this tag, mobile browsers would pretend to be a wide desktop browser and then shrink the result, making your text tiny and ignoring your media queries. This tag tells the browser to use its actual device width as the viewport size, ensuring your responsive styles are applied correctly.


Refining the Product Grid with Media Queries

In our last lesson, we made a great responsive product grid with a single line of CSS:
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));

This is fantastic, but let's say our design requires more explicit control:

  • On mobile: Always a single column.
  • On tablet: Always two columns.
  • On desktop: Three or more columns, filling the space.

We can achieve this by combining our mobile-first approach with multiple media queries.

  1. Modify the base .product-grid rule in styles.css. We'll make it single-column by default.

    .product-grid {
      display: grid;
      /* Mobile-first: single column by default */
      grid-template-columns: 1fr;
      gap: 2rem;
    }
    
  2. Add new rules inside our media query block to create the multi-column layouts.

    /* --- Media Queries --- */
    
    @media (min-width: 768px) {
      /* ... existing header styles ... */
    
      /* Tablet: 2 columns */
      .product-grid {
        grid-template-columns: repeat(2, 1fr);
      }
    }
    
    /* Add a new breakpoint for desktops */
    @media (min-width: 1024px) {
      /* Desktop: Our original auto-fitting grid */
      .product-grid {
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
      }
    }
    

Now you have precise control at each stage, while still leveraging the power of auto-fit for the largest screens.

For a full demonstration of this process from start to finish, the following video by Traversy Media is an excellent resource.

Grid CSS Responsive Website Layout - "Mobile First" Design

This video walks through the entire process of building a page layout with a mobile-first mindset.

Watch the introduction from the beginning where the mobile-first strategy is outlined. Pay close attention starting at this point, where the @media (min-width: 700px) rule is introduced. All the styles before this point are for mobile by default. Continue watching until this mark to see how the CSS Grid layout is defined inside the media query to create the desktop version.


Conclusion

In this lesson, you've added one of the most fundamental skills of a front-end developer to your toolkit. You now understand how to build truly responsive layouts that provide an optimal experience for every user, regardless of their device.

Here are the key takeaways:

  • Media Queries allow you to apply CSS rules based on conditions like viewport width.
  • The Mobile-First approach involves writing simple, base styles for small screens and then using min-width media queries to add complexity for larger screens.
  • Breakpoints are the min-width values where your layout adapts. They should be determined by when your content starts to look poorly, not by specific device sizes.
  • The <meta name="viewport"> tag is essential for ensuring media queries work correctly on mobile browsers.

You've now built a solid, responsive, and accessible foundation for the storefront. However, layouts can sometimes misbehave in unexpected ways. In our next lesson, we will dive into a critical skill for any developer: using browser developer tools to diagnose and correct layout or accessibility issues.