Day 73: size container features
posted on
It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.
In my previous posts about size container features I’ve only used the min-width
feature, but there’s actually more you can query.
container-type: inline-size
establishes size containment only on the inline axis. There is no block-size
option because it wasn’t possible for browsers to implement, but there is a size
option, which establishes size containment on both dimensions of the container. According to Miriam Suzanne, you should be careful using this option because I may cause side effects, but it allows you to query more than just the width/inline-size.
orientation
You can query the orientation of the container. If the height is larger than the width, the orientation
is portrait
. If the width is larger than the height, it's landscape
.
.container {
border: 8px solid aqua;
container-type: size;
width: 10rem;
height: 15rem;
}
.container2 {
width: 15rem;
height: 10rem;
}
.child {
aspect-ratio: 1;
width: 5rem;
border: 4px solid;
color: red;
}
@container (orientation: portrait) {
.child {
background: currentColor;
}
}
<div class="container">
<div class="child"></div>
</div>
<div class="container container2">
<div class="child"></div>
</div>
aspect-ratio
.container3 {
width: 10rem;
height: 10rem;
}
.container4 {
width: 10rem;
height: 5.625rem;
box-sizing: content-box;
}
@container (aspect-ratio: 1 / 1) {
.child {
background: blue;
}
}
@container (aspect-ratio: 16 / 9) {
.child {
background: green;
}
}
height
You can also query the height.
@container (min-height: 14rem) {
.child {
background: fuchsia;
}
}
logical properties
Instead of width
you can also use inline-size
in your queries and instead of height
you can use block-size
.
@container (min-block-size: 14rem) {
.sample4 .child {
background: aqua;
}
}
Further reading
- Day 56: container queries
- Day 59: naming containers
- Day 62: the container shorthand
- Day 65: using the em unit in container queries
- Day 69: width in container queries
- Day 78: container query units
- Day 90: scoped styles in container queries
Do you want to learn even more awesome CSS?
I'm running a workshop with my friends at Smashing Magazine in which I introduce you to the most useful modern features in CSS and show how you can implement them today in your code base to improve scalability, maintainability, and productivity.
Learn more about the workshop!
Overview: 100 Days Of More Or Less Modern CSS