Custom themes
Any name passed to Theme() becomes a theme. Build a themed look once and switch it on by adding a single ancestor class.
A theme is an ancestor class
FluentCss does not ship a fixed list of themes. Dark is
just the built-in shortcut for Theme("dark"). Passing
any other name produces a new theme: every utility you build from that root is prefixed with the
theme name and scoped by an ancestor carrying the same name.
Inline usage in Razor
Custom themes compose with the same inline shorthands you already use:
@Css,
@Dark and
@Theme("name"). Stack the variants in one
class attribute and the active ancestor decides which
one wins.
Inline themed card
Click Ocean or Sunset below to re-skin this card.
<div class="@Css.P(6).RoundedLg().Border().BorderColor(Color.Slate200)
.Background(Color.White).Color(Color.Slate900)
@Dark.Background(Color.Slate800).BorderColor(Color.Slate700).Color(Color.White)
@Theme("ocean").Background(Color.Sky50).BorderColor(Color.Sky300).Color(Color.Sky900)
@Theme("sunset").Background(Color.Orange50).BorderColor(Color.Orange300).Color(Color.Orange900)">
<p class="@Css.FontBold().Mb(1)">Inline themed card</p>
<p class="@Css.TextSm().Color(Color.Slate600)
@Dark.Color(Color.Slate300)
@Theme("ocean").Color(Color.Sky800)
@Theme("sunset").Color(Color.Orange800)">
Click Ocean or Sunset above to re-skin this card.
</p>
</div>
Try it on this whole page
The buttons below add theme-ocean or
theme-sunset to the
<html> element, so the entire page re-skins.
Nothing is written to the cookie: as soon as you navigate away the class is removed and the
persisted light/dark choice is restored, so these themes never follow you to other pages.
Revenue
$48.2k
Active users
3,214
Churn
1.8%
Applying a theme
Theming is plain DOM work. Add the ancestor class to opt in, remove it to opt out. There is no registry call and no re-render: the CSS for the themed utilities already exists and only activates while the ancestor class is present.
// Opt in: an ancestor carries the theme class.
document.documentElement.classList.add('theme-ocean');
// Opt out again.
document.documentElement.classList.remove('theme-ocean');
// Never persisted, so a navigation restores the normal theme.
// (Only 'dark' / 'light' are stored in the FluentCss cookie.)
Why this never leaks to other pages
dark and
light in the cookie. Custom themes are applied at
runtime and are removed by the navigation cleanup in docs.js,
so another page can never inherit them. If you want a custom theme to survive a reload,
store its name in your own cookie and read it the same way the dark theme is read.
Combining with hover and responsive variants
Prefixes compose in a fixed order — media, then theme, then pseudo — so a themed and responsive utility reads naturally.
Theme("ocean")
.MinMd(md => md
.Background(Color.Sky100)
.Hover(h => h.Background(Color.Sky200)));
// classes: min-md:theme-ocean:bg-sky-100
// min-md:theme-ocean:hover:bg-sky-200