Giter VIP home page Giter VIP logo

bootstrap-5.3-dark-mode-light-mode-switch's Introduction

Bootstrap Dark Mode Switch

A simple and reusable dark mode toggle switch using Bootstrap 5 for your website.

Description

This project integrates a dark mode switch into your website, allowing users to toggle between dark and light themes. It uses Bootstrap 5 for styling and JavaScript for functionality, with the state of the theme being persisted in local storage so that the user's preference is maintained across sessions.

Features

  • Easy to integrate with existing Bootstrap projects
  • Uses local storage to remember the user's theme preference
  • Automatically applies the user's preferred theme on page load

How to Use

To use this switch in your project, follow these steps:

  1. Make sure Bootstrap 5 is included in your project:
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Bootstrap Bundle JS (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  1. Include the HTML for the switch in your webpage:
<!-- Bootstrap 5 switch -->
<div class="form-check form-switch">
    <input class="form-check-input" type="checkbox" id="darkModeSwitch" checked>
    <label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
</div>
  1. Add the JavaScript code to handle the switch behavior:
document.addEventListener('DOMContentLoaded', (event) => {
    const htmlElement = document.documentElement;
    const switchElement = document.getElementById('darkModeSwitch');

    // Set the default theme to dark if no setting is found in local storage
    const currentTheme = localStorage.getItem('bsTheme') || 'dark';
    htmlElement.setAttribute('data-bs-theme', currentTheme);
    switchElement.checked = currentTheme === 'dark';

    switchElement.addEventListener('change', function () {
        if (this.checked) {
            htmlElement.setAttribute('data-bs-theme', 'dark');
            localStorage.setItem('bsTheme', 'dark');
        } else {
            htmlElement.setAttribute('data-bs-theme', 'light');
            localStorage.setItem('bsTheme', 'light');
        }
    });
});
  1. (Optional) Customize the switch's appearance using Bootstrap's form-check classes or your own CSS.

Customization

You can easily customize the look and feel of the dark mode switch to match your website's branding. Use Bootstrap's utility classes or your own CSS to create a unique user experience.

Contribution

Feel free to fork the project and submit pull requests. You can improve the script, add new features, or propose improvements to the documentation.

License

This project is open-source and available under the MIT License.

bootstrap-5.3-dark-mode-light-mode-switch's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.