Skip to main content
April 2, 2026Dan Rodney/7 min read

Responsive Web Design- Grid and Media Queries

Master modern responsive layouts with Grid and media queries

Core Responsive Design Components

CSS Grid

A powerful two-dimensional layout system that allows you to create column and row-based grids. Unlike Flexbox, Grid excels at complex layouts with precise positioning control.

Media Queries

CSS rules that apply styles conditionally based on screen size, device orientation, and other media features. Essential for true responsive design across devices.

Mobile-First Development

A design approach where you start with mobile styles as the base, then progressively enhance for larger screens using media queries.

Flexbox vs Grid Layout Systems

FeatureFlexboxCSS Grid
DimensionalityOne-dimensionalTwo-dimensional
Best Use CaseComponent layoutsPage layouts
Item ArrangementSequential flowPrecise positioning
Browser SupportExcellentModern browsers
Recommended: Use Grid for complex layouts with defined rows and columns, Flexbox for component-level arrangements

Setting Up CSS Grid Layout

1

Apply display: grid to parent

Set the container element to display grid to enable grid properties for child elements

2

Define grid template columns

Use grid-template-columns to specify the number and size of columns using fractional units (fr)

3

Add gap for spacing

Use gap property to add consistent spacing between grid items without affecting outer margins

4

Position grid items

Use properties like grid-column: span 2 to control how items span across multiple columns or rows

Understanding Fractional Units

The fr unit represents a fraction of available space. Using 2fr 1fr creates a 2:1 ratio (67% to 33%), while 1fr 1fr creates equal columns (50% each). This eliminates the need for percentage calculations.

Common Grid Column Ratios

Equal columns (1fr 1fr)
50
Two-thirds layout (2fr 1fr)
67
Three-quarters layout (3fr 1fr)
75

Grid Implementation Checklist

0/5
Mobile-First Responsive Strategy

Start with styles that work on mobile devices, then use media queries to enhance the layout for larger screens. This ensures a solid foundation and progressive enhancement.

Implementing Media Queries

1

Write base mobile styles

Create styles outside media queries that target mobile devices as the default experience

2

Choose appropriate breakpoints

Select screen width values like 700px where layout changes make sense for your content

3

Add min-width media queries

Use @media (min-width: 700px) to apply styles only to screens 700px and larger

4

Place media queries after base styles

Ensure media queries come later in CSS to properly override base styles with same specificity

All the rules outside of a media query are going to target mobile phones. We're going to do mobile first development.
This approach ensures your website works on the most constrained devices first, then enhances the experience for larger screens.

Mobile-First Development Approach

Pros
Ensures core functionality works on all devices
Progressive enhancement improves performance
Forces focus on essential content and features
Better SEO as mobile-first is Google's standard
Easier to add features than remove them
Cons
May require more initial planning
Desktop designs might feel constrained
Requires different mindset from traditional design
Some complex layouts harder to simplify

Responsive Layout Progression

0px - 699px

Mobile Base Styles

Single column layout, no body margins, mobile-optimized spacing

700px

Breakpoint Activation

Media query triggers at minimum width threshold

700px+

Desktop Enhancement

Two-column grid, body margins, spanning elements, optimized for larger screens

Key Success Indicators

Your responsive design is working when layouts look appropriate at all screen sizes, content remains readable and accessible, and the user experience feels natural across devices.

This lesson is a preview from our Web Development with HTML & CSS Course Online (includes software) and Full-Stack Web Development Certificate Online (includes software). Enroll in a course for detailed lessons, live instructor support, and project-based training.

While Flexbox provides powerful layout capabilities, CSS Grid offers an even more sophisticated approach to web design. Think of Grid as a two-dimensional layout system that creates precise column-and-row structures, giving you unprecedented control over how elements align and flow across your page.

Grid represents a fundamental shift in how we approach web layouts. Unlike Flexbox's one-dimensional flow, Grid establishes a coordinate system where you can place elements with surgical precision. This makes it particularly valuable for complex layouts that need to adapt seamlessly across devices—a critical requirement in today's mobile-first world.

This brings us to a crucial distinction in modern web development: truly responsive design requires media queries, not just fluid layouts. While we've been creating mobile-optimized designs that scale smoothly, genuine responsiveness means strategically changing layout behavior at specific screen sizes. Media queries are the mechanism that makes this possible, allowing different visual treatments for different viewport conditions.

Let's examine a practical implementation. Our current layout performs well on mobile devices, but desktop users deserve a more sophisticated presentation. The challenge lies in creating a two-column desktop layout while maintaining the single-column mobile experience—without compromising either use case.

Right-click and inspect the current layout, then narrow your browser window to simulate mobile viewing. Notice how the typography feels oversized and the spacing inefficient on smaller screens. These aren't minor aesthetic issues—they directly impact user experience and engagement rates.

For larger screens, we can leverage horizontal real estate more effectively. A two-column approach would allow the education section to sit alongside work experience, creating better visual balance and reducing scroll fatigue. This kind of adaptive design thinking separates professional web development from amateur attempts.

Examining our HTML structure reveals three semantic sections within the main content area. Each section represents a distinct content category—statement of intent, work experience, and education. This semantic structure becomes our foundation for Grid-based layout control.

The parent-child relationship is crucial here. When we apply `display: grid` to the main element, its three section children become grid items automatically. This mirrors Flexbox's behavior but with far more sophisticated positioning options. Unlike Flexbox's linear flow, Grid lets us create complex layouts that would require multiple nested containers in older approaches.

Notice that each section has a unique ID alongside shared classes—a deliberate structural choice. While classes could work equally well, IDs emphasize the singular nature of each content area. The shared `primary-section` class provides consistent styling across all sections, demonstrating how modern CSS architecture balances specificity with maintainability.

Now for the implementation. First, we need to establish our grid container. Looking at the existing CSS, there's no main element styling yet, so we'll create it. Adding `display: grid` alone doesn't change the visual presentation—it simply activates Grid functionality, similar to how `display: flex` unlocks Flexbox properties.


The real power emerges with `grid-template-columns`. This property simultaneously defines column quantity and sizing. For our two-thirds/one-third layout, we'll use `2fr 1fr`—fractional units that represent proportional space allocation rather than fixed measurements.

Fractional units (fr) deserve deeper explanation because they're genuinely revolutionary. Unlike percentages, which require manual calculation, fractional units automatically handle the math. `2fr 1fr` means "give the first column twice the space of the second column." The browser calculates that 2+1=3 total parts, allocating 2/3 to column one and 1/3 to column two. This approach scales elegantly and simplifies responsive adjustments.

You could create more complex distributions—`1fr 2fr 1fr` would create three columns with the center one twice as wide as the outer columns. The system handles all proportional calculations automatically, making layout adjustments remarkably straightforward.

Adding `gap: 30px` creates consistent spacing between grid areas—both horizontally and vertically. Unlike margins or padding, gap only affects internal spacing between grid items, never adding unwanted space around the container's exterior. This precision is exactly what professional layouts require.

However, we now face a three-items-into-two-columns challenge. Grid automatically flows items into available cells, creating new rows when necessary. Our statement of intent sits in column one, work experience in column two, but then education wraps to a new row—not our intended design.

The solution lies in explicit grid placement. Using `grid-column: span 2` on the statement section forces it to occupy both columns. This pushes the remaining sections to the next row, where they naturally fill the two-column structure we defined. The result is a clean, intentional layout that maximizes content hierarchy.

This creates an excellent desktop experience, but it's problematic on mobile devices. Two narrow columns on a phone screen create cramped, unreadable content. This is where media queries become essential—allowing us to serve different layouts to different screen sizes without compromising either experience.

We also need to address the body margins. The current 20px margin works well on desktop but wastes precious screen real estate on mobile. A mobile-first approach means starting with no margins, then adding them back for larger screens.

Media queries follow a specific syntax: `@media (min-width: 700px)` creates a conditional block that only applies when the viewport meets the specified criteria. This particular query targets screens 700 pixels wide and larger. Choose breakpoints based on your content's behavior, not arbitrary device dimensions.


The mobile-first methodology means base styles target small screens, with media queries progressively enhancing the experience for larger viewports. This approach aligns with how browsers parse CSS and how users actually consume web content—mobile traffic has dominated desktop since 2016 and continues growing in 2026.

Inside our media query, we'll override specific properties for larger screens: body margins return to 20px, grid columns shift to `2fr 1fr`, and the statement section spans both columns. The heading size can also scale up—perhaps from 30px on mobile to 42px on desktop, improving hierarchy and visual impact.

The cascade order matters critically here. Media query rules must come after their base counterparts in the stylesheet. Both selectors have identical specificity, so source order determines which rule applies. The later rule overwrites the earlier one when the media query condition is met.

Testing responsive behavior requires proper tools. Browser window resizing provides basic feedback, but mobile simulation gives more accurate results. Modern browsers include device simulation tools that account for pixel density, touch interactions, and viewport meta tags—crucial factors that simple resizing can't replicate.

Breakpoint selection deserves strategic consideration. Rather than targeting specific devices, focus on when your content begins to look cramped or awkwardly spaced. Watch the browser's viewport indicator as you resize—when the layout starts feeling uncomfortable, that's your natural breakpoint. It might be 660px, 800px, or something entirely different based on your specific content and typography choices.

This approach scales beautifully beyond two breakpoints. Complex sites often use 3-4 media queries to optimize for phones, tablets, laptops, and large displays. Each breakpoint addresses specific usability challenges while building upon the previous layer's foundation.

The result is a genuinely responsive layout that serves users appropriately regardless of their device choice. On mobile, they get focused, scrollable content optimized for thumb navigation. On desktop, they get sophisticated multi-column layouts that leverage available screen space efficiently. This isn't just good design—it's good business, directly impacting engagement metrics and conversion rates.

One final consideration: always test your responsive layouts on actual devices when possible. Desktop simulation tools are excellent for development, but real-world testing reveals issues that even the best simulators miss. The investment in proper responsive testing pays dividends in user satisfaction and site performance.

Key Takeaways

1CSS Grid enables two-dimensional layouts with precise control over both columns and rows, making it ideal for complex page layouts
2Fractional units (fr) in Grid eliminate the need for percentage calculations and automatically handle proportional spacing
3Media queries with min-width create truly responsive designs by applying different styles based on screen size conditions
4Mobile-first development starts with mobile styles as the foundation, then progressively enhances for larger screens
5Grid properties like grid-template-columns define the structure while grid-column: span controls individual item placement
6The gap property provides consistent spacing between grid items without affecting outer margins or padding
7Breakpoints should be chosen based on when your layout needs to change, not specific device sizes
8Media queries must come after base styles in CSS to properly override rules with the same specificity

RELATED ARTICLES