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

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
CSS Container Query Units
Ahmad Shadeed · 2021-09-18 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

A few days ago, I saw a tweet by Miriam Suzanne about CSS query units being supported. This was originally proposed by Una Kravets on Github. I couldn’t resist experimenting with them and see how we can get even more benefit from CSS container queries.

I will try my best to explain how each unit works, and where we can use a unit(s) to enhance how a component should react to its parent width. If you don’t know about CSS container queries yet, I wrote an intro with lots of examples and also how container queries can affect a designer work. I recommend reading them before reading this article.

A kind reminder: CSS container queries are only supported in Chrome Canary under an experiment flag. To play with it, go to chrome://flags and search for “Enable CSS Container Queries” and enable the feature.

Let’s dive in.

Introduction

In CSS, we have a lot of units that can be used for different purposes. The most used ones are px, rem, and em. If there is something close to how CSS container query units work, then I would say viewport units.

CSS viewport units work in a way that responds to the browser viewport size (width or/and height). That’s great, but we don’t always want to use a unit that is related to the viewport size. What if we want to query against a container width? That’s what query units are for.

To make it more clear, I want to highlight the difference between the viewport units and query units. Consider the following figure:

On the left, the font-size is being controlled by rem and vw and that will be relative to the viewport width. In some cases, this might work, but it can cause unexpected issues because it’s relative to the viewport size.

Query units can save us effort and time when dealing with things like font-size, padding, and margin within a component. Instead of manually increasing the font size, we can use query units instead.

Consider the following example.

We have a card component that starts with a stacked style, all the way to the hero-like style when the container is large. For such a component, we might need to alter these things:

  • Thumbnail size
  • Font size
  • Spacing between the elements

Without CSS query units, we need to manually alter the above.

.card {
  /* The stacked, base style */
}

.card__title {
  font-size: 1rem;
}

/* The horizontal style, v1 */
@container (min-width: 400px) {
  .card__title {
    font-size: 1.15rem;
  }
}

/* The horizontal style, v2 */
@container (min-width: 600px) {
  .card__title {
    font-size: 1.25rem;
  }
}

/* The hero style */
@container (min-width: 800px) {
  .card__title {
    font-size: 2rem;
  }
}

Notice how the .card__title font size is being changed with different container queries. We can enhance that and avoid duplicating font-size by using query units along with CSS clamp() function.

.card__title {
  font-size: clamp(1rem, 3qw, 2rem);
}

With that, we need to define the font-size only once. If we want to compare this visually, here is how it looks:

If you’re familiar with CSS viewport units, this isn’t new for you. It’s just like querying against the container rather than the browser’s viewport. Neat, no?

Demo

Now that we have an idea about the problems query units are supposed to solve, let’s get the units. According to the CSS spec:

  • Query Width (qw): 1% of the container width. For example, 5qw will resolve to 5% of the container’s width.
  • Query Height (qh): 1% of the container height. For example, 5qh will resolve to 5% of the container’s height.
  • Query Minimum (qmin): the smaller value of Query Width qw or Query Height qh.
  • Query Maximum (qmax): the larger value of qw or qh.

We also have two other units: qi and qb and they mean Query Inline and Query Block, respectively. They can be used for cases where we have different writing modes.

Use cases and examples

Card component

In addition to the previously explained example about cards, we can use query units for a stacked card where we want the font size to be slightly bigger based on the container width.

Notice that the font size is slighter getting bigger based on the container size. This can be useful for creating a stacked card component that works no matter where it’s placed.

Demo

Change font-size based on the importance

When an element is bigger than another, it’s more likely an indicator that it’s more important. A good example of this is aside and main. The headline size in an <aside> should be smaller than the headline in a <main> section.

Notice how the headline in the <aside> is smaller than the one in <main>. We can easily do that thanks to query units.

First, we need to define <aside> and <main> as inline-size containers.

aside,
main {
  container: inline-size;
}

Then, we will use a query unit for the section title. We can also use query units for the bottom margin.

.section-title {
  font-size: clamp(1.25rem, 3qw, 2rem);
  margin-bottom: clamp(0.5rem, 1.5qw, 1rem);
}

Demo

Bio Component

A bio can live within a smaller container (e.g: sidebar) or be shown on a mobile viewport, or a large container like a page header.

We can build a component that can adapt to its container width. In the example, the user avatar and font sizes change based on their container width.

.bio {
  container: inline-size;
}

.c-avatar {
  --size: calc(60px + 10qw);
  width: var(--size, 100px);
  height: var(--size, 100px);
  margin-bottom: clamp(0.5rem, 3qmin, 2rem);
}

Demo

Headings with counters

In some cases, we need to show a counter next to a heading with an article body. This is a perfect use case for query units.

.article-body {
  counter-reset: heading;
}

h2 {
  container: inline-size;
  font-size: clamp(1.25rem, 3qw, 2rem);
  margin-bottom: clamp(0.5rem, 1.5qw, 1rem);
}

h2:before {
  --size: calc(1.25rem + 3qw);
  content: counter(heading);
  counter-increment: heading;
  width: var(--size);
  height: var(--size);
  font-size: calc(0.85rem + 1qw);
  margin-right: calc(var(--size) / 4);
}

Demo

Dynamic Gaps

We’ve been using viewport units to create dynamic gaps with CSS grid. It’s something like:

.wrapper {
  gap: calc(1rem + 2vw);
}

That’s cool, but what about components within specific wrappers? Using viewport units won’t work. That’s why using query units is a good solution for such a case.

.card {
  display: flex;
  flex-direction: column;
  gap: calc(0.5rem + 1qmin);
}

The gap will be a bit bigger when the component switch to the horizontal style.

What happens if we use query units without defining containers?

According to the spec:

If no eligible query container is available, then use the small viewport size for that axis.

The browser will deal with query units as if they were viewport units. Consider the following example:

h2 {
  font-size: clamp(1.25rem, 3qw, 2rem);
}

If there is no container that is defined for the <h2>, the browser will consider 3qw as if it was 3% of the viewport width. That means, using query units without defining containers can lead to unexpected results.

I hope you enjoyed the article. Thanks for reading!