Stacks provides a robust theming API to handle theming in various contexts.
Light and dark modesSection titled Light and dark modes
Stacks supports light and dark modes. By using Stacks, you will get dark mode for free. By using a Stacks component, an atomic color class, or CSS variable directly, your theme will switch appropriately based on the following methods:
- You can apply
bodyelement. This will change colors based on the
prefers-color-schememedia query, which is ultimately powered by the user’s system or browser settings. This can be preferable for folks who have their system turn to dark mode based on ambient light or time of day.
- Alternatively, you can set a dark mode that is not system dependent by attaching
There are also conditional classes that can be applied to override assumed dark mode colors, force light mode, or to force dark mode. Forcing modes can be good for previews in admin-only situations.
Global themingSection titled Global theming
Like light and dark modes, Stacks has baked-in theming available as primary and secondary CSS color variables. These can be overridden in various scopes. By default, the Stacks primary and secondary variables are Stack Overflow’s branded orange and blue, respectively, at a global scope, affecting every element on the page. These can be overridden in the context of your product, e.g. Teams, a Collective, or Stack Exchange Site. Both primary and secondary colors are available in the full spectrum of atomic classes and variables.
At its core, our theming is powered by CSS variables that are split into primary and secondary
B values. For example:
These then get combined to generate the full set of color stops. Overriding them is as simple as redefining those individual theming variables.
By default, we take those single primary and secondary colors and generate an appropriate dark mode equivalent. However, what if your branding color is dark purple—or black? You can pass an optional dark mode override:
Child themingSection titled Child theming
Stacks allows for further theming various portions of a page. You can simply pair the
.themed class with an atomic color stop, and a new theming scope. For this example, we’re using a class name of
.theme-team-[xxx] with a unique ID appended.
<div class="themed theme-team-001 bg-theme-primary-500"></div>
<div class="themed theme-team-002 bg-theme-primary-500"></div>
<div class="themed theme-team-003 bg-theme-primary-500"></div>