Using CSS selectors with @media queries
Hey! I want user to select between
dark
, light
, and system
themes (and maybe even third
, fourth
, etc).
I came up with this pen in mind: https://codepen.io/zobweyt/pen/BagdyGq
But it's not working as expected. I want to turn on dark styles when any of the following conditions satisfy:
- There's [data-theme="dark"]
on an element
- There's [data-theme="system"]
on an element with @media (prefers-color-scheme: dark)
I can't find a solution to make it without copy-pasting and mixins from SCSS or PostCSS, etc
Is it even possible?4 Replies
You'll need to copy paste, like you said. Media queries aren't selectors.
As for the
data-theme="system"
, I wouldn't include that anywhere, so it just goes back to the default that way.
They've added a light-dark()
function, but support for it isn't great yet, but it will make things a lot easier and allow you to do it with only selectors though ๐Didn't expect to get an answer right from you :) Thank you for your awesome videos!!
You'll need to copy pasteSo a more neat solution is to use a preprocessor like SASS for this, right?
As for the data-theme="system"
, I wouldn't include that anywhere, so it just goes back to the default that way.
Yeah, you're right, but I'd move the @media
queries upper. If you had more than 2 themes, it'd be better to do it like this:
I guess light-dark()
function is suitable if you have only 2 color schemes: light and dark
But it's still very cool!The above would work, yup!
They are also working on mixins for CSS, so that could also potentially solve it, but that's years away ๐
you can reduce a lot of the black/white part by using nested css:
this is actually plain css (but you can pass it through scss to make it compatible with older browsers, which you should avoid because plenty of chrome versions have RCE exploits that were used in the wild)