提示:本网站现在为维护调试状态

Complete Guide to CSS Container Queries

Choyeon
Choyeon
Sep 1, 2026 1 min read 532 Views
348 words 1,996 chars Updated Nov 8, 2026
Complete Guide to CSS Container Queries
Table of contents

For years responsive relied on media queries (viewport-based), but components live in arbitrary widths. Container Queries + :has() deliver true component-level responsiveness.

Container Queries

Declare container-type: inline-size on parent; use @container in children to switch layout by parent width. Pair with cqw/cqh/cqi units for contextual adaptation.

.card-wrapper { container-type: inline-size; container-name: productCard; }
.product-card {
  display: flex; flex-direction: column; padding: 1rem;
  border: 1px solid var(--border); border-radius: 12px; transition: all 0.2s;
}
.product-image { width: 100%; aspect-ratio: 4 / 3; }
.product-info { display: grid; grid-template-columns: 1fr; gap: 0.5rem; }
.product-title { font-size: 1rem; }
.product-price { font-weight: 700; color: var(--primary); }

@container productCard (min-width: 400px) {
  .product-card { flex-direction: row; gap: 1rem; }
  .product-image { width: 45%; aspect-ratio: 1 / 1; }
  .product-title { font-size: 1.125rem; }
}
@container productCard (min-width: 640px) {
  .product-card { padding: 1.5rem; }
  .product-info { grid-template-columns: 2fr 1fr; }
  .product-title { font-size: 1.25rem; grid-row: span 2; }
  .product-price { font-size: 1.5rem; align-self: end; }
}

.product-card:has(.out-of-stock) { opacity: 0.7; filter: grayscale(0.3); }
.product-card:has(input[type="checkbox"]:checked) {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 30%, transparent);
}
.product-wrapper:has(> .product-card:hover) .product-card:not(:hover) {
  transform: scale(0.98); opacity: 0.85;
}

.hero-title { font-size: clamp(1.5rem, 8cqi, 3rem); line-height: 1.1; }
.hero-cta { padding: 0.05cqw 0.2cqh; border-radius: 0.1cqh; }

The :has() Parent Selector

CSS traditionally styled ancestors -> descendants one-way. :has() breaks the limit: style ancestors by descendant presence/state for hover-sibling-dim, form validation, empty-state styling.

Feature @media @container :has()
Trigger Viewport size Parent size Descendant state
Scope Page-level Component-level Any ancestor
Units vw/vh/vmin cqw/cqh/cqi -
Support Universal Modern evergreen Chrome/Safari/Firefox

Best Practices

Avoid deeply nested :has() and universal :has(*:hover). Pair container queries with explicit container-name to prevent multi-instance collisions.

X微博Telegram

About the author

Poll

你最希望 Rosetta 优先加入哪项功能?

社区投票(演示数据,可后台删除)

Comments (2)

0 / 3000
代码幽灵
代码幽灵ghostdev.blogSep 29, 2026

作为一个后端转全栈的,看你的前端文章从来不会觉得难懂,讲得特别接地气,谢谢!

云端漫步
云端漫步cloud-notes.cloudSep 28, 2026

这个组件封装得太优雅了,拿走直接用在项目里了,省了我半天时间,给博主点个大大的赞!

Related posts

Tailwind CSS v4: Zero-config + CSS-native Variables

Tailwind CSS v4: Zero-config + CSS-native Variables

Tailwind CSS v4 is an architectural upgrade: goodbye tailwind.config.js, declare design tokens in @theme blocks inside native CSS files. Single @import "tailwindcss" entry replaces postcss.config; built-in CSS vars, native container queries, oxc engine 10-40x faster, simplified color functions.

1 min read
React 18 Hooks Best Practices

React 18 Hooks Best Practices

React 18 introduces revolutionary features like concurrent rendering, automatic batching, useTransition, and useDeferredValue. This article systematically covers correct useState/useEffect usage, useMemo/useCallback optimization boundaries, custom Hook design patterns, and a complete guide to avoiding stale closures, race conditions, and other pitfalls in concurrent mode.

1 min read
Frontend Performance: Core Web Vitals in Practice

Frontend Performance: Core Web Vitals in Practice

Practical Google Core Web Vitals playbook: LCP via preconnects/image-priority/CDN, CLS elimination via image dimensions/font-loading/placeholders, INP via long-task splitting + debouncing.

1 min read
Vue 3 Composition API in Practice

Vue 3 Composition API in Practice

Vue 3's Composition API fundamentally changed how Vue components are organized. Through building a complex e-commerce filter component, this article demonstrates ref vs reactive choices, proper computed/watch usage, custom Composable extraction principles, lifecycle hook timing, and the correct approach to cross-layer state via provide/inject.

1 min read
Astro Deep Dive: Islands Architecture & Content Collections

Astro Deep Dive: Islands Architecture & Content Collections

Astro innovations: Islands Architecture for minimal JS with partial hydration, Content Collections for type-safe Markdown/MDX management, ultra-fast zero-JS static sites.

1 min read
PWA Offline App in Practice: Service Worker + IndexedDB + Background Sync

PWA Offline App in Practice: Service Worker + IndexedDB + Background Sync

Build installable, offline-first Progressive Web Apps: Service Worker caching strategies (Cache First, Stale-While-Revalidate), IndexedDB wrapper for structured local data, Background Sync queue that replays offline operations when network returns. Full TypeScript code included.

1 min read