slides.oddbird.net/css-next/camosun/

Container Queries & The Future of CSS

@ Camosun College

It is required that HTML be a common language between all platforms

WWW Project

…This implies no device-specific markup, or anything which requires control over fonts or colors.

WWW Project

The fact we can control a paper page is really a limitation of that medium.

– John Allsopp, 2000

@media prefers-reduced-motion

@supports container-type: inline-size

Animation of twitter-like posts appearing

We’re designing unknown content with unknown collaborators on an infinite and unknowable canvas, across operating systems, interfaces, writing-modes, & languages

– me

The user/browser specifies initial preferences and hands the remaining influence over to the document [authors].

– Håkon Lie

Provide hints that the browser may or may not use.

– Håkon Lie

<button style='color: blue'></button>
<button style='color: blue'></button>
<button style='color: blue'></button>
<button style='color: blue'></button>
<button style='color: blue'></button>
<button></button>
<button></button>
<button></button>
button { color: violet; }
<link rel="stylesheet" href="so-many-hints.css">
<style>...</style>

An ordered list (cascade) of style sheets. Referenced from the same document.

– Håkon Lie

💥🙈⁉️💥

<button style='color: blue'></button>
button { color: violet; }

Multiple or Unknown ⁉️

color: teal;
color: violet;
color: oklch(79.269% 0.171 70.67);

On every html element
For every css property
We need exactly one value

Rules for… Cascading filters out
Inheritance fills in

Mechanical coin sorter on a table,
with a ramp at the top
and a small-to-large series of slots

🖥 User Agent Styles

aka ‘browser defaults

body { margin: 8px; }
a:link { color: blue; }
div, section, article, p, ol, ul /* etc */ {
display: block;
}
h1, h2, h3, h4 /* etc */ {
font-weight: bold;
font-size: something big I dunno;
}

👥 User Styles

Establish desired preferences

Cascade 🗺 Origins

  1. 🖥 User Agent Defaults (applied first)
  2. 👥 User Preferences (override defaults)
  3. 🎨 Author Styles (so powerful)

If conflicts arise the user should have the last word

– Håkon Lie

  1. ❗🎨 Author Important (goes first)
  2. ❗👥 User Important (override authors)
  3. ❗🖥 User Agent Important (most power)
  1. 🖥 User Agent Defaults
  2. 👥 User Preferences
  3. 🎨 Author Styles
  4. ❗🎨 Author Important
  5. ❗👥 User Important
  6. ❗🖥 User Agent Important

Authors 👎🏼 Override Styles

We probably wrote ourselves

Users 👍🏼 Protect Styles

From future origins

Often Isolated

  1. 🎨 Author Styles
  2. ❗🎨 Author Important
  3. ❗…
  4. ❗…

How Specifically

Is a selector targeting elements

More Specific Styles
Likely
More Important

Heuristic

a practical assumption that approximates the goal

@layer settings {}
@layer tools {}
@layer generic {}
@layer elements {}
@layer objects {}
@layer components {}
@layer overrides {}
  1. @layer settings { … }
  2. @layer tools { … }
  3. @layer generic { … }
  4. @layer elements { … }
  5. @layer objects { … }
  6. @layer components { … }
  7. @layer overrides { … }
  1. @layer Utilities { … }
  2. @layer Components { … }
  3. @layer Themes { … }
  4. @layer Frameworks { … }
  5. @layer Resets { … }
@layer reset {
audio[controls] { display: block; }
[hidden] { display: none !important; }
}
@layer framework {
#menu .dropdown .item:hover {
background: whitesmoke;
}

.menu-item {
background: lightcyan;
}
}
@layer framework {
#menu .dropdown .item:hover {
background: whitesmoke;
}
}

@layer override {
.menu-item {
background: lightcyan;
}
}

Like Origins, ❗️mportant Layers Reverse

  1. Resets
  2. Themes
  3. Components
  4. ❗mportant Components
  5. ❗mportant Themes
  6. ❗mportant Resets

Prioritize -> Layers
Protect -> Importance

For flexibility Layer Everything

Once we use un-layered styles, that’s as high as we can go

1. Avoid Naming Conflicts

(across large teams & projects)

.post__title { /* BEM */ }
.title[data-JKGHJ] { /* Vue */ }

2. By Expressing Membership

(through lower boundaries & proximity)

.title { /* global */ }
.post .title { /* nested */ }

.post__title { /* BEM */ }
wireframe of a site, with multiple nested components
.light-theme a { color: purple; }
.dark-theme a { color: plum; }
@scope (.media) to (.content) {
img { /* only images that are "in scope" */ }
}

prototype… Now in Chromium

2008/2009 @media Queries

Ethan Marcotte… Responsive Web Design™️

  • Fluid (%-based) Grids
  • Flexible (%-based) Images
  • Media Queries

Container Queries?

please? if we promise to be good?

Flexbox & Grid

  • Box Alignment
  • Viewport Units
  • Intrinsic Sizing
  • Aspect Ratios
  • Min/Max/Clamp
  • &c.
  1. Truly Two-Dimensional Layouts
  2. Combine Fluid & Fixed
  3. Stages of Squishiness
  4. Nested Contexts
  5. Expand & Contract Content
  6. Media Queries, As Needed

Intrinsic Web Design

The responsive web keeps evolving…

  1. Nested Contexts ???

  2. Expand & Contract Content ???

CSS Containment

Size, Layout, Style, Paint, &c

Size containment Removes Intrinsic Sizing

contain: inline-size;

Too complicated

.container {
contain: inline-size layout style;
}

More declarative

.container {
container-type: inline-size;
}

Some Caveats

  • Inline size not intrinsic
  • Subgrids can’t contribute track sizing
  • Counters contained
.sidebar {
container-name: sidebar layout;
}

Shorthand (Names First)

.sidebar {
container: sidebar layout / inline-size;
}

There’s no default container

html {
// <name> / <type>
container: root layout / inline-size;
}
@media (min-width: 40em) {
.card { /* ... */ }
h2 { /* ... */ }
}
@container (min-width: 40em) {
.card { /* ... */ }
h2 { /* ... */ }
}

Each matched element Queries Nearest Ancestor

with the appropriate container-type

Relative units Resolved By Container

Computed font size

Query Named Containers

@container layout (min-width: 30em) {
.card { padding: 2em; }
}

Finding Containers

  1. For each matched element
  2. Find the nearest ancestor that has…
    • Any required container name
    • Any required container types

Grid Tracks & Flex Sizing?

No element to measure…

Container Query Units

cqw | cqh | cqi | cqb | cqmin | cqmax

Style Queries

@container style(--colors: invert) {}
blockquote, em { font-style: italic; }

@container style(font-style: italic) {
em { background-color: pink; }
}

Chromium Prototype

Currently only custom properties

Our medium is not done. Our medium is still going through radical changes.

– Jen Simmons, Designing with Grid