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

推荐订阅源

博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
爱范儿
爱范儿
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
月光博客
月光博客
Martin Fowler
Martin Fowler
A
About on SuperTechFans
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
博客园 - 聂微东
宝玉的分享
宝玉的分享

Miriam Eric Suzanne

Butter bells, fresh from the kiln Butter bells, fresh from the kiln Butter bells, fresh from the kiln I had to look it up I Laser-cut pottery throwing gauge Tech continues to be political Aggregating my distributed self A web component for CodePen embeds? We don Eleventy buckets & cascade layers A slash-why proposal User styles on the web Custom element, two ways New year, same (terrible) Mia CSS @scope Reclaiming my time Cascade Layers Javascript automation on Mac Personal Histories Ancient Web Browsers Critical CSS? Not So Fast! CSS tie-dye gradient backgrounds Personal website redesign Request for Comments: Sass Color Spaces A long-term plan for logical properties? Container queries in browsers! I never let things be small A whole cascade of layers This content won
Container queries explainer & proposal
2021-05-02 · via Miriam Eric Suzanne

Media-queries allow an author to make style changes based on the overall viewport dimensions – but in many cases, authors would prefer styling modular components based on their context within a layout.

bookmark of CSS Sandbox

My proposal builds on David Baron’s @container proposal, which works by applying size & layout containment to the queried elements. Any element with both size & layout containment can be queried using a new @container rule, with similar syntax to existing media-queries.

The approach has been approved by the CSS Working Group, to be added in CSS Containment Level 3, with me as one of the authors.

See the Contain 3 Project for issues and progress.

Establishing Containers

The first step is to establish containers. Any element can become a container, by adding layout, size (generally inline-size), and style containment:

/* Establish containers */
main, aside {
  contain: layout inline-size style;
}

This syntax works in the prototype, but is clearly not ideal for readable and clear code. There is an open issue to discuss better syntax for establishing query containers

Note: See the Editor’s Draft Specification for the latest syntax. The Chrome prototype already has basic support for container-type and container-name.

Writing Queries

Each container creates a containment context that can be queried by their descendant elements. We can query the current containment context (the nearest ancestor container) using an @container rule that is very similar to existing @media queries:

/* Change styles according to container size */
@container (min-width: 35em) {
  .media {
    grid-gap: 1rem;
    grid-template: "media content" auto / max-content 1fr;
  }
}

The .media class above is now responsive to any container it is in. Each instance of the .media class will query its nearest container.

That means we can have one .media element that moves around, responding to the context we put it in – or multiple .meia elements, each one responding individually to its own location.

Prototype

Chrome has released a prototype in their nightly build. To experiment with the feature, and leave feedback while it is still in development:

  1. Download Chrome Canary,
  2. Go to chrome://flags in the url bar, and
  3. Search for “CSS Container Queries” & enable the experimental feature
  4. Restart the browser

This is a draft prototype and may not match the final design.

Demos & Articles

For a quick introduction, check out:

Or dig into more resources, for the full details:

© Miriam Eric Suzanne,
protect trans kids