Skip to content

How to: make styles responsive

You want a class to look different on a narrow window, in a dark scheme, or inside a small box. The CSS is the same on every adapter. Read the guide for the full model.

.card {
padding: 12px;
}
@media (min-width: 37.5rem) {
.card {
padding: 24px;
}
}

Use rem or px. The rule restyles only when the window crosses 600 wide, not on every resize.

.screen {
background-color: #ffffff;
}
@media (prefers-color-scheme: dark) {
.screen {
background-color: #0f172a;
}
}

The same works for prefers-reduced-motion, prefers-contrast and orientation.

Give the parent a definite size and a container-type. Descendants then query it:

.panel {
container-type: inline-size;
width: 100%;
}
@container (min-width: 15rem) {
.panel-title {
font-size: 20px;
}
}

A container with no width, flex or aspectRatio on that axis fails the query, and the engine logs one console.warn for that container.

Declare the token once and read it with var():

:root {
--accent: #8338ec;
}
.badge {
background-color: var(--accent);
}

Re-declare it under a media query to switch the whole theme with the color scheme:

@media (prefers-color-scheme: dark) {
:root {
--accent: #c4a1ff;
}
}

Every var(--accent) follows the scheme with no class on any node. Later rules win, as in the cascade.

Override it for the whole surface from code, and remove it to fall back to :root:

setVariable(surface, '--accent', '#ffbe0b');
setVariable(surface, '--accent', undefined);

surface is the root tag of the app, read through each adapter’s own accessor, listed in the guide. A --x declared in a class on a node wins over the surface value for that node and its descendants.

A typed property computes to a canonical value and can stop inheritance:

@property --gap {
syntax: '<length>';
inherits: false;
initial-value: 8px;
}

registerProperty({ name: '--gap', syntax: '<length>', inherits: false, initialValue: '8px' }) does the same from code.

matchMedia returns a snapshot plus a change event, with no reactivity of its own. Wrap it once in your framework’s state. Pass signal to addEventListener so the listener goes away with the component:

import { useEffect, useState } from 'react';
import { matchMedia } from '@symbiote-native/react';
function useMatchMedia(query: string) {
const [matches, setMatches] = useState(() => matchMedia(query).matches);
useEffect(() => {
const list = matchMedia(query);
const controller = new AbortController();
setMatches(list.matches);
list.addEventListener('change', event => setMatches(event.matches), { signal: controller.signal });
return () => controller.abort();
}, [query]);
return matches;
}

matchMedia is exported by every adapter. The color scheme has a framework-native form too: useColorScheme on React, Vue and Svelte, createColorScheme on Solid, ColorSchemeService on Angular.