I would want to use the new CSS container queries in modern Safari/Chrome browser. (Safari 16.3, Google Chrome 113.0)
However container queries based on height are not working as expected.
Expected result: as soon as the outer container turns blue (500px screen height or below) I would expect the pink square (50vh of 500px container) to turn red.
Current result: The square stays pink and does not turn pink. The example works if the implementation is width relative.
Did I do anything wrong in my implementation or is it just not jet implemented in Webkit engine? Any other solutions (without Javascript) to solve the problem, if in the final product the container will be resizeable by the user?
body {
margin: 0
}
.container {
height: 50vh;
container-type: inline-size;
}
.test {
width: 250px;
height: 250px;
background-color: hotpink;
}
@container (max-height: 250px) {
.test {
background-color: red;
}
}
@media screen and (max-height: 500px) {
.container {
background: blue;
}
}
<div class="container">
<div class="test"></div>
</div>
inline-size
is the width not the height. You have to use size
inline-size
the query will be based on the inline dimensions of the container. Applies layout, style, and inline-size containment to the element. ref
body {
margin: 0
}
.container {
height: 50vh;
container-type: size;
}
.test {
width: 250px;
height: 250px;
background-color: hotpink;
}
@container (max-height: 250px) {
.test {
background-color: red;
}
}
@media screen and (max-height: 500px) {
.container {
background: blue;
}
}
<div class="container">
<div class="test"></div>
</div>