# CSS element based on color scheme selected

**URL:** https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218
**Category:** Development
**Tags:** css
**Created:** [October 18, 2022, 3:49pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218 "2022-10-18T15:49:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [October 18, 2022, 3:49pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/1 "2022-10-18T15:49:30Z")

</div>

I’m trying to figure out out to set a color of an element based on the color scheme selected. The following works well for my light mode, but not so well in my dark mode.

```plaintext
.d-header-icons .d-icon {
	width: 100%;
	line-height: 1.4;
	display: inline-block;
	color: var(--secondary);
}

```

I’d prefer to have the following for dark mode only.

```plaintext
color: var(--secondary-low) 

```

That works well for dark mode, but not for light mode. Is there any way to adjust the value of `color:` based on the color scheme selected?

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [October 18, 2022, 5:01pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/2 "2022-10-18T17:01:04Z")

</div>

Hello,

There are several ways to do it.

For example:

You can change the color scheme colors on `admin/customize/colors/`. Just select your dark mode color scheme and change the `secondary` color code.

> Note: This will change the secondary color everywhere not just on the header icons.

* * *

Or you can use the `prefers-color-scheme` media to change `secondary` to `secondary-low` on dark mode.

Something like this:

```scss
.d-header-icons .d-icon {
  width: 100%;
  line-height: 1.4;
  display: inline-block;
  color: var(--secondary);
  @media (prefers-color-scheme: dark) {
    color: var(--secondary-low);
  }
}

```

* * *

Or you can create custom color definitions: [Why might dark-light-choose() not work? - #2 by awesomerobot](https://meta.discourse.org/t/why-might-dark-light-choose-not-work/172232/2)

* * *

Hope this helps 🙂

---

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [October 18, 2022, 6:39pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/3 "2022-10-18T18:39:22Z")

</div>

> [@Don](#):
>
> ```plaintext
> .d-header-icons .d-icon {
> width: 100%;
> line-height: 1.4;
> display: inline-block;
> color: var(--secondary);
> @media (prefers-color-scheme: dark) {
> color: var(--secondary-low);
> }
> }
> 
> ```

Thanks for the guidance.

This appears to be exactly what I would like to do (change header icons only, not all items with the `--secondary` color). One final question … this works if the theme is set as the dark mode theme and dark mode is activated by the OS, but not if the dark mode theme is choosen as the “Regular” theme. Any suggestions there?

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [October 18, 2022, 7:02pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/4 "2022-10-18T19:02:54Z")

</div>

> [@agf1997](#):
>
> but not if the dark mode theme is choosen as the “Regular” theme

Oh yes that is not going to work like that if the dark color scheme is selectable.

* * *

I think the best option you can do is create a custom color definition for this and you can use this color variable for header icons.

Create a theme component and add the following to **Color Definitions** tab

 ![Screenshot 2022-10-18 at 20.56.24](https://global.discourse-cdn.com/meta/original/4X/d/4/1/d4125320b31a9011425739a5fa9d72fd097531e8.png)

Set the `secondary-low` color code to `$dark-theme-header-icons` and `secondary` color code to `$light-theme-header-icons`

```scss
$dark-theme-header-icons: #e9e9e9;
$light-theme-header-icons: #222;

$header-icons: dark-light-choose($light-theme-header-icons, $dark-theme-header-icons);

:root {
  --custom-header-icons: #{$header-icons};
}

```

After this you can use this `--custom-header-icons` color variable to the header icons color.

```scss
.d-header-icons .d-icon {
  width: 100%;
  line-height: 1.4;
  display: inline-block;
  color: var(--custom-header-icons);
}

```

---

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [October 18, 2022, 7:19pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/5 "2022-10-18T19:19:44Z")

</div>

This worked like a charm! Thanks!

---

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [October 18, 2022, 8:01pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/6 "2022-10-18T20:01:59Z")

</div>

Just one more thing … will this work if I include `color_definitions.scss` in my theme instead of using a theme component?

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [October 18, 2022, 8:10pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/7 "2022-10-18T20:10:02Z")

</div>

Sure, that will works too. 🙂

---

<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: [November 17, 2022, 8:10pm UTC](https://meta.discourse.org/t/css-element-based-on-color-scheme-selected/242218/8 "2022-11-17T20:10:39Z")

</div>

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