# How to add a specific body class for the dark (or light) mode?

**URL:** <https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062>\
**Category:** Product Support\
**Created:** [March 4, 2022, 2:30pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062 "2022-03-04T14:30:29Z")\
**Posts on this page:** 14\
**Page:** 1

## AI-generated summary

The discussion revolves around adding a specific body class for dark or light mode in Discourse. [Canapin](/t/-/220062/1) wants to customize the color scheme of his forum, specifically the generated color variable `--tertiary-very-low`, which produces a brownish color. However, he cannot change it without a parent-specific class for the dark mode.

[jordan-vidrine](/t/-/220062/5) suggests using the `@media` query for dark/light modes to set a custom color for links, but [Canapin](/t/-/220062/7) finds that this method does not work as expected. The `prefers-color-scheme` media query is based on the device's preference, not the actual selected Discourse theme.

[jordan-vidrine](/t/-/220062/10) explains that Discourse does not add a class to the body based on the user's currently selected color scheme, which is why [Canapin](/t/-/220062/11) is having trouble targeting specific color schemes in CSS.

[jordan-vidrine](/t/-/220062/12) provides a workaround by creating a custom color scheme using an `about.json` file, which can be installed as a theme. This approach allows for customizing the color scheme without relying on the built-in color functions.

[Canapin](/t/-/220062/13) acknowledges this solution and considers it a proper workaround, despite being a bit hacky.

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 4, 2022, 2:30pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/1 "2022-03-04T14:30:30Z")

</div>

There doesn’t seem to be any specific class currently.

I found [this](https://meta.discourse.org/t/add-currentthemeid-into-body-css-classes/161380/2) but it doesn’t work since the theme’s ID is the same for the dark and light mode (which is defined the color scheme, I guess).

I suppose a short script could help? And, maybe add this to Discourse’s core?

---

<div class="post-metadata">

**Author:** ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)\
**Post date:** [March 4, 2022, 3:02pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/2 "2022-03-04T15:02:43Z")

</div>

Would you mind explaining why you need specific classes added to the body for `dark` or `light` mode?

Does this core feature not allow what you are needing?

> [@Automatic Dark Mode color scheme switching](https://meta.discourse.org/t/automatic-dark-mode-color-scheme-switching/161593):
>
> You can now set up your Discourse site to automatically switch color schemes when the user’s device is in dark mode. For a quick preview, head over to the [try.discourse.org](http://try.discourse.org) instance and toggle your device’s dark mode on and off to see this new feature in action. (This feature not enabled on meta.) Enabling automatic dark mode To enable this feature on your instance, you can pick the dark mode color scheme in your site settings: Once that setting is set, you can reload your site with …

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 4, 2022, 3:13pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/3 "2022-03-04T15:13:23Z")

</div>

The staff of the forum I’m migrating doesn’t like a specific generated color variable (`--tertiary-very-low`) that makes this specific brownish color:

 ![image](https://global.discourse-cdn.com/meta/original/3X/0/d/0d69b6fb1926ec388647330b3eb8cbd0fcbaa498.png)

They would like to tweak it, and it seems I can’t really change it since there it’s generated AND is no parent-specific class for the dark mode.

However, I came across this information:

> [@$primary-medium SCSS variable problems](https://meta.discourse.org/t/primary-medium-scss-variable-problems/122390/5):
>
> We stumbled onto this thread because of a similar issue, we solved it with a workaround by adding the following to the theme’s common css: :root { --primary-medium: #666666; }

I don’t know if it’s valid, I couldn’t try it yet, too many things to do right now 😅. But if it works, that will do. It would be even better, in fact.

---

<div class="post-metadata">

**Author:** ![pierrox](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pierrox/32/251996_2.png) [@pierrox](https://meta.discourse.org/u/pierrox)\
**Post date:** [March 4, 2022, 4:30pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/4 "2022-03-04T16:30:17Z")

</div>

We (sorry, I’ll say we as I’m with @Canapin doing the tweaks on our forum) made a dark theme using the preset called “Café crème” in the french version of Discourse (“coffee” I assume) and we just changed the color of the ❤ to red instead of brown.

The strange thing with this theme is that the hyperlinks are almost the same color as the text.  
So when they are an embedded link, you can hardly see them:

 ![Capture d’écran 2022-03-04 à 17.15.58](https://global.discourse-cdn.com/meta/original/3X/d/3/d322286e8cbb992fb914652af78b590147af96a6.png)  
(yep, the last two words “cliquez-ici” are a link to a webpage…)

So our idea was to make them a bit brown, so they would stand out. But when we tweak the `quaternary` setting in the color palette, a whole bunch of colors change along: the background of the “bubble” behind the DM changes to something closer to bird poop than the brown shown in @Canapin 's first message. The top message box changes too (“the outgoing mail has been disabled” box) to this same birdpoop color.

So the idea was first to use the `css` to color the hyperlinks but we didn’t find out how yet.

---

<div class="post-metadata">

**Author:** ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)\
**Post date:** [March 4, 2022, 4:46pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/5 "2022-03-04T16:46:55Z")

</div>

Using `.cooked a` as a css target should work for you to set a custom color for these links.

While our core system now allows the use of dark / light color scheme choices for their corresponding modes, you should also be able to use the css media query for dark/light modes like so:

> **[prefers-color-scheme CSS media feature - CSS | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/prefers-color-scheme)**
>
> The prefers-color-scheme CSS media feature is used to detect if a user has requested light or dark color themes.
> A user indicates their preference through an operating system setting (e.g., light or dark mode) or a user agent setting.

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 5, 2022, 3:31pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/6 "2022-03-05T15:31:16Z")

</div>

```css
@media (prefers-color-scheme: dark) {
    .cooked a, .d-editor-preview a {
        color: var(--primary-low-mid);
    }
}

```

Did the work. Thank you!

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 5, 2022, 4:27pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/7 "2022-03-05T16:27:17Z")

</div>

Oops.  
No, it didn’t work.

Please correct me if I’m wrong. **prefers-color-scheme** is based on the _device_’s preference, not the actual selected Discoure’s theme?

On my Windows device, I set a Dark theme. So websites that use my device’s preference will show, if possible, a dark theme.

But on my forum, I deliberately chose to use a **light** theme, not a dark one.

The media query **prefers-color-scheme** is still loaded despite I’m using a light theme:

Light theme, no dark mode,

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/2/32460a16e5e7f276625f800961475e770617fdc3.png)

Still, the rule for the dark theme is applied nevertheless:

 ![image](https://global.discourse-cdn.com/meta/original/3X/9/1/91023c99806fd5d428644f67d2d213aee8895316.png)

Full screen:

 ![image](https://global.discourse-cdn.com/meta/original/3X/e/7/e7877bf636821534692f958e18f04424487ca701.png)

---

<div class="post-metadata">

**Author:** ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)\
**Post date:** [March 7, 2022, 3:48pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/8 "2022-03-07T15:48:03Z")

</div>

Sorry I misunderstood you to mean, the system setting of dark mode, triggering dark mode on the forum.

So are you personally using the light color scheme, but your users are using the dark color scheme?

The previous screenshot showed the dark color scheme in action, but your recent reply states you are using the light color scheme.

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 7, 2022, 5:07pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/9 "2022-03-07T17:07:01Z")

</div>

Hi, sorry, yeah it’s not clear, especially since I was talking about two things of the same kind but on different color schemes. Also, it was a bit confusing for me.

So my question would be: On Discourse, how do I know which color scheme is used, regardless of the device’s dark/light mode preferences?

If I understand things right:  
If my device (Windows 10) is set to Dark mode, but I select a Light color scheme in Discourse, the media query **(prefers-color-scheme: dark)** in my forum’s CSS will return some sort of “true” anyway and my nested rules will be applied, though I chose a Light color scheme on my forum (even if Windows is set to dark mode).

So: how do I know, in the HTML code of Discourse, if I’m currently using a Light or a Dark color scheme? I didn’t find any parent class like “light-color-scheme” or “dark-color-scheme”. So I can’t differentiate/target specific color schemes in CSS and create rules only for one or another.

---

<div class="post-metadata">

**Author:** ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)\
**Post date:** [March 7, 2022, 5:18pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/10 "2022-03-07T17:18:49Z")

</div>

Thanks for clarifying, I understand now.

> [@Canapin](#):
>
> So: how do I know, in the HTML code of Discourse, if I’m currently using a Light or a Dark color scheme? I didn’t find any parent class like “light-color-scheme” or “dark-color-scheme”. So I can’t differentiate/target specific color schemes in CSS and create rules only for one or another.

We do not add any class to the body based on the user’s currently selected color scheme.

If I can probe further, is the reason being that you dislike the color our color functions generate for `--tertiary` on your currently selected dark scheme?

What would you like to achieve through a body class that you feel like you are not able to do currently?

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 7, 2022, 5:52pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/11 "2022-03-07T17:52:50Z")

</div>

> [@jordan.vidrine](#):
>
> is the reason being that you dislike the color our color functions generate for `--tertiary` on your currently selected dark scheme?

Yup, exactly 🙂

> [@jordan.vidrine](#):
>
> What would you like to achieve through a body class that you feel like you are not able to do currently?

Now that I read this question… I’m not sure, since the only way to replace a generated color, as far as I know, is

```css
:root {
  --primary-medium: #666666;
}

```

But being a root selector, a class on the html or body element couldn’t target a color scheme before :root.  
So… I don’t know.

But it short, the idea would be to change a specific generated color for a specific color scheme. 🤷

No big deal if not possible though. We did some experiments with the our scheme colors and we’re currently satisfied enough with the generated colors. 🙂 Could be better, but that’s far from a big problem. 🙂

---

<div class="post-metadata">

**Author:** ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)\
**Post date:** [March 7, 2022, 6:03pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/12 "2022-03-07T18:03:46Z")

</div>

If you would like, there is a sort of hidden workaround.

You can create a folder and only add in an about.json file in it.

I actually did this to create a `solarized light` color scheme, because I wanted to specify my own variants, rather than rely on the built in color functions.

Here is the actual `about.json` file that I made.

You would then install this “theme”, which would in turn, install this color scheme onto your forum. At that point, you would be able to make it available for use on other themes as well.

```json
{
  "name": "Solarized Light",
  "component": false,
  "license_url": null,
  "about_url": null,
  "authors": null,
  "theme_version": null,
  "minimum_discourse_version": null,
  "maximum_discourse_version": null,
  "assets": {
  },
  "color_schemes": {
    "Solarized Light": {
      "primary": "002B36",
      "primary-high": "4C6869",
      "primary-medium": "7E918C",
      "primary-low-mid": "A4AFA5",
      "primary-low": "D6D8C7",
      "primary-very-low": "F0ECD7",
      "secondary": "FCF6E1",
      "secondary-very-high": "E8E6D3",
      "secondary-high": "97A59D",
      "secondary-medium": "6C8280",
      "secondary-low": "325458",
      "tertiary": "0088cc",
      "tertiary-high": "329ED0",
      "tertiary-medium": "7EBFD7",
      "tertiary-low": "D6E6DE",
      "quaternary": "e45735",
      "header_background": "FCF6E1",
      "header_primary": "002B36",
      "highlight": "ffff4d",
      "highlight-high": "BCAA7F",
      "highlight-medium": "E3D0A3",
      "highlight-low": "FDF9AD",
      "danger": "e45735",
      "danger-low": "F8D9C2",
      "success": "009900",
      "success-medium": "4CB544",
      "success-low": "CFE5B9",
      "love": "fa6c8d",
      "love-low": "FCDDD2"
    }
  },
  "modifiers": {
  },
  "learn_more": "https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966"
}

```

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [March 7, 2022, 6:14pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/13 "2022-03-07T18:14:11Z")

</div>

I remember seeing this a long time ago, when I was working more in-depth on my Discourse installations!

Definitely a good workaround. Thank you. I’ll consider that the proper solution, even if it may be considered as a bit hacky.

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [April 6, 2022, 6:14pm UTC](https://meta.discourse.org/t/how-to-add-a-specific-body-class-for-the-dark-or-light-mode/220062/14 "2022-04-06T18:14:44Z")

</div>

Closed on April 6, 2022, 6:14pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
