everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

A Complete Guide for CSS @container

The @container CSS query provides a solution for the children’s elements to control their’s CSS style response toward the dedicated reference’s container. This feature allows for greater flexibility and modularity in CSS, making it an essential tool for modern web development.

Traditionally, we use CSS @media query to control the responsiveness of the element based on the viewport size (browser’s size).

for example:

@media screen and (max-width: 600px) {
  .example {
     /*when viewport size <= 600px; use this css*/
  }
}
Introducing CSS @container Query

With the help of the new CSS @container query, the CSS @container provides a solution for the children’s elements to control their’s CSS style response toward the dedicated reference’s container.

AttributeDescription
container-typesize, inline-size, normal
container-namedefine a container name to use in @container
containerA shorthand CSS property to set container-type & container-name.

To use CSS @container,
/*establish @container*/
.sample_container {
    container-type: inline-size;
    container-name: containerName;
}

/*Define @container query: 
when .sample_container width is <=600px; 
use below CSS*/
@container containerName (max-width: 600px) {
    .inner_containerA,
    .inner_containerB{
        background: yellow;
    }
}
  • First, you need to establish a reference container by adding container-type to the targeted parent element. (line 3)
  • You can also add in a container-name to distinguish just the targeted container (line 4)
  • Lastly, you can now define the style rules for the targeted @container (line 10)


Example:
<!DOCTYPE html>
<html>
<head>
   <style>
    .sample_main{
        display: flex;
    }
    .sample_sidebar{
        width: 20%;
        height: 200px;
        background: orange;
    }
    .sample_container {
        width: 80%;
        /*establish @container*/
        container-type: inline-size;
        container-name: containerName;
    }
    .inner_containerA,
    .inner_containerB{
        background: green;
        width: 100%;
        height: 100px;
        border: 2px solid black;
        box-sizing: border-box;
    }

    /*when container size for ".sample_container" 
    is <=600px; use below CSS*/
    @container containerName (max-width: 600px) {
        .inner_containerA,
        .inner_containerB{
            background: yellow;
        }
    }
   </style>
</head>
<body>
    <div class="sample_main">
        <div class="sample_sidebar"></div>
        <div class="sample_container">
            <div class="inner_containerA">
                Inner Container A
            </div>
            <div class="inner_containerB">
                Inner Container B
            </div>
        </div>
    </div>
</body>
</html>

Result:

In this example:

  • Originally, .inner_containerA and .inner_containerB are set to green background.
  • By using CSS media query @container containerName (max-width: 600px), when container size for “.sample_container” is <=600px, set .inner_containerA and .inner_containerB background color to yellow
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

$5.00
$10.00
$15.00

Or enter a custom amount

$

Thank you for your contribution!

Donate

Topics:

CSS (27) css pseudo (3) debug (2) design (4) fonts (2) html (6) image (8) layout (5) optimization (10) performance (9) responsive (8) Software-development (1)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements
%d bloggers like this: