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
| Feature | Flexbox | CSS Grid |
|---|---|---|
| Dimensionality | One-dimensional | Two-dimensional |
| Best Use Case | Component layouts | Page layouts |
| Item Arrangement | Sequential flow | Precise positioning |
| Browser Support | Excellent | Modern browsers |
Setting Up CSS Grid Layout
Apply display: grid to parent
Set the container element to display grid to enable grid properties for child elements
Define grid template columns
Use grid-template-columns to specify the number and size of columns using fractional units (fr)
Add gap for spacing
Use gap property to add consistent spacing between grid items without affecting outer margins
Position grid items
Use properties like grid-column: span 2 to control how items span across multiple columns or rows
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
Grid Implementation Checklist
This unlocks all grid-related properties for child elements
Fractional units automatically calculate proportional spacing
Gap only affects interior spacing, not outer margins
Control how elements stretch across multiple columns
Grid automatically creates new rows when columns are filled
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
Write base mobile styles
Create styles outside media queries that target mobile devices as the default experience
Choose appropriate breakpoints
Select screen width values like 700px where layout changes make sense for your content
Add min-width media queries
Use @media (min-width: 700px) to apply styles only to screens 700px and larger
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.
Mobile-First Development Approach
Responsive Layout Progression
Mobile Base Styles
Single column layout, no body margins, mobile-optimized spacing
Breakpoint Activation
Media query triggers at minimum width threshold
Desktop Enhancement
Two-column grid, body margins, spanning elements, optimized for larger screens
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.
Key Takeaways