惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
宝玉的分享
宝玉的分享
I
InfoQ
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
V
V2EX
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
T
The Blog of Author Tim Ferriss
量子位

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Container Queries: The CSS Feature I Wish I Had Years Ago
Muhammad Medhat · 2026-06-14 · via DEV Community
Cover image for Container Queries: The CSS Feature I Wish I Had Years Ago

Muhammad Medhat

Introduction

For years, responsive design meant one thing:

@media (max-width: 768px) {
    ...
}

Media queries work great when you want to adapt a layout based on the viewport size.

But what if a component lives inside a narrow sidebar on a large screen?

Or inside a wide content area on a tablet?

That's where Container Queries shine.


The Problem With Media Queries

Imagine a card component:

<div class="card">
    ...
</div>

You want the card to switch layouts when it becomes too narrow.

With media queries, you're forced to react to the viewport:

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

The problem?

The viewport may be large while the card's container is small.

The component doesn't actually know how much space it has available.


Enter Container Queries

Container Queries allow a component to respond to the size of its parent container rather than the viewport.

First, define a container:

.cards-wrapper {
    container-type: inline-size;
}

Then create queries based on that container:

@container (max-width: 500px) {
    .card {
        flex-direction: column;
    }
}

Now the card adapts based on the space it actually has.


A Real Example

Imagine a product card:

<div class="products">
    <article class="product-card">
        <img src="product.jpg" alt="">
        <div class="content">
            <h3>Product Name</h3>
            <p>Description</p>
        </div>
    </article>
</div>

Default layout:

.product-card {
    display: flex;
    gap: 1rem;
}

Make the parent a container:

.products {
    container-type: inline-size;
}

Change layout when space becomes limited:

@container (max-width: 450px) {
    .product-card {
        flex-direction: column;
    }
}

The card becomes fully reusable regardless of where it's placed.


When I Use Container Queries

I find them most useful for reusable components like:

  • Product cards
  • Team member cards
  • Dashboard widgets
  • Pricing tables
  • Sidebar blocks
  • Custom WordPress blocks

Instead of designing around the page, you design around the component itself.


Container Queries vs Media Queries

Use Media Queries when:

  • Adjusting page layouts
  • Changing navigation behavior
  • Responding to viewport size

Use Container Queries when:

  • Building reusable components
  • Creating modular UI systems
  • Designing components that appear in different layouts

I don't see them as competitors.

They solve different problems and often work together.


Browser Support

Container Queries are now supported in all modern browsers, making them practical for production projects.

Always verify support requirements if you're targeting older browsers, but for most modern projects they're ready to use.


Final Thoughts

Container Queries changed the way I think about responsive design.

Instead of asking:

"How wide is the screen?"

I can ask:

"How much space does this component actually have?"

That small shift makes components more reusable, predictable, and easier to maintain.

If you're still relying only on media queries, Container Queries are definitely worth adding to your CSS toolbox.