How to Create dark theme using Slider in CSS ?
Last Updated :
17 Feb, 2023
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" >
< link rel = "stylesheet" href = "./style.css" >
< script defer src = "/script.js" ></ script >
</ head >
< body >
< header >
< nav >
< 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 >
< h1 >GeeksforGeeks</ h1 >
< p >
GeeksforGeeks is a best learning
plateform for geeks.
</ p >
< p >
click below link to open
geeksforgeeks website
</ p >
GeeksforGeeks
</ a >
</ main >
</ body >
</ html >
|
CSS
:root {
--primary- color : #302AE6 ;
--secondary- color : #536390 ;
--text- color : #424242 ;
-- background-color : #fff ;
--heading- color : #292922 ;
}
[theme= "dark" ] {
--primary- color : #9A97F3 ;
--secondary- color : #818cab ;
--text- color : #e1e1ff ;
-- background-color : #161625 ;
--heading- color : #818cab ;
}
body {
background-color : var(--background-color);
color : var(--text-color);
}
h 1 {
color : var(--secondary-color);
}
a {
color : var(--primary-color);
}
.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: . 4 s;
}
.slider:before {
background-color : #fff ;
bottom : 4px ;
content : "" ;
height : 26px ;
left : 4px ;
position : absolute ;
transition: . 4 s;
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 switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute( 'theme' , 'dark' );
}
else {
document.documentElement.setAttribute( 'theme' , 'light' );
}
}
toggleSwitch.addEventListener( 'change' , switchTheme, false );
|
Output: Click here to see live code output
Share your thoughts in the comments
Please Login to comment...