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.
Change a style at a breakpoint
Section titled “Change a style at a breakpoint”.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.
Follow the color scheme
Section titled “Follow the color scheme”.screen { background-color: #ffffff;}@media (prefers-color-scheme: dark) { .screen { background-color: #0f172a; }}The same works for prefers-reduced-motion, prefers-contrast and orientation.
Size against a parent, not the window
Section titled “Size against a parent, not the window”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.
Theme with a variable
Section titled “Theme with a variable”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.
Type a variable with @property
Section titled “Type a variable with @property”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.
Branch in code on a query
Section titled “Branch in code on a query”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;}import { onScopeDispose, ref } from 'vue';import { matchMedia } from '@symbiote-native/vue';
export function useMatchMedia(query: string) { const list = matchMedia(query); const matches = ref(list.matches); const controller = new AbortController(); list.addEventListener('change', event => (matches.value = event.matches), { signal: controller.signal }); onScopeDispose(() => controller.abort()); return matches;}import { createSignal, onCleanup } from 'solid-js';import { matchMedia } from '@symbiote-native/solid';
export function createMatchMedia(query: string) { const list = matchMedia(query); const [matches, setMatches] = createSignal(list.matches); const controller = new AbortController(); list.addEventListener('change', event => setMatches(event.matches), { signal: controller.signal }); onCleanup(() => controller.abort()); return matches;}<script lang="ts"> import { matchMedia } from '@symbiote-native/svelte';
const list = matchMedia('(min-width: 600px)'); let wide = $state(list.matches);
$effect(() => { const controller = new AbortController(); list.addEventListener('change', event => (wide = event.matches), { signal: controller.signal }); return () => controller.abort(); });</script>Svelte also ships orientation and createWidthQuery for the common cases.
import { DestroyRef, inject, signal } from '@angular/core';import { matchMedia } from '@symbiote-native/angular';
export function injectMatchMedia(query: string) { const list = matchMedia(query); const matches = signal(list.matches); const controller = new AbortController(); list.addEventListener('change', event => matches.set(event.matches), { signal: controller.signal }); inject(DestroyRef).onDestroy(() => controller.abort()); return matches.asReadonly();}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.