With CSS container queries now being supported by all major browsers, I wanted to integrate this new feature into my project. However, I quickly came across a problem and sadly information on this topic is still very hard to come by.
The way these are supposed to work, is that you can style descendants of an element based on the size of a container. However, I want to achieve the opposite effect: style a parent or "outside" element based on the size of a contained element.
Consider the following example, in which the style gets applied to the child, but not to the parent:
.parent{
background-color: blue;
width: 500px;
height: 500px;
}
.container{
container-name: test;
container-type: size;
background-color: grey;
width: 300px;
height: 300px;
}
.child{
background-color: red;
width: 100px;
height: 100px;
}
@container test (min-height: 50px) {
.child {
background-color: green;
}
.parent{
background-color: green;
}
}
<div class="parent">
<div class="container">
<div class="child"></div>
</div>
</div>
From what I understand, this behavior is expected from the spec. However, if anyone knows how this could possibly work it would be greatly appreciated. Maybe there is a trick to it that I have not thought of or considered.