日本語

Media queries

A Media applies part of a style from a viewport width up, or under a user setting such as a dark color scheme or reduced motion.

Breakpoints

A Media converts into a Condition, so Style::on takes it as it takes a pseudo-class. The breakpoints are min-width queries: a style for one applies from that width up, and a larger breakpoint overrides a smaller one. Write the narrowest layout without a condition, then add what changes at each width:

use hypersig::html::{article, nav};
use hypersig::style::value::{Color, Display, pct};
use hypersig::style::{Media, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

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

let column = Style::new()
    // Below 48rem.
    .width(pct(100))
    // From 48rem, and from 64rem.
    .on(Media::Md, Style::new().width(pct(50)))
    .on(Media::Lg, Style::new().width(pct(33)))
    // From 48rem while the system prefers dark.
    .on(Media::Md, Style::new().on(Media::Dark, Style::new().color(Color::WHITE)));

let sidebar = nav::<Docs>()
    .css(Style::new().on(Media::Print, Style::new().display(Display::None)))
    .text("Contents");

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

.column { width: 100% }
@media (min-width: 48rem) { .column { width: 50% } }
@media (min-width: 64rem) { .column { width: 33% } }
@media (min-width: 48rem) and (prefers-color-scheme: dark) { .column { color: white } }
@media print { .sidebar { display: none } }

The widths are the default breakpoints of Tailwind CSS and Panda CSS, so a layout ported from either keeps its widths:

VariantMedia queryPixels at a 16px root font size
Sm(min-width: 40rem)640
Md(min-width: 48rem)768
Lg(min-width: 64rem)1024
Xl(min-width: 80rem)1280
Xxl(min-width: 96rem)1536

User settings and devices

The other variants test a setting of the user or a property of the device:

VariantMedia queryMatches when
Dark(prefers-color-scheme: dark)The system prefers a dark color scheme
Light(prefers-color-scheme: light)The system prefers a light color scheme
ReducedMotion(prefers-reduced-motion: reduce)The user asks for reduced motion
MoreContrast(prefers-contrast: more)The user asks for more contrast
ForcedColors(forced-colors: active)The browser forces a limited palette, as Windows high contrast does
PrintprintThe page is printed
Landscape(orientation: landscape)The viewport is wider than it is tall
Portrait(orientation: portrait)The viewport is taller than it is wide
CanHover(hover: hover)The primary pointer can hover, as a mouse can and a finger cannot
CoarsePointer(pointer: coarse)The primary pointer is coarse, such as a finger

Media conditions nest as pseudo-classes do, and nested ones are joined with and in one query, as Md and Dark are above. print is written first in the query, because the browser reads a media type after a feature as a query that never matches. Dark mode and Reduced motion cover Dark and ReducedMotion in detail.

A media query and a pseudo-class

A rule under a media query comes after every rule without one, and the later rule wins where both match. If a link sets color under Hover and again under Md, the Md value applies from 48rem up even while the link is hovered. To keep the hover color at that width, set it inside the Md style too:

use hypersig::style::value::Color;
use hypersig::style::{Media, PseudoClass, Sheet, Style};

let link = Style::new()
    .color(Color::GRAY)
    .on(PseudoClass::Hover, Style::new().color(Color::BLUE))
    .on(
        Media::Md,
        Style::new()
            .color(Color::BLACK)
            // Without this, the link stays black while hovered from 48rem up.
            .on(PseudoClass::Hover, Style::new().color(Color::BLUE)),
    );

A media query that does not exist

Media holds only the variants above, so a breakpoint name from another framework, or a query that does not exist, is a compile error:

use hypersig::style::value::pct;
use hypersig::style::{Media, Style};

// error[E0599]: no variant, associated function, or constant named `Tablet` found for enum `Media`
let column = Style::new().on(Media::Tablet, Style::new().width(pct(50)));

What the types leave unchecked

  • There is no max-width breakpoint and no width of your own. A style for narrow screens only is the style without a condition, overridden from the breakpoint up.
  • Nothing checks that two conditions can hold at once. on(Landscape, Style::new().on(Portrait, ..)) compiles, and its declarations never apply.
  • The breakpoints are in rem, so they move with the root font size. A visitor who sets a larger default font size gets the narrower layout at a wider window.

Next

Container queries applies a style by the width of an ancestor instead of the viewport.