In front-end development, it is often necessary to design according to different screen sizes to achieve PC and mobile responsiveness. We generally use CSS media queries to detect the viewport width or height and then change the design based on that pattern. This is how web layouts have been designed for the past 10 years.
CSS Container Query, a feature that has long been requested by web developers, will soon appear in CSS. In the latest Chrome Canary, we can use chrome ://flags/#enable-container-queries
Enables the Container Queries function. In this article, I’ll cover what it is, how it will change your workflow as a designer, and more.
Current responsive design status
Currently, we implement responsiveness, which generally requires three styles of UI design, namely mobile, tablet and desktop.
In the picture above, the UI is designed in three versions, so developers can implement it very well, which is very nice (this is only for fear of lazy UI) PC version, this is a pain in the ass).
Now let's take a look at using media queries to see how to implement it.
The picture above is the same component, which has three variants, namely default
, Card
and Featured
. In CSS, developers need to create three variations of this component, where each composition is unique.
.c-media { /* the default styles */ display: flex; flex-wrap: wrap; gap: 1rem; } @media (min-with: 400px) { .c-media--card { display: block; } .c-media--card img { margin-bottom: 1rem; } } @media (min-with: 1300px) { .c-media--featured { position: relative; /* other styles */ } .c-media--featured .c-media__content { position: absolute; left: 0; top: 0; width: 100%; height: 100%; } }
The above variations depend on media queries or viewport width. This means, we cannot control them based on their parent width. Now you may be thinking, what’s the problem here? Oh or, that's a great question.
The problem is that developers only use variants of components when the viewport width is larger than a certain value. For example, if I use the featured
aka PC style in tablet, it doesn't work, why? Because its media query width is greater than 1300px
.
Not only that, we also face a problem when the content is lower than expected. Sometimes, the UP owner may only add one article, but the design is to include three of them. In this case, either we will have an empty space or the project will expand to fill the available space. Consider the following image:
# In the first case (Case 1), the article is too wide, causing the cover to become deformed. The same problem occurs in the second case (Case 2). If you use container queries, we can solve these problems by querying the parent component to decide how to display a specific component. Consider the following diagram, which shows how we can use container queries to fix this problem.
In this case, what if we turn our thoughts to the parent component of the component? In other words, if we query the parent component and determine the component based on the width or height of the parent component What should it look like? Let's take a look at the concept of
.
What is a container queryFirst, let me define the container. It contains an element of another element, generally we call it
wrapper. In the latest Chrome Canary, we can enable the Container Queries function through chrome://flags/#enable-container-queries.
When a component is placed in an item, it is included in the item. This means that we can query the width of the parent element and modify it accordingly. Consider the following image
#Note that each card has a yellow outline that represents the parent component of each component. Using CSS container queries, we can modify a component based on its parent component's width.
<div class="o-grid"> <div class="o-grid__item"> <article class="c-media"></article> </div> <!-- + more items --> </div>
The component is an item with class
.c-media, and its parent is the .o-grid__item
element. In CSS, we can do the following: <pre class='brush:php;toolbar:false;'>.o-grid__item {
contain: layout inline-size style;
}
.c-media {
/* Default style */
}
@container (min-width: 320px) {
.c-media {
/* The styles */
}
}
@container (min-width: 450px) {
.c-media {
/* The styles */
}
}</pre>
First, we tell the browser that every element with an item of class
is a container. Then, you tell the browser that if the parent element's width is equal to or greater than 500px, it should display differently. The same is true for the 700px query. This is how CSS container queries work. Additionally, we can define them wherever we want, which means we can query on the top-level container if needed. Now that you have understood the basic idea of CSS container query, take a look at the picture below to deepen the image.
On the left, this is a viewport being resized. On the right, a component that changes based on the parent component's width. This is the function and use of container queries.
Design with container queries in mind
As a UI, you need to adapt to this revolutionary CSS feature because it will change the way we design for the web. We not only design for the screen size, but also consider how the component should adapt when the width of the container changes.
Nowadays, design systems are becoming more and more popular. The design team will build a set of rules and components so that other members can build the page based on them. With the arrival of CSS container queries, we will also design how a component should adjust based on the width of its parent component.
Consider the following design:
Please note that we have a title, article sections, quotes, and a newsletter. Each of them should fit within the width of the parent view.
I can divide these components into the following parts
- Viewport (media query)
- Parent (container query)
- General: No Affected components such as buttons, labels, paragraphs.
For the sample UI, here is how we divide the components.
When we think with this mindset when designing a UI, we can start thinking about different variations of components that depend on their parent width.
In the image below, notice how each variation of the post component starts with a specific width.
As a designer, it might be a little strange to think about parent width at first, but this is the way forward. We provide front-end developers with details and versions of each component that they can use.
Not only that, we may also have a variant of the component that should only be displayed in a specific context. For example, the event list page. In this case, it is important to know where to use this variant.
The question is, how to tell the designer where these components should be used.
Communicate with developers
Good communication is an important factor in project success. As a designer, we should provide guidance on where component variations should be used. It can be a complete page design or a simple diagram showing how to use each component.
Note how I mapped each variant to a specific context rather than a viewport. To further demonstrate this, let’s look at how our components behave differently when used with CSS Grid.
In a CSS grid, we can tell the browser that we want columns to expand if the number of columns is lower than expected by using the auto-fit keyword (you can read more about this here). This feature is very powerful as it helps us present different variations on the same background.
It is very useful to have a component react to the width of its parent. As you just saw, we're revisiting the desktop-sized page and have different sections, each with a different number of columns.
Avoid complexity when designing responsive components
It’s important to remember that the internal parts of a component are just like a Lego game. We can sort them based on current changes, but everything has a limit. Sometimes, a front-end developer is better off working on an entirely new component rather than using container queries to create variations.
Consider the following.
It has the following content:
- Avatar
- Name
- Button
- Key/Value Pairs
If the internal parts remain the same, or at least don't contain new parts, we can change the component and have multiple variations as shown below.
CSS Container Query Use Cases
Let’s explore some use cases that can be implemented using CSS Container Query.
Chat list
I see this pattern on Facebook messenger. Chat list changes based on viewport width. We can achieve this using CSS container queries.
When there is enough space, the list will expand and display the name of each user. The parent element of the chat list can be a dynamically resized element (for example: using CSS viewport units, or CSS comparison functions).
// HTML <div class="content"> <aside> <ul> <li> <img src="/static/imghwm/default1.png" data-src="shadeed.jpg" class="lazy" alt="Ahmad Shadeed" /> <span class="name">Ahmad Shadeed</span> </li> </ul> </aside> <main> <h2 id="Main-nbsp-content">Main content</h2> </main> </div>
// CSS .content { display: grid; grid-template-columns: 0.4fr 1fr; } aside { contain: layout inline-size style; } @container (min-width: 180px) { .name { display: block; } }
aside
宽度是0.4f
,所以它是动态宽度。另外,我添加了contain
属性。然后,如果容器宽度大于180px,将显示用户名。
另一个类似的用例是侧导航。我们可以切换导航项标签的位置,从在新行或旁边的图标。
当容器很小时,导航项标签是如何从一个新行切换的,当有足够的空间时,导航项标签是如何靠近导航图标的。
示例地址:https://codepen.io/shadeed/pen/Popmryw?editors=0100
英文原文地址:https://ishadee.com/article/contner-queries-for-designers/
作者:AAhmad Shadeed
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of CSS container queries you may not know! !. For more information, please follow other related articles on the PHP Chinese website!

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.

There was once upon a time when native CSS lacked many essential features, leaving developers to come up with all sorts of ways to make CSS easier to write over the years.

HTML 5 Readiness was a site that showed through a rainbow of colors the browser support for several web features. What about a new version?

One thing we can do to help teams code consistently is provide type-checking so that all of the configurable options for a specific component are available while coding. Bryan demonstrates how he does this with TypeScript when working with Astro comp

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!