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

推荐订阅源

V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
罗磊的独立博客
小众软件
小众软件
I
InfoQ
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium

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