reactjs - lightdark theme is overriding additional classes I add later - Stack Overflow

admin2025-04-22  0

I have the following light/dark theme setup in my app.css which gives default styling to <div> and <a> tags:

<html lang="en" data-theme="dark">
... code omitted for brevity
</html>
@import "tailwindcss";
@import "tailwindcss/utilities";

@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
@theme {
    --color-light-bg: #ffffff;
    --color-light-primary: #e6e6e6;
    --color-light-secondary: #d1d1d1;
    --color-light-text: #000000;
    --color-light-accent: #26a1ff;

    --color-dark-bg: #1e1e1e;
    --color-dark-primary: #292929;
    --color-dark-secondary: #363636;
    --color-dark-text: #ffffff;
    --color-dark-accent: #006aba;
}

body {
    height: 100vh;
    width: 100vw;
    display: flex;
    @apply bg-light-primary text-light-text;
}

[data-theme="dark"] {
    body {
        @apply bg-dark-bg text-dark-text;
    }
}

div {
    @apply bg-light-bg;
}

[data-theme="dark"] div {
    @apply bg-dark-primary;
}

button {
    @apply bg-light-accent text-light-text;
}

[data-theme="dark"] button {
    @apply bg-dark-accent text-dark-text;
}

a {
    @apply text-light-text;
}

[data-theme="dark"] a {
    @apply text-dark-text;
}

However these styles overwrite any tailwind classes I decide to add later, such as:

<NavLink to="/genres" className="text-red-500">Genres</NavLink>

As seen in the GIF above, any class I choose to add appears to have lower specificity than the light & dark mode classes.

Is there any approach I can take to make sure this isn't the case?

Any help would be great, thank you!

I have the following light/dark theme setup in my app.css which gives default styling to <div> and <a> tags:

<html lang="en" data-theme="dark">
... code omitted for brevity
</html>
@import "tailwindcss";
@import "tailwindcss/utilities";

@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
@theme {
    --color-light-bg: #ffffff;
    --color-light-primary: #e6e6e6;
    --color-light-secondary: #d1d1d1;
    --color-light-text: #000000;
    --color-light-accent: #26a1ff;

    --color-dark-bg: #1e1e1e;
    --color-dark-primary: #292929;
    --color-dark-secondary: #363636;
    --color-dark-text: #ffffff;
    --color-dark-accent: #006aba;
}

body {
    height: 100vh;
    width: 100vw;
    display: flex;
    @apply bg-light-primary text-light-text;
}

[data-theme="dark"] {
    body {
        @apply bg-dark-bg text-dark-text;
    }
}

div {
    @apply bg-light-bg;
}

[data-theme="dark"] div {
    @apply bg-dark-primary;
}

button {
    @apply bg-light-accent text-light-text;
}

[data-theme="dark"] button {
    @apply bg-dark-accent text-dark-text;
}

a {
    @apply text-light-text;
}

[data-theme="dark"] a {
    @apply text-dark-text;
}

However these styles overwrite any tailwind classes I decide to add later, such as:

<NavLink to="/genres" className="text-red-500">Genres</NavLink>

https://gyazo/1da170a1f413777ec7dfbdf43ec77799

As seen in the GIF above, any class I choose to add appears to have lower specificity than the light & dark mode classes.

Is there any approach I can take to make sure this isn't the case?

Any help would be great, thank you!

Share Improve this question edited Jan 29 at 13:42 rozsazoltan 11.5k6 gold badges21 silver badges60 bronze badges asked Jan 29 at 8:15 GanzGanz 977 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You'd want to add the rules into a @layer, such as @layer base perhaps. This is needed since the utility classes are compiled into a cascade layer. Styles in cascade layer have lower precedence than those outside. Consider reading up on cascade layers on MDN.

<html data-theme="dark">
  <script src="https://unpkg/@tailwindcss/[email protected]"></script>

  <style type="text/tailwindcss">
  @import "tailwindcss";
  @import "tailwindcss/utilities";

  @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
  @theme {
      --color-light-bg: #ffffff;
      --color-light-primary: #e6e6e6;
      --color-light-secondary: #d1d1d1;
      --color-light-text: #000000;
      --color-light-accent: #26a1ff;

      --color-dark-bg: #1e1e1e;
      --color-dark-primary: #292929;
      --color-dark-secondary: #363636;
      --color-dark-text: #ffffff;
      --color-dark-accent: #006aba;
  }

  @layer base {
    body {
        height: 100vh;
        width: 100vw;
        display: flex;
        @apply bg-light-primary text-light-text;
    }

    [data-theme="dark"] {
        body {
            @apply bg-dark-bg text-dark-text;
        }
    }

    div {
        @apply bg-light-bg;
    }

    [data-theme="dark"] div {
        @apply bg-dark-primary;
    }

    button {
        @apply bg-light-accent text-light-text;
    }

    [data-theme="dark"] button {
        @apply bg-dark-accent text-dark-text;
    }

    a {
        @apply text-light-text;
    }

    [data-theme="dark"] a {
        @apply text-dark-text;
    }
  }
  </style>

  <a href="/genres" class="text-red-500">Genres</a>
  <a href="/genres">Genres</a>
</html>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745304992a295485.html

最新回复(0)