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

CSS Container Queries 完全ガイド

Choyeon
Choyeon
2026年9月1日 1 分で読了 531 閲覧
348 字 1,996 文字 更新日 2026年11月8日
CSS Container Queries 完全ガイド
目次

Complete Guide to CSS Container Queries

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

筆者について

投票

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

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

コメント (2)

0 / 3000
代码幽灵
代码幽灵ghostdev.blog2026年9月29日

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

云端漫步
云端漫步cloud-notes.cloud2026年9月28日

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

関連記事

Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数

Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数

Tailwind CSS v4はアーキテクチャ級アップデート:tailwind.config.js を廃止し、ネイティブCSS内の@themeブロックでデザイントークン宣言。@import "tailwindcss" 単一エントリでpostcss設定不要。CSS変数内蔵、Container Queriesネイティブ対応、oxcエンジンで10-40倍高速化、カラー関数簡略化。

1 分で読了
React 18 Hooks ベストプラクティス

React 18 Hooks ベストプラクティス

React 18 は、コンカレントレンダリング、自動バッチング、useTransition、useDeferredValue などの革新的な機能を導入しました。この記事では、useState/useEffect の正しい使い方、useMemo/useCallback の最適化境界、カスタム Hook の設計パターン、コンカレントモードでの古いクロージャや競合状態を回避するための完全なガイドを提供します。

1 分で読了
フロントエンド性能:Core Web Vitals 実践

フロントエンド性能:Core Web Vitals 実践

Google検索順位のCore Web Vitals完全実践:LCP最適化(事前接続・画像優先度・CDN)、CLS除去(画像寸法・フォント・プレースホルダ)、INP改善(長タスク分割・デバウンス)。

1 分で読了
Vue 3 コンポジション API 実践

Vue 3 コンポジション API 実践

Vue 3 のコンポジション API は、Vue コンポーネントの設計手法を根本的に変えました。複雑な EC サイトのフィルターコンポーネントの構築を通じて、ref/reactive の使い分け、computed/watch の適切な使用方法、カスタム Composable の抽出原則、ライフサイクルフックのタイミング、provide/inject による階層を超えた状態受け渡しの正しい方法を解説します。

1 分で読了
Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション

Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション

Astroの革新:最小JSのアイランドアーキテクチャ、型安全なMarkdown/MDXのコンテンツコレクション、ゼロJSの高速静的サイト構築。

1 分で読了
PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

インストール可能・オフラインファーストなPWAを段階的構築:Service Worker キャッシュ戦略(Cache First / Stale-While-Revalidate)、構造化ローカルデータ用IndexedDBラッパー、ネットワーク復旧時にオフライン操作を自動リプレイするBackground Syncキュー。完全TypeScriptコード付き。

1 分で読了