How to Create dark theme using Slider in CSS ? Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like 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 HTMLDefine 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.cssStep 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> <head> <link rel="stylesheet" href="./style.css"> <script defer src="/script.js"></script> </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 Comment More infoAdvertise with us Next Article How to Create dark theme using Slider in CSS ? vikashgautam11 Follow Improve Article Tags : Web Technologies CSS CSS-Questions Similar Reads How to Create Multiple Themes using Tailwind CSS ? In this article, we will discuss how to create multiple themes using Tailwind CSS. Tailwind CSS is a popular utility-first CSS framework that provides a lot of pre-designed CSS classes for designing websites. With Tailwind CSS, you can easily create multiple themes for your website. This means that 3 min read How to Create a Slider using HTML and CSS? A slider (also called a slideshow) is a sequence of content frames that users can navigate through. Creating a slider using just HTML and CSS is efficient because it doesn't require JavaScript, making it lightweight and fast. The slider can contain images, text, or any other content you want to show 3 min read How to Darken an Image using CSS? To darken an image using CSS, the filter property with the brightness function and background-image property is used. By setting the brightness value below 1, you can reduce the lightness of the image, making it appear darker.1. Using the filter PropertyThe CSS filter property allows you to apply va 2 min read How to create Image Comparison Slider using CSS ? In this article, we will see how to create an Image Comparison Slider using CSS. The Image Comparison Slider essentially aids in the distinction of two photographs or products. As a result, the user can quickly determine which of the two products or two images in a better way.Approach: The approach 2 min read How to create linear gradient text using HTML and CSS ? The linear-gradient is used to create eye-catching text effects, particularly suitable for dark-themed websites or applications. This method involves filling text with linear-gradient color codes, making it look attractive and bold. Linear-gradient text effects are ideal for dark themes and provide 2 min read How to create Form element Themed Range slider using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Form element Themed Range slider using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link 1 min read How to create dark mode using prefer-color-scheme media query ? In this article, we are creating a dark mode interface using the prefer-color-scheme media query. The prefer-color-scheme is a new media query that is used to detect the current theme of your system and provide a feature that helps in creating your web page theme according to your system preference. 3 min read How to create a marquee effect using CSS ? In this article, we will be creating the marquee effect by using HTML and CSS. This effect can be created by using the <marquee> tag in HTML, but we will not use this tag as it is not widely used nowadays. We will create and design a marquee effect by using the CSS classes and styling properti 2 min read How to make a Theme Slider using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Theme Slider using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href= 1 min read How to create image overlay hover slide effects using CSS ? In this article, we will learn how to make image overlay hover slide effects using CSS. When the users hover over the image, a smooth sliding overlay effect occurs. This technique gives a visual appeal with interactivity, making your website more interactive.Table of ContentFull Width Slide-In Text 4 min read Like