🌙Dark Mode Toggle </JS>🌙

·

1 min read

🌖Online Demo🌖 🔗colours.pages.dev


🎯index.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">
    <title>Dark Mode Toggle</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Nabla&display=swap" 
    rel="stylesheet">
</head>

<body>
    <div class="hero">
        <div class="content">
            <h1 class="header">Toggle Colour</h1>
            <div class="btn">
                <button id="mode" onclick="toggle()">🌙</button>
            </div>
        </div>
    </div>
</body>

</html>

🎯style.css

body {
    margin: 0;
    padding: 0;
    background-color: #0F111A;
}

.hero {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.content {
    margin-bottom: 10%;
    text-align: center;
}

.header {
    font-family: "Nabla";
    font-size: 7rem;
}


#mode {
    font-size: 2rem;
    background: #1E293B;
    padding: 0.5rem;
    border: none;
    cursor: pointer;
    border-radius: 100%;
}

🎯script.js

index = 0
function toggle() {
    let colors = ["#DEFFFF", "#0F111A"];
    document.body.style.background = colors[index++]

    if (index > colors.length - 1) {
        index = 0;
    }

    let mode = document.getElementById("mode")

    if (index == 0) {
        mode.innerHTML = "🌙"
        mode.style.background = "#1E293B";
    }
    else {
        mode.innerHTML = "☀️"
        mode.style.background = "white";
    }
}