Day 59: naming container

When you add a container query, it will look for the nearest ancestor container, by default. If you have multiple nested containers or if you just want to make sure that your query uses the right container, you can name containers and query them specifically.

Let’s say, we have 2 size containers, .wrapper and <section>.

<div class="wrapper">
<section>
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>
</div>
/* That's our outer size container. */
.wrapper {
container-type: inline-size;
}

/* That's our inner size container. */
section {
width: 50%;
container-type: inline-size;
}

/* Default styles for the card. */
.card {
background-color: yellow;
border: 5px solid;
}

/* Container query */
@container (min-width: 500px) {
.card {
background-color: hotpink;
}
}

By default, the container query watches the width of the closest size container, <section>. You grab and resize the <section> by clicking and dragging it in the bottom right corner. The background color of the .card changes as soon as the width of the parent section hits 500px.

Latest news

Hey, I’m a card!

By naming the container and using that name in the query, you can target a specific container.

/* That's our outer size container. */
.wrapper {
container-type: inline-size;
container-name: wrapper;
}

/* That's our inner size container. */
section {
container-type: inline-size;
}

/* The query watches the width of the outer size container (.wrapper) */
@container wrapper (min-width: 500px) {
.card {
background-color: hotpink;
}
}

You grab and resize the .wrapper by clicking and dragging it in the bottom right corner. The background color of the .card changes as soon as the width of the .wrapper is lower than 500px.

Latest news

Hey, I’m a card!


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović

When you add a container query, it will look for the nearest ancestor container, by default. If you have multiple nested containers or if you just want to make sure that your query uses the right container, you can name containers and query them specifically.

Let's say, we have 2 size containers, .wrapper and <section>.

<div class="wrapper">
<section>
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>
</div>
/* That's our outer size container. */
.wrapper {
container-type: inline-size;
}

/* That's our inner size container. */
section {
width: 50%;
container-type: inline-size;
}

/* Default styles for the card. */
.card {
background-color: yellow;
border: 5px solid;
}

/* Container query */
@container (min-width: 500px) {
.card {
background-color: hotpink;
}
}

By default, the container query watches the width of the closest size container, <section>. You grab and resize the <section> by clicking and dragging it in the bottom right corner. The background color of the .card changes as soon as the width of the parent section hits 500px.

Latest news

Hey, I'm a card!

By naming the container and using that name in the query, you can target a specific container.

/* That's our outer size container. */
.wrapper {
container-type: inline-size;
container-name: wrapper;
}

/* That's our inner size container. */
section {
container-type: inline-size;
}

/* The query watches the width of the outer size container (.wrapper) */
@container wrapper (min-width: 500px) {
.card {
background-color: hotpink;
}
}

You grab and resize the .wrapper by clicking and dragging it in the bottom right corner. The background color of the .card changes as soon as the width of the .wrapper is lower than 500px.

Latest news

Hey, I'm a card!


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-12-15T09:38:54+00:00) Day 59: naming container. Retrieved from https://www.scien.cx/2022/12/15/day-59-naming-container/

MLA
" » Day 59: naming container." Manuel Matuzović | Sciencx - Thursday December 15, 2022, https://www.scien.cx/2022/12/15/day-59-naming-container/
HARVARD
Manuel Matuzović | Sciencx Thursday December 15, 2022 » Day 59: naming container., viewed ,<https://www.scien.cx/2022/12/15/day-59-naming-container/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 59: naming container. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/15/day-59-naming-container/
CHICAGO
" » Day 59: naming container." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/12/15/day-59-naming-container/
IEEE
" » Day 59: naming container." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/12/15/day-59-naming-container/. [Accessed: ]
rf:citation
» Day 59: naming container | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/12/15/day-59-naming-container/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.