CSS container queries you may not know! !

青灯夜游
Release: 2021-07-01 09:51:26
forward
3343 people have browsed it

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 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.

CSS container queries you may not know! !

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.

CSS container queries you may not know! !

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%;
  }
}
Copy after login

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.

CSS container queries you may not know! !

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:

CSS container queries you may not know! !

# 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.

CSS container queries you may not know! !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

First, 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

CSS container queries you may not know! !#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>
Copy after login

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: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;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><div class="contentsignin">Copy after login</div></div>First, we tell the browser that every element with an item of class

.o-grid

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:

CSS container queries you may not know! !

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.

CSS container queries you may not know! !

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.

CSS container queries you may not know! !

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.

1CSS container queries you may not know! !

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.

1CSS container queries you may not know! !

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.

1CSS container queries you may not know! !

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.

1CSS container queries you may not know! !

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.

1CSS container queries you may not know! !

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="shadeed.jpg" alt="Ahmad Shadeed" />
        <span class="name">Ahmad Shadeed</span>
      </li>
    </ul>
  </aside>
  <main>
    <h2>Main content</h2>
  </main>
</div>
Copy after login
// CSS
.content {
  display: grid;
  grid-template-columns: 0.4fr 1fr;
}

aside {
  contain: layout inline-size style;
}

@container (min-width: 180px) {
  .name {
    display: block;
  }
}
Copy after login

aside 宽度是0.4f,所以它是动态宽度。另外,我添加了contain属性。然后,如果容器宽度大于180px,将显示用户名。

另一个类似的用例是侧导航。我们可以切换导航项标签的位置,从在新行或旁边的图标。

1CSS container queries you may not know! !

当容器很小时,导航项标签是如何从一个新行切换的,当有足够的空间时,导航项标签是如何靠近导航图标的。

示例地址: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!

Related labels:
css
source:segmentfault.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template