USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Responsive Design with Media Queries and Container Queries

Lesson 12/30 | Study Time: 28 Min

Responsive design is an essential approach in modern web development that ensures websites adapt smoothly to different screen sizes, devices, and user contexts.

Media queries allow developers to apply CSS rules based on viewport characteristics such as screen width, height, and resolution, making it possible to design layouts for mobile, tablet, and desktop screens.

Container queries, a newer CSS feature, extend this concept by enabling styles to respond to the size of a parent container rather than the entire viewport. 

Media Queries Fundamentals

Media queries let CSS respond to device characteristics like width, height, and orientation.

Introduced in CSS3, they're the backbone of responsive design, powering 95% of top websites per HTTP Archive data.


Syntax and Breakpoints

Media queries follow a simple "if-this-then-that" logic. Place them after base styles for mobile-first development—the 2025 gold standard.


Basic Syntax:

css
@media (min-width: 768px) {
.container { width: 750px; }
}

Common Breakpoint Strategy


Pro Tip: Use em units for breakpoints (e.g., 48em) since they scale with font-size changes.

Practical Media Query Patterns

Apply these patterns to common components:


Example: Responsive Navigation

css
.navbar {
display: flex; /* Mobile: horizontal */
}

@media (max-width: 768px) {
.navbar {
flex-direction: column;
}
}


Testing Process


1. Start with mobile view in Chrome DevTools.

2. Gradually increase viewport width.

3. Verify smooth transitions at each breakpoint.

4. Test real devices via BrowserStack.

Container Queries: The Modern Revolution

Container queries shift focus from viewport size to parent container size, solving component reusability issues that plagued media queries. Native since Chrome 105 (2022), they're now supported in 95%+ of browsers per CanIUse.


Container Query Syntax

First, define a container, then query its size. This powers design systems where components adapt independently.


Step-by-Step Implementation


1. Create container: .card { container-type: inline-size; }

2. Query inside: @container (min-width: 300px) { .content { grid-template-columns: 1fr 1fr; } }


Complete Example: Responsive Card Component

css
.article-card {
container-type: inline-size;
max-width: 500px;
}

.article-image {
width: 100%;
}

@container (min-width: 400px) {
.article-card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}

Key Container Types:

Advanced Responsive Techniques


Combine these tools with modern CSS for production-ready responsiveness.


Fluid Typography and Spacing

Use clamp() for scale-agnostic sizing that works everywhere.


Examples:

css
h1 { font-size: clamp(2rem, 5vw, 4rem); }
.padding { padding: clamp(1rem, 5vw, 3rem); }


Logical Properties for RTL Support

Replace margin-left with margin-inline-start for global audiences.


Before/After

css
/* Old way */
.card { margin-left: 20px; }

/* Modern RTL-ready */
.card { margin-inline-start: 20px; }


Responsive Images Best Practices


1. Use <picture> with srcset + media conditions.

2. CSS object-fit for perfect cropping.

3. sizes attribute predicts rendered width.


Example:

xml
<picture>
<source media="(min-width: 768px)" srcset="large.jpg">
<img src="small.jpg" alt="Responsive image">
</picture>

Testing and Debugging Responsive Design

Comprehensive Testing Workflow



Common Pitfalls to Avoid:


1. Over-nesting media queries (use 4-5 breakpoints max).

2. Forgetting box-sizing: border-box.

3. Ignoring prefers-reduced-motion for animations.


Debug Checklist


1. Mobile-first base styles

2. Smooth breakpoint transitions

3. No horizontal scroll (overflow-x: hidden)

4. Touch targets ≥ 44px

5. Container queries on reusable components