this response will provide you with some code-based solutions,
Be sure to refer to @MiriamSuzanne's answer for a detailed explanation of why this solution may not be functioning correctly.
final outcome (gif)
https://i.sstatic.net/RzzNR.gif
how to resolve
to make this idea operational:
start by creating a <div>
as the main container.
subsequently, enclose the desired content within, such as a button or any other element.
apply width: 100%;
to all child elements so they inherit the container's width.
now enjoy using @container
like so:
basic example
.container {
container-type: inline-size;
}
.container>* {
width: 100%;
}
.container button {
background-color: lightgreen;
}
@container (width > 500px) {
.container button {
background-color: red;
}
}
<div class="container">
<button>this is a button</button>
</div>
https://i.sstatic.net/Yx5br.gif
real life scenario
demonstrating that @container
behaves differently from @media
.container {
container-type: inline-size;
}
.container,
.container>* {
width: 100%;
}
.container button {
background-color: lightgreen;
}
@container (width > 400px) {
.container button {
background-color: red;
}
}
<!-- 1 item -->
<div class="container">
<button>this is a button</button>
</div>
<!-- 2 items -->
<div style="display: flex">
<div class="container">
<button>this is a button</button>
</div>
<div class="container">
<button>this is a button</button>
</div>
</div>
<!-- 4 items -->
<div style="display: flex">
<div class="container">
<button>this is a button</button>
</div>
<div class="container">
<button>this is a button</button>
</div>
<div class="container">
<button>this is a button</button>
</div>
<div class="container">
<button>this is a button</button>
</div>
</div>
https://i.sstatic.net/RzzNR.gif