#containerQuery

Nils Hörrmannnilshoerrmann
2025-05-15

In Safari, does anyone know why the existence of a container definition (`container-name`/`container-type`) in a layout results in media queries not getting executed at all or being delayed by 5 to 10 seconds? Like you resize the window and nothing happens, you can still interact with the site and then suddenly the layout flips?! Experienced this for the first time and am a bit stumped.

Ana Tudor 🐯anatudor
2025-05-08

Haven't been posting much coding stuff this past week as I've found it difficult to focus on it given the state of things in my country 😞

But... I have a new article out!

Using Container Query Units Relative to an Outer Container frontendmasters.com/blog/using

Sparked by a question @mattwilcox asked here.

Ana Tudor 🐯anatudor
2024-12-19

Pretty sure I've posted about this before, but a quick search at the morning hours didn't find it, plus I saw someone ask about it yet again¹, so... fixed aspect-ratio box within variable size container!

Live on @codepen codepen.io/thebabydino/pen/ogv

¹here reddit.com/r/css/comments/1hhf

.parent {
  display: grid; /* for child middle alignment */
  container-type: size; /* for child sizing */
  /* random ass parent sizing */
  width: min(60em, 90%);
  height: min(800px, 50vh);
  background: dodgerblue /* make its boundary visible */
}

.aspect-ratio-child {
  --r: 8/ 5;
  place-self: center; /* put it in the middle along both axes */
  /* the magic, using container query units */
  width: min(100cqw, var(--r)*100cqh);
  height: min(100cqh, 100cqw/(var(--r)));
  /* or use aspect-ratio: var(--r) instead of height, but... pixel rounding issues */
  background: crimson /* make its boundary visible */
}
Ana Tudor 🐯anatudor
2024-11-04

Container queries on the body? Why, when media queries have better support?

Well, container queries make it an IF depending on the width of the body's content-box.

That is, subtracting the scrollbar IF we have one (we can't know).

**Heavily** commented @codepen demo codepen.io/thebabydino/pen/ZEg

/* consider this structure inside the body:
section.item
	.text-box
	img
... and so on, multiple such sections with 2 children */

/* allows us to use a container query - better than media query
 * because it takes into account the scrollbar IF there's one */
body { container-type: inline-size }

.item, .text-box { display: grid }

.item {
	/* 1 column for each of the two items, as long as there's room
	 * for each of them to have at least 150px */
	grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr));
	grid-auto-flow: dense /* prevent unoccupied cells in grid */
}

/* 1st, 3rd & so on section have text box on 2nd column 
 * IF there's enough room to have 2 columns of at least 150px each */
@container (min-width: calc(2*150px)) {
	:nth-of-type(odd) > .text-box { grid-column: 2 }
}
Inautiloinautilo
2024-09-03
Inautiloinautilo
2024-08-15


‘Smart’ CSS layouts with container queries · New options we never had with media queries ilo.im/15zshu

_____

Inautiloinautilo
2024-08-03


What if you used Container Units for… everything? · “Surprise, surprise, that it’s not that easy.” ilo.im/15zotb

_____

Inautiloinautilo
2024-07-25


Fit-to-width text: A new technique · How modern CSS helps to solve an ancient challenge ilo.im/15zk30

_____

Inautiloinautilo
2024-07-05


How to use CSS Container Queries now · Can Container Queries already be used in production? ilo.im/15zdwd

_____

Ana Tudor 🐯anatudor
2024-06-24

2⃣ feColorMatrix: swap channels ☆ interactive demo, adaptive layout codepen.io/thebabydino/full/QW

An interactive, responsive demo illustrating how feColorMatrix can be used to swap channels.

Inautiloinautilo
2024-06-14


Learn CSS grid now, container queries can wait · “Take your time with new CSS, but don’t sleep on the essentials.” ilo.im/15z72q

_____

Ana Tudor 🐯anatudor
2024-06-13

If you want a fixed aspect-ratio element that always touches either the horizontal or vertical edges of its flexible size and aspect ratio wrapper *which does not coincide with the viewport*, you can make the wrapper a container and use container query units.

Live test on @codepen codepen.io/thebabydino/pen/Exz

The CSS code used to achieve this:

```
html, body, div { display: grid }

html { min-height: 100% }

body {
	grid-gap: 1em;
	grid-template-columns: 
		repeat(auto-fit, 
		       minmax(min(100%, 16em), 1fr));
	font-size: clamp(.625em, 1vw + 1vh, 1.5em)
}

.wrap {
	container-type: size;
	box-shadow: 0 0 0 1px, inset 0 0 0 1px
}

.box {
	--r: 16/9;
	place-self: center;
	width: min(100cqw, var(--r)*100cqh);
	aspect-ratio: var(--r);
	background: crimson;
}
```
Inautiloinautilo
2024-06-11


CSS Container Queries · A contemporary approach to responsive web design ilo.im/15z5q7

_____

Ana Tudor 🐯anatudor
2024-05-11

Extremely annoying gotcha: container query units seem to be relative to the `content-box` dimensions. And there doesn't seem to be any way to make them relative to the `border-box` dimensions... which is what I need to solve other problems caused by container queries. 😭

Ana Tudor 🐯anatudor
2024-05-02

Let's say we want a 90° rotated element (like a range input because rotation is the only way we can make it vertical) to fully cover its flexible size (think `clamp()` using `%` values, grid item on a flexible grid and stuff like that) parent.

We can make this parent a container and use container query units to style our element.

.parent { /* flexible dimensions element */
	container-type: size
}

[type='range'] {
	/* make its width equal to the flexible height of its parent */  
	width: 100cqh;
	/* make its height equal to the flexible width of its parent */  
	height: 100cqw;
	/* get a vertical slider by rotating it */
	rotate: -90deg
}
Ana Tudor 🐯anatudor
2024-04-22

In the final case, when we use the B channel, the only orange parts that aren't almost fully transparent are the white pith and seeds.

Screenshot of the case when we use the blue channel as an alpha mask.
Ana Tudor 🐯anatudor
2024-04-19

When we use the G channel, the white orange pith and seeds, as well as the flesh of the less reddish oranges are the least transparent parts. The aquatic blue background is also less transparent in this case, through still more transparent than when we use the B channel.

Screenshot of the case when we use the green channel as an alpha mask.
Ana Tudor 🐯anatudor
2024-04-18

When we use the R channel, the oranges are the least transparent (darkest) part (R value higher there as orange is mainly R + some G), especially the whitish pith (which almost maxes out all channels), while the aquatic blue (mostly B + G) background is the most transparent.

Screenshot of the case when we use the red channel as an alpha mask.

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst