Dark CSS

Light and Dark Theme Toggle using Html CSS and JavaScript

Facebook
Twitter
WhatsApp

code preview

html code

				
					<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">   
</head>

<body>
    <button id="btn">DARK</button>
</body>

</html>
				
			

css code

				
					* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    transition: background 0.5s;
}

body.light {
    background-color: #e8e8e8;
}

body.dark {
    background-color: #212121;
}

#btn {
    padding: 10px 15px;
    cursor: pointer;
    border: 0;
    outline: none;
    background-color: #212121;
    font-size: 1rem;
    color: #fff;
    font-weight: 600;
    letter-spacing: 1px;
    border-radius: 4px;
    box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.25);
}
				
			

javascript code

				
					document.body.classList.add('light');
            document.getElementById('btn').addEventListener('click', () => {
                document.body.classList.toggle('dark');
                document.body.classList.toggle('light');

                const btn = document.getElementById('btn');
                if (btn.innerText === 'DARK') {
                    btn.innerText = 'LIGHT';
                    btn.style.backgroundColor = '#e8e8e8'
                    btn.style.color = '#000'
                } else {
                    btn.innerText = 'DARK';
                    btn.style.backgroundColor = '#212121';
                    btn.style.color = '#fff'
                }
            })