Container queries in css

Container queries are fully supported by the main browsers. They work similarly to media queries, with only a difference in what they are based on. Media queries depend on viewport size, while container ones depend on element size. Table of contents Practical use of container queries Basic syntax Naming Container query length units Important notes Practical use of container queries Since container queries work similarly to media queries, in what cases would we prioritize container queries over media ones? For example, when we have elements with the same style in different wrappers, where structure depends on the wrapper's size: When item class element (in main and aside component) has enough width it splits into two columns. Basic syntax Let's learn with an example: Title lorem First, we need to set container-type property for our container, on the sizes of which we will use a query. Values of container-type: size - both dimensions; inline-size - inline dimension; normal - not for dimension changes but for container style queries (we can refer to container sizes inside the query). For our example: .container { container-type: inline-size; } Next, we should use the css at-rule @container to define the query. It's set similar to media queries: @container (width

Jan 22, 2025 - 04:08
 0
Container queries in css

Container queries are fully supported by the main browsers. They work similarly to media queries, with only a difference in what they are based on. Media queries depend on viewport size, while container ones depend on element size.

Table of contents

  1. Practical use of container queries
  2. Basic syntax
  3. Naming
  4. Container query length units
  5. Important notes

Practical use of container queries

Since container queries work similarly to media queries, in what cases would we prioritize container queries over media ones?

For example, when we have elements with the same style in different wrappers, where structure depends on the wrapper's size:

When item class element (in main and aside component) has enough width it splits into two columns.

Basic syntax

Let's learn with an example:

 class="container">
  class="child">
  

Title

lorem