ZL
About Articles Contact
Published on Jul 10, 2023
Filed under:
#container queries,
#css,
#astro

Here’s how to quickly test components that use container queries

Container queries are amazing. They let us build UI that allow a component looks good no matter what size the component is displayed at.

But this feature also makes it difficult to test container queries.

If you do it normally, you have to resize your browser many times, which can be irritating and a huge waste of time.

There are better ways.

There are two ways to make things easier

The first is to use a browser like Polypane that lets you debug multiple viewports at the same time. It costs only $9/m and it has the potential to reduce a lot of developmental headaches.

If you don’t want to spend any money, the second way is to debug your components with a utility that lets you display the component in many different sizes.

Here’s how the utility works

The basic idea is:

  1. You create a series of containers at different widths.
  2. Then you insert the component into each container.
  3. And each container will let you see what the component looks like at that width.

You’ll find the code below.

Note

I’ve written this in Astro because I currently use Astro. But this can be reworked for React, Vue, Svelte, Nunjucks, Pug, and all sorts of frameworks and template engines easily.

---
let { containers, increment, startAt } = Astro.props
// Default options
containers = containers || 10 // Show 10 containers
increment = increment || 50 // Containers should increase by 50px each time by default
startAt = startAt || 300 // The smallest container is 300px wide
---
<div class="CQTestGrid">
{
[...Array(containers)].map((_, i) => {
const width = i * increment + startAt + 'px'
return (
<div style={`width: ${width}`}>
<div class="size">{width}</div>
<slot />
</div>
)
})
}
</div>
<style>
.CQTestGrid {
display: flex;
flex-flow: row wrap;
padding: 1rem;
> * {
margin: 0.5rem;
}
}
.size {
font-size: 0.8rem;
color: #666;
}
</style>

Here’s how you would use the utility in an actual test.

---
import CQTest from '../components/CQTest.astro'
const props = {
containers: 10,
increment: 25,
startAt: 100,
}
---
<Base>
<CQTest {...props}>
{
[...Array(props.containers)].map(_ => (
<div class="“Container”">Your component here</div>
))
}
</CQTest>
</Base>

By using this, I can make sure the component looks great all the time.

Take this out for a spin! 🙃

I hope this utility helps you reduce the problems you may face as you make the best UI you can!

Previous Automatic reactivity with Vanilla JavaScript with two methods — Getters and Setters and JavaScript Proxies Next The easiest way to get __dirname in Node with ES Modules

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is the rare sort of developer who both knows his stuff and can explain even the most technical jargon in approachable — and even fun — ways!

I’ve taken his courses and always look forward to his writing because I know I’ll walk away with something that makes me a better front-ender.

Geoff Graham
Geoff Graham — Chief Editor @ CSS Tricks
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms