How a data-theme Attribute Changes an Application’s Color Palette

Live demo at CodePen

I was already using CSS variables, mainly for a simple reason: I wanted to define a color in one place and reuse it wherever it was needed. What I hadn’t considered was that I could keep the same variable names and change their values across the entire application with a single HTML attribute.

I use this in desktop-style web applications with forms that open in tabs and dialogs. With the customers grid open, I can select Forest from the toolbar, and the grid immediately takes on the new colors. The same grid, in the same tab. All JavaScript had changed was the data-theme attribute on <body>.

One Panel, Two Palettes

Let’s start with a piece of HTML you can try on a blank page.

<body data-theme="light"> <button id="theme-toggle" type="button">Change Theme</button> <section class="panel"> <h2>Application Settings</h2> <p>Choose the palette you prefer.</p> </section> </body> 

The panel needs a background color and a text color. The variable names describe where they are used, so we can keep them the same for every palette.

.panel { padding: 1rem; background-color: var(--app-surface); color: var(--app-text); } 

The values are defined in two CSS rules. Each one matches <body> when data-theme has the corresponding value.

body[data-theme="light"] { --app-surface: #f4f4f4; --app-text: #20262c; } body[data-theme="forest"] { --app-surface: #24392e; --app-text: #f4f4f4; } 

Now place this script after the button and panel:

<script> const button = document.getElementById("theme-toggle"); button.addEventListener("click", () => { const currentTheme = document.body.dataset.theme; document.body.dataset.theme = currentTheme === "light" ? "forest" : "light"; }); </script> 

Click the button and look at <body> in DevTools. You’ll see data-theme change from light to forest. If you inspect the panel, its rule still says background-color: var(--app-surface). The difference appears in Computed. The background is now rgb(36, 57, 46), which is Forest’s #24392e.

That’s the whole trick. The browser recalculates which rule matches <body>. CSS variables are inherited by its descendants, so the panel picks up the new value. If you add a second panel to the DOM after selecting Forest, it will appear with the Forest palette straight away. The CSS it needs is already there.

The screenshots below show sample applications from Tripous.Seven.

The repository contains the code and sample applications for a book I am writing on building data-entry and business applications for desktop and web environments.

Light Theme

Light

Forest Theme

Forest

Connecting the Palette to Existing Controls

Suppose you already have a grid that uses --grid-background for its background and --grid-selection for the selected row. You can connect those variables to the application palette in one place:

body[data-theme] { --grid-background: var(--app-surface); --grid-selection: var(--app-selection); } body[data-theme="light"] { --app-surface: #f4f4f4; --app-selection: #cce5f8; } body[data-theme="forest"] { --app-surface: #24392e; --app-selection: #326149; } 

Notice the difference between the selectors. body[data-theme] matches whenever <body> has the attribute, regardless of its value. That’s where you define where the grid gets its colors. The more specific selector, body[data-theme="forest"], supplies the Forest palette values. When you select another theme, the grid continues to use the same variables, and the browser applies the new values.

The same applies to a dialog you load later with Ajax. As soon as its HTML is inserted beneath <body>, it inherits the active palette’s variables. If its title bar has background-color: var(--app-surface), it immediately gets the correct color. The code that opens the dialog can focus on opening the dialog.

The Right Palette from the First Render

In the small example, we wrote data-theme="light" by hand. If the application saves each user’s preference and generates its HTML on the server, it can put the saved value there. In ASP.NET Core Razor, for example, the layout could contain:

<body data-theme="@(ViewData["Theme"])"> @RenderBody() </body> 

If you selected Forest last time, the browser receives data-theme="forest" with the initial HTML. The correct palette is applied from the page’s first render. When you change your selection, JavaScript immediately updates the attribute and sends the new value to the server for next time.

You May Have Used It Already

If you’ve changed the theme in an application that uses a CSS library, such as Bootstrap, you may already have seen this in action. The library might put the theme on <html> for the entire page, or on an element containing only the area you want to change. In both cases, the idea is the same. The selector matches the element that declares the palette, and its descendants inherit the values.

What stayed with me is a question worth asking whenever I write var(--app-surface): Where do I want its value to be defined? For a palette that applies to the whole application, <body> is a natural place. For a particular section of the page, the elements’ shared parent is enough. Once the values are connected to data-theme, changing the attribute updates the entire area.