日本語

Container queries

A Container applies part of a style from a width of the nearest query container up, so a component follows the space it is placed in.

A style by the width of an ancestor

A media query measures the viewport, so a card in a narrow sidebar gets the same layout as one in a wide main column. A Container measures the nearest ancestor that is a query container instead. An element becomes a query container with container_type(ContainerType::InlineSize), and Style::on(Container::Md, ..) on an element inside it applies from 28rem of that ancestor's width up:

use hypersig::html::{div, h3, p};
use hypersig::style::value::{ContainerType, Display, FlexDirection, rem};
use hypersig::style::{Container, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Shop {}

// The card lays itself out by the width of the column it is in,
// whatever the width of the viewport.
let card = div::<Shop>()
    .css(
        Style::new()
            .display(Display::Flex)
            .flex_direction(FlexDirection::Column)
            .gap(rem(0.5))
            .on(Container::Md, Style::new().flex_direction(FlexDirection::Row)),
    )
    .child(h3().text("Desk lamp"))
    .child(p().text("A lamp with a long arm."));

let column = div::<Shop>()
    // The query container the card measures.
    .css(Style::new().container_type(ContainerType::InlineSize))
    .child(card);

Writing .column and .card for the two elements, the browser applies:

.column { container-type: inline-size }
.card { display: flex; flex-direction: column; gap: 0.5rem }
@container (min-width: 28rem) { .card { flex-direction: row } }

The widths

The widths are the default container sizes of Panda CSS, so its @/md is Container::Md here:

VariantContainer queryPanda CSS
Xs3(min-width: 16rem)@/3xs
Xs2(min-width: 18rem)@/2xs
Xs(min-width: 20rem)@/xs
Sm(min-width: 24rem)@/sm
Md(min-width: 28rem)@/md
Lg(min-width: 32rem)@/lg
Xl(min-width: 36rem)@/xl
Xl2(min-width: 42rem)@/2xl
Xl3(min-width: 48rem)@/3xl
Xl4(min-width: 56rem)@/4xl
Xl5(min-width: 64rem)@/5xl
Xl6(min-width: 72rem)@/6xl
Xl7(min-width: 80rem)@/7xl
Xl8(min-width: 90rem)@/8xl

Like the breakpoints of Media, each is a min-width query, a larger width overrides a smaller one, and container conditions nest. A container condition nested in a media condition is written inside the @media block.

Only the listed widths

Container has no variant that takes a length, so a width outside the table is a compile error:

use hypersig::style::value::{FlexDirection, rem};
use hypersig::style::{Container, Style};

// error[E0599]: no variant, associated function, or constant named `MinWidth` found for enum `Container`
let card = Style::new().on(Container::MinWidth(rem(30)), Style::new().flex_direction(FlexDirection::Row));

What the types leave unchecked

  • Nothing checks that an ancestor is a query container. Without one, the condition never matches, and the element keeps the style it has without the condition.
  • A container condition measures a container above the element, never the element itself. container_type and on(Container::..) on the same element measure the next container up, or none.
  • With container-type: inline-size, the container no longer takes its width from its content. A container that would shrink to fit its content, such as a flex item without a width, can collapse to zero width.
  • The query always measures the nearest container. A container condition that names a container, as @container sidebar (..) does, has no variant.

Next

Group and peer applies a style while an ancestor or a sibling matches a pseudo-class.