Add Dark and Light Mode Using CSS and Save Preference

If you want to allow users to allow preference for dark and light mode, you can use the below given examples.

Firstly, save the below file as index.html and add the HTML content. It also contains links to css style and script js files.

The below file contains the CSS for dark and light mode. The below example CSS code apply CSS to classes based on dark and light mode. Save the below file as ‘styles.css‘.

The JS script contains the code to toggle the dark and light mode. In addition to this, it also save the preference using browser local storage. Save the below file as ‘script.js‘.

You can combine these file to create a page with dark and light mode feature.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.