Open In App

How to Create dark theme using Slider in CSS ?

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to create a dark theme using the slider in CSS. In modern websites, we see a wonderful feature to change the website themes just by checking a slider. To check this awesome feature go to the geeksforgeeks.org website where you can change the theme as per your preference. In this article, we will learn to create a slider to change the website theme. It allows users to customize the interface of the website as per their preference is the best user experience. Here we are going to create a nice slider to change our website theme. 

Steps to create a dark theme slider: These are the following steps to create a Dark theme slider. 

  • Create a webpage using HTML
  • Define all CSS variables such as background color, primary text color, secondary text color for the default theme, and dark theme.
  • Add functionality using JavaScript to toggle default mode to dark mode.

Example: The following example demonstrates how to create a dark theme using the slider in CSS.

Step 1: Create an index.html file and add the following HTML code into it to provide a basic structure to the webpage.

Step 2: Now we will create a new file for adding style to the above HTML code called style.css

Step 3: Now we will create a javascript file and make the function toggleSwitch which allow us to toggle between dark and light theme.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <!-- Stylesheet link -->
    <link rel="stylesheet" href="./style.css">
  
    <!-- JavaScript file -->
    <script defer src="/script.js"></script>
    <!-- Instead of add defer, embed javascript 
        file in body tag -->
</head>
<body>
    <!--Heading Section-->
    <header>
        <nav>
            <!-- Add your custom menu here 
                and theme slider -->
            <div class="theme-switch-container">
                <label class="theme-slider" for="checkbox">
                    <input type="checkbox" id="checkbox" />
                    <div class="round slider"></div>
                </label>
                <p>use this slider to change theme</p>
            </div>
        </nav>
    </header>
  
    <!-- Main Section -->
    <main>
        <h1>GeeksforGeeks</h1>
        <p>
            GeeksforGeeks is a best learning
            plateform for geeks.
        </p>
  
        <p>
            click below link to open
            geeksforgeeks website
        </p>
        <a href="https://www.geeksforgeeks.org/">
            GeeksforGeeks
        </a>
    </main>
</body>
  
</html>


CSS




/* Default light theme */
:root {
    --primary-color: #302AE6;
    --secondary-color: #536390;
    --text-color: #424242;
    --background-color: #fff;
    --heading-color: #292922;
}
  
/* Dark theme */
[theme="dark"] {
    --primary-color: #9A97F3;
    --secondary-color: #818cab;
    --text-color: #e1e1ff;
    --background-color: #161625;
    --heading-color: #818cab;
}
  
/* Adding css variable in our webpage */
body {
    background-color: var(--background-color);
    color: var(--text-color);
}
  
h1 {
    color: var(--secondary-color);
}
  
a {
    color: var(--primary-color);
}
  
/* Slider styling */
.theme-switch-container {
    display: flex;
    align-items: center;
}
  
.theme-slider {
    display: inline-block;
    height: 34px;
    position: relative;
    width: 60px;
}
  
.theme-slider input {
    display: none;
}
  
.slider {
    background-color: #ccc;
    bottom: 0;
    cursor: pointer;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    transition: .4s;
}
  
.slider:before {
    background-color: #fff;
    bottom: 4px;
    content: "";
    height: 26px;
    left: 4px;
    position: absolute;
    transition: .4s;
    width: 26px;
}
  
input:checked+.slider {
    background-color: #66bb6a;
}
  
input:checked+.slider:before {
    transform: translateX(26px);
}
  
.slider.round {
    border-radius: 34px;
}
  
.slider.round:before {
    border-radius: 50%;
}


Javascript




const toggleSwitch =
    document.querySelector('.theme-slider input[type="checkbox"]');
  
/* Function to change theme */
function switchTheme(e) {
  
    /* Once checkbox is checked default theme change to dark */
    if (e.target.checked) {
        document.documentElement.setAttribute('theme', 'dark');
    }
  
    /* While page in dark mode and checkbox is 
    checked then theme back to change light*/
    else {
        document.documentElement.setAttribute('theme', 'light');
    }
}
  
toggleSwitch.addEventListener('change', switchTheme, false);


Output: Click here to see live code output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads