Blog Details
MD Rasedul Islam
11 Oct 2024
7 min read
CSS Container Queries are a powerful tool in modern web design that allow you to style elements based on the size of their container, not just the size of the browser window. Unlike traditional media queries that respond to the viewport's size, container queries enable a more component-based approach to responsive design.
For example, let’s say you have a card inside a flexible grid layout. As the grid changes in size, the card might need to adjust its appearance to stay user-friendly. With CSS Container Queries, you can make the card's styles adapt dynamically, based on the available space inside the grid cell.
In traditional responsive design, media queries have been our go-to solution to adapt layouts to different screen sizes. However, media queries apply styles based on the size of the entire viewport. This works well in simple layouts, but as websites become more complex, with components that need to be flexible in different contexts, media queries fall short.
This is where CSS Container Queries come in. They allow you to style an element based on the space it has inside its parent container. For example, if a component is displayed in a narrow column, you might want it to use a smaller font size and more compact layout. With container queries, the component can automatically adapt to the space it's given.
CSS Container Queries are particularly useful when working with flexible and reusable components, especially in responsive or adaptive layouts. Some common use cases include:
Component-based design: If you’re using a design system with reusable components (like cards, modals, or widgets), container queries allow each component to adjust its styling based on the size of the space it occupies, regardless of the overall viewport size.
Complex grid or flexbox layouts: In layouts where containers can have vastly different sizes depending on screen width or user interaction, container queries allow elements inside those containers to resize, change layout, or restyle accordingly.
Using CSS Container Queries involves two main steps: defining a container and then writing container queries to adjust styles based on the size of that container.
Step 1: Define the Container
To apply container queries, first, you need to declare an element as a container. You do this by setting the container-type
property. Here’s a simple example:
.container {
container-type: inline-size;
}
This tells the browser to monitor the size of .container
in terms of its inline-size (usually the width).
Step 2: Write the Container Query
Next, you can write a container query to style elements based on the size of the container. Here's an example where we change the background color of a .box
when the container is at least 400 pixels wide:
@container (min-width: 400px) {
.box {
background-color: lightblue;
font-size: 20px;
}
}
In this example, when the .container
element is at least 400 pixels wide, the .box
inside it will get a light blue background and larger font size.
Step 3: Put It All Together
Here’s how the HTML and CSS might look in a real-world scenario:
<div class="container">
<div class="box">Resize me!</div>
</div>
And the CSS:
.container {
container-type: inline-size;
padding: 20px;
border: 2px solid black;
}
.box {
background-color: lightgray;
padding: 10px;
}
@container (min-width: 400px) {
.box {
background-color: lightblue;
font-size: 20px;
}
}
As the .container
changes size, the styles of the .box
will adapt accordingly based on the container’s width, not the browser window.
Now that you understand the what, why, and when of CSS Container Queries, let’s talk about the benefits:
Increased Flexibility in Responsive Design:
Component-Based Adaptability:
Simplified CSS for Complex Layouts:
Better Reusability and Scalability:
CSS Container Queries are a game-changer in responsive design, allowing you to build smarter, more adaptable components that respond to their container size. By using container queries, you can achieve greater flexibility, improve reusability, and simplify your code for more complex layouts.
container-type
property to define a container and write @container
rules to style elements based on the container’s dimensions.Don’t worry, we don’t spam!