Home>Article>Web Front-end> CSS container queries you may not know! !
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 usechrome ://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.
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, namelydefault
,Card
andFeatured
. 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 thefeatured
aka PC style in tablet, it doesn't work, why? Because its media query width is greater than1300px
.
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
container query.
What is a container query.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.
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:
First, we tell the browser that every element with an item of class.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 */ } }
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.
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
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.
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.
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:
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.
Let’s explore some use cases that can be implemented using CSS Container Query.
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).
// HTMLMain content
// 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!