Everything you need as a full stack developer

CSS Container Queries with element-based responsive design

- Posted in CSS by

TL;DR CSS Container Queries allow elements to adapt based on their own size, not just the viewport size. This new media query type uses @container instead of @media, enabling reusable components that respond to different contexts with more flexible and dynamic layouts.

Unlocking Responsive Design with CSS Container Queries

As a full-stack developer, you're no stranger to the challenges of responsive design. For years, we've relied on media queries to adapt our layouts to different screen sizes and devices. However, this approach has its limitations. What if you want to create a component that responds to its own size, rather than the size of the viewport? That's where CSS Container Queries come in – a game-changing feature that allows you to write element-based responsive design.

What are CSS Container Queries?

Container Queries, also known as "container queries" or "element queries," are a new type of media query that targets elements based on their own size, rather than the size of the viewport. This means you can create components that adapt to their container's dimensions, without relying on media queries.

The syntax is similar to traditional media queries, but instead of using @media, you use @container. The basic structure looks like this:

@container (max-width: 400px) {
  /* styles for when the container is less than or equal to 400px wide */
}

Why do we need Container Queries?

Before Container Queries, our options for responsive design were limited. We could use media queries to target specific viewport sizes, but this approach has several drawbacks:

  • Tight coupling: Media queries are tightly coupled to the viewport size, making it difficult to create reusable components that adapt to different contexts.
  • Limited flexibility: Media queries only allow you to target specific breakpoint sizes, which can lead to rigid and inflexible layouts.

Container Queries solve these problems by allowing you to write responsive styles that are decoupled from the viewport size. This means you can create components that:

  • Adapt to their container's dimensions
  • Are reusable across different contexts
  • Have more flexible and dynamic layouts

Example: Responsive Card Component

Let's say we want to create a card component that adapts its layout based on its container's width. We can use Container Queries to achieve this:

.card {
  display: flex;
  flex-direction: column;
}

@container (max-width: 400px) {
  .card {
    flex-direction: row;
  }
}

In this example, the card component will change its layout from a column to a row when its container is less than or equal to 400px wide. This means we can use the same card component in different contexts – such as on a narrow mobile screen or within a wider desktop layout – and it will adapt accordingly.

Tricks and Techniques

Here are some more advanced techniques for working with Container Queries:

  • Using min-width and max-width: You can use both min-width and max-width to target containers that fall within a specific size range.
@container (min-width: 400px) and (max-width: 800px) {
  /* styles for when the container is between 400px and 800px wide */
}
  • Using width: You can use the width property to target containers that have a specific width.
@container (width: 500px) {
  /* styles for when the container is exactly 500px wide */
}
  • Combining Container Queries with media queries: You can combine Container Queries with traditional media queries to create more complex responsive designs.
@media (max-width: 600px) {
  @container (min-width: 300px) {
    /* styles for when the viewport is less than or equal to 600px and the container is at least 300px wide */
  }
}

Conclusion

CSS Container Queries are a powerful tool for creating responsive designs that adapt to their context. By targeting elements based on their own size, rather than the size of the viewport, you can create more flexible, reusable, and dynamic components.

Whether you're building a complex web application or just want to improve your responsive design skills, Container Queries are definitely worth exploring. With this feature, you'll be able to unlock new levels of creativity and productivity in your front-end development work.

Get started with Container Queries today!

Note: At the time of writing, Container Queries are supported in Chrome 105+ and Safari Technology Preview Release 141+. Support is expected to improve over time as more browsers adopt this feature.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more