A Complete Guide To CSS Container Queries
Tags: webdev, tutorial, programming, css
Published: 2022-12-09 17:49:22

In 2007, Steve Jobs launched the first iPhone, which revolutionized the world. But because of that, many businesses dealt with the problem of changing the layout of websites from desktop to mobile by delivering completely different mobile-compatible websites under the subdomain of ‘m’ (e.g., https://m.facebook.com). And we were all trying to figure out how to work in this new world of contending with mobile and desktop screen sizes.

But after a few years, in 2010, Ethan Marcotte introduced the concept of Responsive Design. [Responsive Web Design](https://www.lambdatest.com/learning-hub/responsive-design?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) is the idea that design and development should respond to the user’s behavior and environment based on the screen size, device, and orientation. And central to that idea was the availability of [CSS Media Query](https://www.lambdatest.com/blog/css-media-queries-for-responsive-design/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog), which allowed setting various layout rules depending on the size of the viewport.

The responsive design opened up many more layout solutions with the help of Media Query. Frameworks like [Bootstrap](https://www.lambdatest.com/blog/css-grid-vs-bootstrap/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) rose in popularity due to providing responsive grid systems. As per W3Techs, Bootstrap is used by 19.3% of all websites, with a market share of 79.5%.

Nowadays, component libraries (ReactJS) have gained popularity because of its **‘build once, deploy anywhere’** nature. A component is built once and can be used anywhere in the layout to make complex interfaces more efficient. And those components come together to create a webpage.

Currently, you can only change the webpage layout with respect to the viewport using Media Query. But when writing media queries for UI elements, we are trying to find a **“magic”** viewport value for a breakpoint like **320px — 480px** for mobile devices, **481px — 768px** for tablets/iPads, **769px — 1024px** for small laptops and **1025px — 1200px** for big screen laptops/pc, so that the target element has minimum dimensions, and the layout doesn’t break. This value usually differs from the viewport dimension and leads to bugs when the inner container’s dimension or layout changes.

![](https://cdn-images-1.medium.com/max/2400/0*pEohGIFS1C55Hwj_.jpg)

This is one of the issues that **CSS Container Queries** attempt to fix. **CSS Container Queries extend existing media query** functionality with queries that depend on the target element dimensions. And in this blog, we will dive deep into the concept of **CSS Container Queries**.

***Perform browser automation testing on the most powerful cloud infrastructure. Leverage LambdaTest [automation testing platform](https://www.lambdatest.com/automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=webpage) for faster, reliable and scalable experience on cloud.***

## Getting Started with CSS Container Queries

The most important thing to know about CSS Container Queries is that “containers” are the elements being queried, but rules within the CSS Container Queries affect only the descendants of the container. In other words, you may define the ‘body’ element, ‘ main’ element, or perhaps event list items. Then, CSS Container Queries will allow defining rules for how elements within those change across container sizes.

CSS Container Queries are not as straightforward as regular media queries. You’ll have to add the **container** property to the parent UI element to make CSS Container Queries work, but there’s a reason, and we’ll cover that next.

## Browser Support

CSS Container Queries are by default enabled in Chrome (the latest version). For smooth and better performance, you can install the latest browser.

![](https://cdn-images-1.medium.com/max/2800/0*8dtMul1159GP4_zQ.png)

***Note:** If you are not using the above-recommended browser, the following CodePens might not work. So an image showcasing the intended working example will be provided alongside the CodePens demo.*

***Accelerate your release velocity with blazing fast [test automation cloud](https://www.lambdatest.com/automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=webpage)!***

## Contain Property

CSS contain property has been added to most modern browsers and has a decent 90% browser support when writing this article.

![](https://cdn-images-1.medium.com/max/2800/0*-hy3ZGN-EtzkRQCz.png)

The contain property is mainly used for performance optimization by hinting to the browser which parts of the page can be treated as independent and won’t affect the other elements of the page. If a change occurs in a single element, the browser will re-render only that part instead of the whole page. With contain property values, we can specify which types of containment we want to use — layout, size, paint, content, or none.

The level 3 draft specification adds the **inline-size** and **block-size** keywords to **contain**. In addition, the draft specification proposes some new properties:

* **Container-type**

Defines an element as a **query container**. Descendants can query aspects of its sizing, layout, and state.

* **Container-name**

Specifies a list of **query container names** for **@container** rules to filter which query containers are targeted.

* **Container**

A shorthand property to set both **container-type** and **container-name**.

***Try an online [Selenium automation](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=webpage) Grid to run your browser automation testing scripts. Our cloud infrastructure has 3000+ desktop & mobile environments. Try for free!***

## Container-Type

It assigns a query container to an element. Depending on the value, descendants can query aspects of its size, layout, style, and state of the object. This helps to make setting the **contain** property easier with arguments that combine different properties.

The container type property can have the following values:

* **Size:** Creates a query container that supports dimensional queries on both the inline and block axis.

* **Inline-size:** Creates a query container that supports dimensional queries on the inline axis.

* **Block-size:** Creates a query container that supports dimensional queries on the block axis.

* **Style:** Allows for style queries by defining a query container.

* **State:** Allows for state queries by defining a query container.

Size, Inline-size, and Block-size are also given layout, size, and style containment.

But what does the CSS Contain property used for optimization have to do with CSS Container Queries? For CSS Container Queries to work, the browser must know if a change occurs in the element’s children layout. It should re-render only that component. The browser will know to apply the code in the container query to the matching component when the component is rendered or the dimension of the container changes.

Let’s have a look at contain property with a small code example.

HTML:

    <div class="wrapper">
        <div class="container">
          <article class="card">
            <header>
              <h3 class='header'>
                Order Pizza Anywhere & Anytime
              </h3>
            </header>
     
            <div class="image"><img src="https://i.imgur.com/OzPVEvW.jpg" alt="a pizza"></div>
     
            <div class="content">
              <p class='para'>Delicioussss Pizza at your doorstep!! All you have to do click the 'Order' button and enjoy your amazing pizza </p>
              <button class='btn'>Order now</button>
            </div>
     
          </article>
        </div>

**CSS :**

    * {
              box-sizing: border-box;
            }
     
            img {
              max-width: 100%;
              height: 100;
              display: block;
            }
     
            body {
              font: 1.2rem Roboto, sans-serif;
              background: whitesmoke;
              line-height: 1.5;
            }
     
            .wrapper {
              padding: 20px;
              display: grid;
              grid-template-columns: 2fr 1fr;
              gap: 20px;
            }
     
            .card {
              background: white;
              box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
              border-radius: 12px;
              display: grid;
              overflow: hidden;
              padding: 15px;
            }
     
            .card .image {
              border-radius: 10px;
            }
     
            .image > img {
              border-radius: 10px;
            }
     
            .card .content {
              padding: 10px;
            }
     
            .para {
              font-size: 14px;
            }
     
            .btn {
              cursor: pointer;
              color: black;
              background: lightgray;
              padding: 10px 20px;
              border-radius: 5px;
              border: none;
            }
     
            .btn:hover {
              background: gray;
              color: white;
            }
     
            .card h2 {
              margin: 0;
              padding: 10px;
            }
     
            .container {
              container-type: inline-size;
            }
     
            @container (min-width: 500px) {
              .card {
                grid-template-columns: 1fr 2fr;
                grid-template-rows: auto 1fr;
                align-items: start;
                column-gap: 20px;
              }
     
              .card h2 {
                padding: 0;
                margin: 0.5em 0 0 0;
              }
     
              .card header {
                grid-row: 1;
                grid-column: 2;
              }
     
              .card .image {
                grid-row: 1 / 3;
                grid-column: 1;
              }
     
              .card .content {
                grid-column: 2;
                grid-row: 2;
                padding: 0 20px 20px 0;
              }
            }

As we can see in the above code block, we are not adding the ‘container’ property to a more distant parent like ‘wrapper’ and keeping the container as close to the affected element as possible.

That is why we should correctly signal the browser about the change. Wrapping a distant parent element with a ‘container’ property can be counter-productive and negatively affect page performance. In worst-case scenarios of misusing the ‘container’ property, the layout may break, and the browser won’t render it correctly.

That’s all for now about the ‘container’ property. Now let’s look at the CSS Container Queries and how they work.

***Automate [cypress testing](https://www.lambdatest.com/cypress-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=webpage) and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop & mobile environments. Try for free!***

## What are CSS Container Queries?


A Container (also called a Wrapper) is an element that contains another element(s). So in simple terms, CSS Container queries specify changes to an element in relation to its parent container or element rather than the entire viewport.

![**(Desktop — 1084px)**](https://cdn-images-1.medium.com/max/2000/1*g6861nsBzpdcLWp4xk0XnA.jpeg)

![**(Mobile — 386px)**](https://cdn-images-1.medium.com/max/2000/1*U3rqfQeYngkfufgocrA4QQ.png)


When a component is placed within an item, it’s contained within it. That means we can query the width of its parent and modify the component based on that. CSS Container Queries enable developers to approach responsiveness from a component standpoint, where elements on a page are designed to be responsive as individual units. This means that regardless of where these elements are placed on the page, they will adjust based on the parent element’s size rather than the entire page’s size.

After the container property has been added to the card element wrapper in the above code example, we can write a container query. We’ve added a container property to an element with a container class, so now we can include any of its child elements in a container query.

    \    .container {
              container-type: inline-size;
            }

Just like a regular media query, we need to define a query using **min-width** or **max-width** properties and nest all selectors inside the block. However, we’ll use the **@container** keyword instead of **@media** to define a container query.

     @container (min-width: 500px) {
              .card {
                grid-template-columns: 1fr 2fr;
                grid-template-rows: auto 1fr;
                align-items: start;
                column-gap: 20px;
              }
     
              .card h2 {
                padding: 0;
                margin: 0.5em 0 0 0;
              }
     
              .card header {
                grid-row: 1;
                grid-column: 2;
              }
     
              .card .image {
                grid-row: 1 / 3;
                grid-column: 1;
              }
     
              .card .content {
                grid-column: 2;
                grid-row: 2;
                padding: 0 20px 20px 0;
              }
            }

HTML:

    <div class="wrapper">
      <div class="container">
        <article class="card">
          <header>
            <h3 class='header'>
              Order Pizza Anywhere
            </h3>
          </header>
    
          <div class="image"><img src="https://i.imgur.com/F5W5bY6.jpg" alt="a pizza"></div>
    
          <div class="content">
            <p class='para'>Delicioussss Pizza at your doorstep!! All you have to do click the 'Order' button and enjoy your amazing pizza </p>
            <ul>
              <li>Loaded with Cheese</li>
              <li>Rich in taste</li>
              <li>50% off on first order</li>
            </ul>
            <button class='btn'>Order now</button>
          </div>
    
        </article>
      </div>
    
    

CSS:

    * {
      box-sizing: border-box;
    }
    
    img {
      max-width: 100%;
      height: 100;
      display: block;
    }
    
    body {
      font: 1.2rem Roboto, sans-serif;
      background: whitesmoke;
      line-height: 1.5;
    }
    
    .header{
    }
    
    .wrapper {
      padding: 20px;
      display: grid;
      grid-template-columns: 2fr 1fr;
      gap:20px;
    }
    
    .card {
      background: white;
      box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      border-radius: 12px;
      display: grid;
      overflow: hidden;
      padding: 15px;
    }
    
    .card .image {
    /*   border: 10px solid #000; */
      border-radius: 10px;
    }
    
    .image > img{
      border-radius: 10px;
      height: 400px;
      object-fit:cover;
    }
    
    .card .content {
      padding: 10px;
    }
    
    .content > ul{
        display:none;
      }
    
    .para{
      font-size: 14px;
    }
    
    .btn{
      cursor: pointer;
      color: black;
      background: lightgray;
      padding : 10px 20px;
      border-radius: 5px;
      border: none;   
    }
    
    .btn:hover{
      background: gray;
      color: white;
    }
    
    .card h2 {
      margin: 0;
      padding: 10px;
    }
    
    .container{
      container-type: inline-size;
    }
    
    @container (min-width: 500px) {
      .card {
        grid-template-columns: 1fr 2fr;
        grid-template-rows: auto 1fr;
        align-items: start;
        column-gap: 20px;
      }
      
      .card h2 {
        padding: 0;
        margin: .5em 0 0 0;
      }
    
      .card header {
        grid-row: 1;
        grid-column: 2;
      }
    
      .card .image {
        grid-row: 1 / 2;
        grid-column: 1;
      }
    
      .card .content {
        grid-column: 2;
        grid-row: 1/2;
        padding: 60px 20px 20px 0;
      }
      
      .content > ul{
        display:flex;
        flex-direction:column;
        list-style:none;
        padding-left:0;
        font-size:14px;
        color: green;
        line-height:30px;
      }
    }

![](https://cdn-images-1.medium.com/max/2000/1*Mq5Ba8znDPrv0uuFWMJ8Eg.png)

![](https://cdn-images-1.medium.com/max/2000/1*fcfyUF7OU-rU2xTE74EYYQ.png)


CSS Container Queries provide a new and refreshing approach to responsiveness. However, it is by no means a replacement for media queries. Instead, consider it a cool new addition to the developer’s responsive toolbox.

***This Cypress automation testing tutorial will help you learn the benefits of [cypress automation](https://www.lambdatest.com/blog/cypress-test-automation-framework/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog), and how to install Cypress and execute Cypress automation testing over scores of browsers and operating systems online.***

## What problems do CSS Container Queries solve?


CSS Container Queries offer a unique solution for developers working on large-scale projects where different components make up different parts of the webpage. They allow components to modify their appearance to fit the context in which they’re put. This means that the components can be used repeatedly, making them reusable.

Many websites have components that need to change their layout based on the container’s available width. This may not always be related to the viewport’s size but to where the component is placed in the layout. CSS Container Queries provide a solution for developers to change component style and layout depending on the available width of the container or parent element.

Here’s a proper example so that we can see the power of container queries:

HTML:

    <div class="wrapper">
        <div class="left">
          <article class="card">
            <header>
              <h2>Little Doggo</h2>
            </header>
     
            <div class="image"><img src="https://i.imgur.com/0l8iHZD.jpg" alt="a little puppy in the grass"></div>
     
            <div class="content">
              <p>A dog is a beautiful creature on the earth that consists of four legs and sharp teeth. The teeth of a dog are tough and bright, which helps in biting the flesh of the body. Dogs have an intelligent nature, which is why they help officers in catching thieves and strangers. They consist of a very loud barking nature..  </p>
            </div>
     
          </article>
        </div>
        <div class="right">
        <article class="card">
            <header>
              <h2>Little Doggo</h2>
            </header>
     
            <div class="image"><img src="https://i.imgur.com/0l8iHZD.jpg" alt="a little puppy in the grass"></div>
     
            <div class="content">
              <p>A dog is a beautiful creature on the earth that consists of four legs and sharp teeth. The teeth of a dog are tough and bright, which helps in biting the flesh of the body. Dogs have an intelligent nature, which is why they help officers in catching thieves and strangers. They consist of a very loud barking nature.. </p>
            </div>
     
          </article>
        </div>
      </div>

**CSS :**

    * {
      box-sizing: border-box;
    }
     
    img {
      max-width: 100%;
      display: block;
    }
     
    body {
      font: 1em Roboto, sans-serif;
      background: whitesmoke;
    }
     
    .wrapper {
      display: grid;
      grid-template-columns: 2fr 1fr;
      gap:20px;
    }
     
    .card {
      background: white;
      box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      border-radius: 12px;
      display: grid;
      overflow: hidden;
    }
     
    .card .image {
    /*   border: 10px solid #000; */
      border-radius: 10px;
    }
     
    .card .content {
      padding: 10px;
    }
     
    .card h2 {
      margin: 0;
      padding: 10px;
    }
     
    /* make the two grid items a containment context */
    .left,
    .right{
      container-type: inline-size;
    }
     
    @container (min-width: 500px) {
      .card {
        grid-template-columns: 1fr 2fr;
        grid-template-rows: auto 1fr;
        align-items: start;
        column-gap: 20px;
      }
     
      .card h2 {
        padding: 0;
        margin: .5em 0 0 0;
      }
     
      .card header {
        grid-row: 1;
        grid-column: 2;
      }
     
      .card .image {
        grid-row: 1 / 3;
        grid-column: 1;
      }
     
      .card .content {
        grid-column: 2;
        grid-row: 2;
        padding: 0 20px 20px 0;
      }
    }

HTML:

    <div class="wrapper">
      <div class="left">
        <article class="card">
          <header>
            <h2>Cross Browser Testing</h2>
          </header>
    
          <div class="image"><img src="https://i.imgur.com/dV3ywkg.png"></div>
    
          <div class="content">
            <p>Browser & app testing cloud to perform both exploratory and automated testing across 3000+ different browsers, real devices and operating systems. </p>
          </div>
    
        </article>
      </div>
      <div class="right">
      <article class="card">
          <header>
            <h2>Cross Browser Testing</h2>
          </header>
    
          <div class="image"><img src="https://i.imgur.com/dV3ywkg.png"></div>
    
          <div class="content">
            <p>Browser & app testing cloud to perform both exploratory and automated testing across 3000+ different browsers, real devices and operating systems. </p>
          </div>
    
        </article>
      </div>
    </div>

CSS:

    * {
      box-sizing: border-box;
    }
    
    img {
      max-width: 100%;
      height: 100%;
      display: block;
    }
    
    body {
      font: 1.2rem Roboto, sans-serif;
      background: whitesmoke;
      line-height: 1.5;
    }
    
    .wrapper {
      display: grid;
      grid-template-columns: 2fr 1fr;
      gap:20px;
    }
    
    .card {
      background: white;
      box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      border-radius: 12px;
      display: grid;
      overflow: hidden;
      max-width:400px;
    }
    
    .card .image {
    /*   border: 10px solid #000; */
      border-radius: 10px;
    }
    
    .image > img{
    /*   object-fit:contain; */
    }
    
    .card .content {
      padding: 10px;
    }
    
    .card h2 {
      margin: 0;
      padding: 10px;
    }
    
    /* make the two grid items a containment context */
    .left,
    .right{
      container-type: inline-size;
    }
    
    /* the card is placed as a child of the two grid items, displaying as one or two columns  */
    @container (min-width: 500px) {
      .card {
        grid-template-columns: 1fr 2fr;
        grid-template-rows: auto 1fr;
        align-items: start;
        column-gap: 20px;
      max-width:100%;
        
      }
      
      .card h2 {
        padding: 0;
        margin: .5em 0 0 0;
      }
    
      .card header {
        grid-row: 1;
        grid-column: 2;
      }
    
      .card .image {
        grid-row: 1 /3;
        grid-column: 1;
        height:100%;
    /*     object-fit: cover; */
      }
    
      .card .content {
        grid-column: 2;
        grid-row: 2;
        padding: 0 20px 20px 0;
      }
    }

*Note: View this example in Chrome Canary with container queries enabled.*

For those who aren’t using Chrome canary, here are the output images at different container sizes.

![](https://cdn-images-1.medium.com/max/2728/1*uNfTmBCGYj-8Te5UKwjUKQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*vuWPwrp8B0JfAcEGmRb1Ew.png)

![](https://cdn-images-1.medium.com/max/2000/1*aQAdmv_sEc4mF-7o9rAVVA.png)

We created a simple card that resizes and adapts to fit any layout and container in our example. Our one container has a width of ⅔ and the other one of ⅓. This means our card component will have a width of 1066px and 534px at a viewport width of 1600px.

We want our container to look slightly different at different breakpoints depending on its available space while remaining responsive. We could do this with a media query, but to do that, we’d have to make changes regarding the viewports meta-information rather than the container itself. This means we must constantly watch for changes to the screen size and adjust our components according to that particular size.

Although this might not seem like an issue, it could quickly lead to the use of too many media queries to target if we had multiple containers because we would need to make sure all elements on the page, not just the card component, are responsive.

This is where **CSS Container Queries** come into play. Let’s go over our above code example again and see how we can turn this into a container:

We want our container to look slightly different at different breakpoints depending on its available space while remaining responsive. We could do this with a media query, but to do that, we’d have to make changes regarding the viewports meta-information rather than the container itself. This means we must constantly watch for changes to the screen size and adjust our components according to that particular size.

    <div class="wrapper">
        <div class="left">
          <article class="card">
            <!-- Card Content -->
          </article>

To query elements, we first need to make the parent element a container by setting the **container** property on the parent element. The container property takes two values: **container-type** and **container-name**.

Notice how the element with the **.card** class is wrapped by a parent element? This would be our parent container, and our card would adjust its size, layout, and styles based on the meta information of this element.

Let’s go ahead and make our element with the **left** class a container, so its children elements can be queried based on its meta-information.

    .left{
      container: inline-size;
    }

We set the **container property** to **inline-size** because we want containment to happen on the inline axis.

The container property and value are set on our **.left** class, which automatically turns the container element into a queryable container. So now, we can change the card element whenever the size of our container element changes.

We can change the style or layout of our card element at certain breakpoints. We can set these breakpoints similarly to how media queries breakpoints are set. You can learn more about it by going through our blog on [CSS breakpoints](https://www.lambdatest.com/blog/how-to-use-css-breakpoints-for-responsive-design/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog).

Let’s look at the breakpoints we put in the above example to see how CSS Container Queries handle changes based on our breakpoints and how CSS Container Queries compare to media queries.

In our example, we started by building out how our card component would look when its parent container has the smallest width, which is any width less than 500px. We want it to look like this:

![](https://cdn-images-1.medium.com/max/2000/1*ys6bwg8-HKNQ7FWmQxklIQ.png)

This would be our starting point for our card component. Since its parent container is now a container that can be queried, we can go ahead and start making changes to the layout and style at different breakpoints depending on the current width of the parent container.

We do that by setting breakpoints. Our breakpoint is when our parent container is at a min-width of **500px (500px or greater)**, and we start defining a new look for our card container. Here we want our container to look like this:

![](https://cdn-images-1.medium.com/max/2000/1*UYCeCIuWmH71QZYGrcWWHg.png)

Code Example:

    @container (min-width: 500px) {
        .card {
          grid-template-columns: 1fr 2fr;
          grid-template-rows: auto 1fr;
          align-items: start;
          column-gap: 20px;
        }
       
        .card h2 {
          padding: 0;
          margin: .5em 0 0 0;
        }
     
        .card header {
          grid-row: 1;
          grid-column: 2;
        }
     
        .card .image {
          grid-row: 1 / 3;
          grid-column: 1;
        }
     
        .card .content {
          grid-column: 2;
          grid-row: 2;
          padding: 0 20px 20px 0;
        }
      }

If we want our card element to look slightly different at another breakpoint, we can simply follow the same process for every new breakpoint.

CSS Container Queries enable us to create more robust and reusable UI components that can adapt to nearly any layout and container, as shown in the above two examples. This would’ve been a lot more difficult with media query because it relies on the information of the entire viewport.

With container queries, the card component could be placed in a narrow or wide column on the webpage and still look good and responsive. Furthermore, if there is enough space in our container queries example, it displays as a two-column layout, otherwise, it has a block layout.

In our example above, container queries helped us achieve this:-

A block layout for smaller sections of the webpage:

![](https://cdn-images-1.medium.com/max/2000/1*CgIf_9HxS-c79xfuG2id5A.png)

A two-column layout for when we have more space:

![](https://cdn-images-1.medium.com/max/2000/1*13Wk2rrgKIIRoM543v4J8Q.png)

The examples above show the power of CSS Container Queries and how vital they are in the component-driven development world. Now that we have a basic understanding of CSS Container Queries and how they function, let’s look at a more detailed code example.

HTML:

    <div class="page-container">
      <div class="card-container card-1" data-size="0">
        <div class="card">
          <figure>
            <img src="lamdatest.png" />
          </figure>
          <div>
            <div class="meta">
              <h2>Cloud Selenium Testing</h2>
              <span class="subtitle">A cross-browser testing platform</span>
            </div>
     
            <div class="notes">
              <p class="desc">
                In this episode we narrow our focus on user-preference-based media
                queries, which enable you to create personalized experiences based
                on your users custom settings and needs.
              </p>
              <div class="links">
                <h3>Lists</h3>
                <ul>
                  <li>
                    Selenium Testing →
                    <a href="#">https://www.lambdatest.com/selenium-automation</a>
                  </li>
                  <li>
                    Cypress Testing →
                    <a href="#">https://www.lambdatest.com/cypress-testing</a>
                  </li>
                  <li>
                    Hyper Execute<a href="#"
                      >https://www.lambdatest.com/hyperexecute</a
                    >
                  </li>
                  <li>
                    Visual Regression Cloud
                    <a href="#"
                      >https://www.lambdatest.com/smart-visual-ui-testing</a
                    >
                  </li>
                  <li>
                    Automation Testing Cloud<a href="#"
                      >https://www.lambdatest.com/automation-testing</a
                    >
                  </li>
                </ul>
              </div>
            </div>
     
            <button>Start Free Testing</button>
          </div>
        </div>
      </div>
      <div class="card-container card-2" data-size="0">
        <div class="card">
          <figure>
            <img src="lamdatest.png" />
          </figure>
          <div>
            <div class="meta">
              <h2>Cloud Selenium Testing</h2>
              <span class="subtitle">A cross-browser testing cloud</span>
            </div>
     
            <div class="notes">
              <p class="desc">
                LambdaTest is a cloud-based cross browser testing tool that allows
                you to perform cross browser testing for web applications across
                more than 2000 browsers, operating systems, and devices
              </p>
              <div class="links">
                <h3>Lists</h3>
                <ul>
                  <li>
                    Selenium Testing →
                    <a href="#">https://www.lambdatest.com/selenium-automation</a>
                  </li>
                  <li>
                    Cypress Testing →
                    <a href="#">https://www.lambdatest.com/cypress-testing</a>
                  </li>
                  <li>
                    Hyper Execute →
                    <a href="#">https://www.lambdatest.com/hyperexecute</a>
                  </li>
                  <li>
                    Visual Regression Cloud →
                    <a href="#"
                      >https://www.lambdatest.com/smart-visual-ui-testing</a
                    >
                  </li>
                  <li>
                    Automation Testing Cloud →
                    <a href="#">https://www.lambdatest.com/automation-testing</a>
                  </li>
                </ul>
              </div>
            </div>
     
            <button>Start Free Testing</button>
          </div>
        </div>
      </div>
    </div>

**CSS:**

    body {
      font-family: "Google Sans", system-ui, serif;
      line-height: 1.5;
      padding: 2rem 1rem;
      background: whitesmoke;
    }
     
    .page-container {
      display: grid;
      grid-template-columns: 35% 1fr;
      gap: 1rem;
    }
     
    img {
      width: 100%;
    }
     
    .card {
      display: grid;
      grid-template-columns: 40% auto;
      gap: 2rem;
      margin: 0 auto;
      box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      padding: 1rem;
      border-radius: 7px;
    }
     
    .card-container:before {
      content: attr(data-size);
      position: absolute;
      padding: 4px;
      left: 0;
      right: 0;
      top: -1.4rem;
      width: 6ch;
      margin: 0 auto;
      text-align: center;
      display: inline-block;
      border-radius: 4px;
      background: black;
      color: white;
    }
     
    h2 {
      font-size: 2.5rem;
      font-weight: 400;
      line-height: 1;
    }
     
    h3 {
      font-size: 1.15rem;
      font-weight: 800;
      text-transform: uppercase;
      margin: 1rem 0 0.25rem;
      line-height: 1;
    }
     
    a {
      color: gray;
    }
     
    a:hover {
      color: black;
    }
     
    .subtitle {
      font-size: 1.2rem;
      line-height: 1.2;
      font-weight: 300;
      color: gray;
    }
     
    button {
      border: none;
      padding: 0.7rem;
      background: black;
      color: white;
      font-weight: 600;
      letter-spacing: 0.1rem;
      text-transform: uppercase;
      margin-top: 1rem;
    }
     
    /* Container Queries */
     
    .card-container {
      container-type: inline-size;
    }
     
    @container (max-width: 850px) {
      .links {
        display: none;
      }
     
      .subtitle {
        font-size: 1.25rem;
      }
     
      h2 {
        font-size: 2.2rem;
      }
    }
     
    @container (max-width: 650px) {
      .desc {
        display: none;
      }
     
      h2 {
        font-size: 2rem;
      }
     
      .card {
        gap: 1rem;
      }
    }
     
    @container (max-width: 460px) {
      .card {
        grid-template-columns: 1fr;
        text-align: center;
        gap: 0.5rem;
      }
     
      button {
        display: block;
        margin: 1rem auto 0;
      }
    }
     
    @container (max-width: 300px) {
      h2 {
        font-size: 1.5rem;
      }
     
      .subtitle {
        display: none;
      }
    }

HTML:

    <div class="page-container">
      <div class="card-container">
        <div class="card">
          <figure>
            <img src="https://i.imgur.com/2xiaL5l.png" />
          </figure>
          <div>
            <div class="meta">
              <h2>Cloud Selenium Testing</h2>
              <span class="subtitle">A cross-browser testing platform</span>
            </div>
    
            <div class="notes">
              <p class="desc">In this episode we narrow our focus on user-preference-based media queries, which enable you to create personalized experiences based on your users custom settings and needs.</p>
              <div class="links">
                <h3>Features</h3>
                <ul>
                  <li>Selenium Testing </li>
                  <li>Cypress Testing </li>
                  <li>Hyper Execute </li>
                  <li>Visual Regression Cloud </li>
                  <li>Automation Testing Cloud </li>
                </ul>
              </div>
            </div>
    
            <button>Start Free Testing</button>
          </div>
        </div>
      </div>
      <div class="card-container">
        <div class="card">
          <figure>
            <img src="https://i.imgur.com/2xiaL5l.png" />
          </figure>
          <div>
            <div class="meta">
              <h2>Cloud Selenium Testing</h2>
              <span class="subtitle">A cross-browser testing cloud</span>
            </div>
    
            <div class="notes">
              <p class="desc"> LambdaTest is a cloud-based cross browser testing tool that allows you to perform cross browser testing for web applications across more than 2000 browsers, operating systems, and devices</p>
              <div class="links">
                <h3>Features</h3>
                <ul>
                  <li>Selenium Testing </li>
                  <li>Cypress Testing </li>
                  <li>Hyper Execute </li>
                  <li>Visual Regression Cloud </li>
                  <li>Automation Testing Cloud </li>
                </ul>
              </div>
            </div>
    
            <button>Start Free Testing</button>
          </div>
        </div>
      </div>
    </div>
    

CSS:

    body {
      font-family: 'Google Sans', system-ui, serif;
      line-height: 1.5;
      padding: 2rem 1rem;
      background: whitesmoke;
    }
    
    .page-container {
      display: grid;
      grid-template-columns: 35% 1fr;
      gap: 1rem;
    }
    
    img {
      width: 100%;
    }
    
    .card {
      display: grid;
      grid-template-columns: 40% auto;
      gap: 2rem;
      margin: 0 auto;
      box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      padding: 1rem;
      border-radius: 7px;
    }
    
    h2 {
      font-size: 2.5rem;
      font-weight: 400;
      line-height: 1;
    }
    
    h3 {
      font-size: 1.15rem;
      font-weight: 800;
      text-transform: uppercase;
      margin: 1rem 0 0.25rem;
      line-height: 1;
    }
    
    li {
      color: gray;
      cursor:pointer;
      letter-spacing:1px;
    }
    
    li:hover{
      color: black;
    }
    
    .subtitle {
      font-size: 1.2rem;
      line-height: 1.2;
      font-weight: 300;
      color: gray;
    }
    
    button {
      border: none;
      cursor:pointer;
      padding: 0.7rem;
      background: black;
      color: white;
      font-weight: 600;
      letter-spacing: 0.1rem;
      text-transform: uppercase;
      margin-top: 1rem;
    }
    
    button:hover{
      color: lightgray;
    }
    
    /* Container Queries */
    
    .card-container {
      container-type: inline-size;
    }
    
    @container (max-width: 850px) {
      .links {
        display: none;
      }
      
      .subtitle {
        font-size: 1.25rem;
      }
      
      h2 {
        font-size: 2.2rem;
      }
    }
    
    @container (max-width: 650px) {
      .desc {
        display: none;
      }
      
      h2 {
        font-size: 2rem;
      }
      
      .card {
        gap: 1rem;
      }
    }
    
    @container (max-width: 460px) {
      .card {
        grid-template-columns: 1fr;
        text-align: center;
        gap: 0.5rem;
      }
      
      button {
        display: block;
        margin: 1rem auto 0;
      }
    }
    
    @container (max-width: 300px) {
      h2 {
        font-size: 1.5rem;
      }
      
      .subtitle {
        display: none;
      }
    }
    
    strong {
      font-weight: 800;
    }
    
    i {
      font-style: italic;
    }

We created two similar card components that can resize and adapt to fit in any container by using the CSS Container Queries. Now, we want our container to look slightly different at different breakpoints depending on its available space while remaining responsive. So as we can see in the above CSS code, we added 4 different breakpoints for four different layouts. Let’s have a look at them:

 1. For containers more than 850px:

![](https://cdn-images-1.medium.com/max/2000/1*CEOB3_NtkxmdWVHhurMQNw.png)

2. For containers less than 850px :

![](https://cdn-images-1.medium.com/max/2000/1*4k7oJga9eOkOw8jrBOd1NQ.png)

3. For containers less than 650px:

![](https://cdn-images-1.medium.com/max/2000/1*ujQGHCtzsgipV-ijx5VftA.png)

4. For containers less than 460px:

![](https://cdn-images-1.medium.com/max/2000/1*KtLN5N6unkXF2G82vvcgzg.png)

5. For containers less than 300px:

![](https://cdn-images-1.medium.com/max/2000/1*KtLN5N6unkXF2G82vvcgzg.png)

As we’ve seen from the previous examples, CSS Container Queries are best used for highly reusable components with a layout that depends on the available container space and that can be used in various contexts and added to different containers on the page.

***This Playwright tutorial will guide you through the setup of the [playwright automation](https://www.lambdatest.com/blog/playwright-framework/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) framework, which will enable you to write end-to-end tests for your future projects.***

## How to perform responsiveness testing of CSS Container Queries?


It’s important to test your website on multiple screen sizes to ensure that it looks and behaves correctly on all of them.

You can use a [mobile-friendly tester](https://www.lambdatest.com/mobile-friendly-test?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) like [LT Browser](https://www.lambdatest.com/lt-browser?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) to test the responsiveness of your website across various pre-installed device viewports for Android, iOS, Windows, and macOS. It also offers other features like:

* Check mobile view of websites.

* Compare side-by-side view of the website.

* Generate performance reports of your websites powered by Google Lighthouse.

* Get JavaScript Error reports.

* Test websites across different network conditions.

* One-click bug logging and video recording of test sessions.

You can also try our new Chromium-based LT Browser 2.0, which has several new features and enhancements. [Try LT Browser 2.0 now](https://www.lambdatest.com/lt-browser-beta?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog).

To get started, check our support documentation — Getting started with LT Browser 2.0.

{% youtube K1dlmU3QWWk %}

Subscribe to the [LambdaTest YouTube channel](https://www.youtube.com/c/LambdaTest?sub_confirmation=1?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog) for tutorials around [Selenium testing](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog), [Playwright browser testing](https://www.lambdatest.com/blog/playwright-framework/?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog), [Appium](https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec09_pk&utm_term=pk&utm_content=blog), and more.

## It’s a Wrap!


Once the spec has been implemented and widely supported by browsers, CSS Container Queries might become a game-changing feature. CSS Container Queries are an impending approach for moving beyond media queries tied to the viewport. For many years, developers have desired the ability to change an element’s width based on its container’s width. Therefore, it is only logical that elements have a way to be responsive based on their parent’s container meta information rather than the entire viewport.

As it stands, CSS Container Queries are still in an early experimental phase, and the implementation is prone to change. We hope to see the final draft specifications for this feature soon so we can all start using it. Until then, keep experimenting !!

Thank you for reading.