<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap"
rel="stylesheet"
/>
</head>
<body>
<div>
<div id="resizable" class="resizable">
<div class="example">
<p class="text text--2xs">My container is less than 101px wide</p>
<p class="text text--xs">My container is betwween 101px and 360px</p>
<p class="text text--sm">My container is between 361px and 499px</p>
<p class="text text--md">My container is between 500px and 699px</p>
<p class="text text--lg">My container is between 700px and 899px</p>
<p class="text text--xl">My container is between 900px and 1099px</p>
<p class="text text--2xl">
My container is wider than 1099px
</p>
</div>
</div>
<p>
Current width:
<span id="current_width"></span>
</p>
</div>
</body>
</html>
p {
font-famile: Lato;
}
.resizable {
resize: horizontal;
overflow: scroll;
width: 350px;
height: 100px;
font-family: Lato;
container: pagination / inline-size;
--button-width: 36px;
}
/** Base style */
.resizable {
resize: horizontal;
overflow: scroll;
width: 350px;
font-family: Lato;
container: example / inline-size;
--button-width: 36px;
}
/** Base style */
.text {
width: 100%;
height: 100%;
color: black;
display: none;
&--2xs {
color: #39FEA3;
}
&--xs {
color: #f0f;
}
&--sm {
color: #009018;
}
&--md {
color: #f00;
}
&--lg {
color: #00f;
}
&--xl {
color: #ffc800;
}
&--2xl {
color: #00ffbb;
}
}
/* Container queries */
@container example (max-width: 100px) {
.text {
display: none;
&--2xs {
display: inline;
}
}
}
@container example (min-width: 101px) {
.text {
display: none;
&--xs {
display: inline;
}
}
}
@container example (min-width: 361px) and (max-width: 499px) {
.text {
display: none;
&--sm {
display: inline;
}
}
}
@container example (min-width: 500px) and (max-width: 699px) {
.text {
display: none;
&--md {
display: inline;
}
}
}
@container example (min-width: 700px) and (max-width: 899px) {
.text {
display: none;
&--lg {
display: inline;
}
}
}
@container example (min-width: 900px) and (max-width: 1099px) {
.text {
display: none;
&--xl {
display: inline;
}
}
}
@container example (min-width: 1100px) {
.text {
display: none;
&--2xl {
display: inline;
}
}
}
View Compiled
const $target = document.querySelector('#resizable');
const $currentWidth = document.querySelector('#current_width');
window.addEventListener('mousemove', (e) => {
const width = $target.getBoundingClientRect().width;
$currentWidth.innerHTML = `${width}px`;
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.