Table of Contents
Case 1: Card Grid
In another project, I also need another card component. This time, I need the card to transition from horizontal layout to vertical layout when the screen becomes smaller...and then go back to horizontal layout again, and back to vertical layout.
This is another very common pattern I have used recently. If you use container queries, you will get a more complete product. Suppose you have an icon locked with the title:
Additional content: Other types of container size query
Home Web Front-end CSS Tutorial A Few Times Container Size Queries Would Have Helped Me Out

A Few Times Container Size Queries Would Have Helped Me Out

Mar 09, 2025 am 11:57 AM

A Few Times Container Size Queries Would Have Helped Me Out

CSS container query technology is becoming more and more mature, and many developers are beginning to try to apply it to various projects, even if it is just simple experiments. While browser support is not entirely popular, it is practical enough in some projects, but may not be enough to completely replace media queries in older projects.

Container query is indeed very convenient! In fact, I've been in a few situations and I've been very eager to use it, but I'm limited by browser compatibility. If container query can be used at that time, the effect will be better.

All the following demos are best viewed in Chrome or Safari at the time of writing. Firefox plans to provide support in version 109.

Case 1: Card Grid

Everyone should be familiar with this case, right? This is a very common pattern that we almost encounter. But the truth is that if container queries can be used instead of standard media queries, it will save a lot of time and get better results.

Suppose you need to build a card grid that requires each card to maintain a 1:1 aspect ratio:

This is harder than it seems! The problem is that resizing component contents based on viewport width will allow you to be subject to how the component responds to the viewport, and how any other ancestor containers respond to the viewport. For example, if you want the font size of the card title to decrease when it reaches a specific inline size, there is no reliable way to do this.

You can set the font size using vw units, but the component is still bound to the browser's viewport width. This can cause problems when the card grid is used in other contexts that may not have the same breakpoint.

In my actual project, I took the JavaScript method:

  1. Listen to resize events.
  2. Calculate the width of each card.
  3. Add inline font size according to card width.
  4. Use em units to set the internal style.

It looks like a lot of work, right? But this is a stable solution to achieve the desired scaling in different screen sizes and in different contexts.

Container query will be better because it provides Container query units , such as cqw units. As you may have learned, 1cqw is equal to 1% of the width of the container. We also have cqi units, which is a measure of the inline width of the container, and cqb is used for the container block width. So if we have a 500px wide card container, the value of 50cqw is calculated as 250px.

If I can use container query in my card grid, I can set the .card component to container:

<code>.card { 
  container: card / size;
}</code>
Copy after login
Copy after login
Copy after login

I can then set up an internal wrapper with padding using cqw units that scales by 10% of the width of .card:

<code>.card__inner { 
  padding: 10cqw; 
} </code>
Copy after login
Copy after login
Copy after login

This is a good way to consistently scale the spacing between the edge of the card and its contents, regardless of where the card is used at any given viewport width. No media inquiries are required!

Another idea? Use cqw units as the font size of the internal content, and then apply the fill using em units:

<code>.card { 
  container: card / size;
}</code>
Copy after login
Copy after login
Copy after login

5cqw is an arbitrary value - I just selected a value. The padding is still equal to 10cqw, because the em unit is relative to the .card__inner font size!

Did you notice it? 2em relative to the 5cqw font size set on the same container ! The container works differently than we are used to because the em unit is relative to the font size value of the same element. But I quickly noticed that the container query unit is related to the recent parent (also a container) of . For example, in this example, 5cqw does not scale based on the width of the

element:

.card

Instead, it will scale to the nearest parent defined as the container. That's why I set up a
<code>.card__inner { 
  padding: 10cqw; 
} </code>
Copy after login
Copy after login
Copy after login
wrapper.

.card__innerCase 2: Alternating layout

In another project, I also need another card component. This time, I need the card to transition from horizontal layout to vertical layout when the screen becomes smaller...and then go back to horizontal layout again, and back to vertical layout.

I did this daunting job to make the component become portrait within two specific viewport ranges (a salute to the new media query range syntax!), but the problem is that it is then locked on the media query that is set for it, its parent, and anything else that might respond to the viewport width. We need something that works in any situation without worrying about where the content will be interrupted!

Container query can easily solve this problem due to the use of the

rule:

@container

One query, infinitely smooth:
<code>.card__inner { 
  font-size: 5cqw; 
  padding: 2em;
} </code>
Copy after login
Copy after login

But wait! There are some issues you may need to be aware of. Specifically, using such container queries in prop-based design systems can be difficult. For example, this

component might contain child components that rely on prop to change their appearance.

.info-cardWhy is this important? The portrait layout of the card may require alternate styles, but you cannot change the JavaScript prop using CSS. Therefore, you may repeat the style you want. I actually discussed this and how to solve this in another post. If you need to use container queries for a large number of styles, you may need to build an entire design system around them, rather than trying to stuff them into an existing design system that relies on media queries.

Case 3: SVG Stroke

This is another very common pattern I have used recently. If you use container queries, you will get a more complete product. Suppose you have an icon locked with the title:

Even without media queries, it is easy to scale the icon according to the size of the title. However, the problem is that the
<code>.card { 
  container: card / size; 
  container-name: card; 
  font-size: 5cqw; 
}</code>
Copy after login
Copy after login
of SVG can be too thin to notice in smaller sizes, and can be too thick and too conspicuous in larger sizes.

I had to create and apply classes for each icon instance to determine its size and stroke width. This is OK if the icon is next to a title that uses a fixed font size, but it is not as good when using a constantly changing fluid type.

The font size of the title may be based on the viewport width, so the SVG icon needs to be adjusted accordingly and will work properly at any size. You can set the stroke width relative to the font size of the title by using em units. However, if you need to stick to a specific set of stroke sizes, this method doesn't work because it scales linearly - without using media queries on viewport width, you can't adjust it to a specific stroke width value without resorting to media queries on viewport width.

However, if I have the opportunity to use container query, I will do this:

<code>.card { 
  container: card / size;
}</code>
Copy after login
Copy after login
Copy after login

Compare these implementations and see how the container query version captures the SVG's stroke to the specific width I want based on the width of the container.

Additional content: Other types of container size query

OK, I didn't actually encounter this in the actual project. However, when I looked closely at the information about container queries, I noticed that we can query other contents of containers related to container size or physical size.

The examples I've been using in this post mostly query width, maximum and minimum width, height, block size, and inline size.

<code>.card__inner { 
  padding: 10cqw; 
} </code>
Copy after login
Copy after login
Copy after login

But MDN outlines two other things we can query. One is the direction, which makes a lot of sense because we have been using it in media queries. The same is true for container queries:

<code>.card__inner { 
  font-size: 5cqw; 
  padding: 2em;
} </code>
Copy after login
Copy after login

The other is aspect ratio, believe it or not:

<code>.card { 
  container: card / size; 
  container-name: card; 
  font-size: 5cqw; 
}</code>
Copy after login
Copy after login

This is an editable demonstration that can be used to experiment with these two examples:

I haven't really found good use cases for these two yet. If you have any ideas or feel it can help you with your project, please let me know in the comments!

The above is the detailed content of A Few Times Container Size Queries Would Have Helped Me Out. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles