Theming
Havelcss ships greys only, even the accent is a grey that sets itself off from the text. Everything else you decide, and most themes are a dozen lines.
Colors come in pairs
Every color exists twice, once per scheme, and
light-dark() picks between them. Set the pair and both
schemes follow:
:root {
--lightmode-color-brand: oklch(0.62 0.11 220);
--darkmode-color-brand: oklch(0.72 0.13 220);
}
Setting only one side is fine, the other keeps its default. These two are inputs and nothing else. In your own rules always read the effective token instead, so the value follows whichever scheme is actually active:
.badge {
background: var(--color-bg-elevated);
color: var(--color-text-muted);
}
The color tokens
Ten roles, each with a --lightmode- and a
--darkmode- input behind it. The swatches show the
scheme you are in right now.
| Token | What it is |
|---|---|
--color-bg |
The page |
--color-bg-surface |
One step off the page: inline code, progress tracks, readonly fields |
--color-bg-elevated |
Sitting above the page: details, popover, dialog, cards, code blocks |
--color-text |
Body text |
--color-text-strong |
Emphasis, and what hover and focus move towards |
--color-text-muted |
Captions, placeholders, scrollbars |
--color-border |
Rules and outlines of static elements |
--color-input |
The surface of input, select and textarea |
--color-button |
The surface of button |
--color-brand |
Your accent: link underline, accent-color, primary button |
The four status colors are the same in both schemes and have no
pair behind them by default:
--color-success, --color-warning,
--color-danger and --color-info. They are
pitched for fills and borders. For text, read their
-text variant, --color-danger-text and so on,
which turns darker in the light scheme to stay readable.
The classes .success, .warning,
.danger and .info hand their element these two
as --status-color and --status-text, and a
notice or a button reads that pair, not the class. A status of your own
is one rule, and a component of your own can read the same pair:
.neutral {
--status-color: oklch(0.6 0.1 300);
--status-text: light-dark(oklch(0.4 0.1 300), oklch(0.8 0.1 300));
}
.badge {
background: var(--status-color, var(--color-bg-elevated));
}
Neither of the two inherits, so a status stays on the element that
carries the class. A button with a status of your own takes one more
line, --color-brand: var(--status-color).
Variants from the brand color
--color-brand can be set on any element, not only on
:root, and whatever is derived from it follows. A
section with an accent of its own is one line:
.promo {
--color-brand: oklch(0.7 0.15 300);
}
The label of a primary button inside still picks black or white for the
new background, and the hover shade, link underlines and
accent-color come along. The status classes on a button,
.success, .warning, .danger and
.info, are this very line with a status color in it.
Borders on controls
Every control already has a border of --border-width,
transparent until you color it. The surface reaches under it and the
padding gives up its width, so showing it moves nothing:
:root {
--color-input: var(--color-bg);
--color-input-border: var(--color-border);
--color-button-border: oklch(from var(--color-text) l c h / 15%);
}
--color-input-border frames fields, checkboxes, radios and
the list of a select, --color-button-border buttons and
the button of a file input. Against the default gray surfaces a light
border hardly shows, so the fields move onto the page color here.
Primary and compact buttons keep their border transparent, outline
buttons color their own.
A border-color on a single control works the same way, and
so does --border-width: an outline button with a heavier
frame stays as tall as the rest.
A whole theme
This is a realistic one, in full:
:root {
--lightmode-color-brand: oklch(0.62 0.11 220);
--darkmode-color-brand: oklch(0.72 0.13 220);
--font-family: 'Inter', system-ui, sans-serif;
--border-radius: 8px;
--space-xs: 0.125rem;
}
--space-xs is the base of the spacing scale, each step is
twice the one before, so moving it moves everything. Your theme does not
even have to be loaded after Havelcss: an unlayered rule beats every
layered one, whatever the order.
Download theme.example.css.
It lists every token with its default, all of it commented out. Copy the
lines you want to change and delete the rest.
Typed tokens
Wherever the syntax allows it, tokens are registered with
@property and carry a type: a color, a length, a time. The
browser checks what you assign against that type. A value that does not
fit is discarded and the default stays in place, instead of the token
going empty and every rule that reads it failing along with it.
Switching the scheme
Both schemes follow the operating system out of the box. To let the
visitor decide, set data-color-scheme:
// 'dark', 'light', or remove the attribute to follow the system again
document.documentElement.dataset.colorScheme = 'dark';
It is not limited to <html>. The effective tokens stay
unresolved until the element that finally reads them, so
color-scheme keeps working further down the tree and a single
panel can hold its own scheme: