☼ ☾

IPPC Design Docs → Theme Switcher

Dark Mode Website Theme Switcher with CSS and JavaScript Using localStorage instead of Cookies

A dark mode CSS/JS theme switcher to help reduce eyestrain in different lighting conditions, such as changing colors to light text on a dark background at night.

Try it by clicking on the sun and moon ☼ ☾ icons on the top of the page.

Your setting preference is saved with web browsers’ localStorage instead of cookies to help avoid needing “Accept Cookies” popups.

  1. Put data-theme="light" in your <html> tag:
  2. Set theme colors as variables in your linked stylesheet.css (<link rel="stylesheet" href="/ippc/work/templates.css" type="text/css" media="screen" />):
  3. Add a toggle link to the HTML (see the top-right of this webpage):
  4. Link to the following theme switcher JavaScript code before the closing </body> tag of the webpage (<script src="/ippc/work/scripts.js"></script> </body>):

Done! See it working by clicking the ☼ ☾ icons on the top-right of the page.


Updated on 2024-02-15