Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dark/Light mode using two CSS files

I am using a web software, that allows to create themes. It creates a single CSS file per theme, so i've got two CSS files: dark.css and light.css.

What's the best way to create a dark/light mode switch (independent from the visitor's system settings) using both CSS files? I've tried loading the opposite theme upon click, but that's very impractical and requires at least one more roundtrip.

I can embed the CSS files contents into the DOM, if that helps to achieve a smooth switch.

like image 912
Sgl Avatar asked Jun 07 '26 00:06

Sgl


1 Answers

If you'd like to keep CSS files separate you can use:

<link rel="stylesheet"
    href="css/light.css">
<link rel="stylesheet" media="(prefers-color-scheme: dark)"
    href="css/dark.css">
like image 96
jazzcool Avatar answered Jun 08 '26 13:06

jazzcool