<button class="btn" id="toggleBtn">
  Toggle Dark Mode
</button>
:root{
    --primary: #4240b4;
    --background: #dddddd;
}

.dark{
    --background: #222222;
}

html,
body{
    background-color: var(--background);
    /* Animated Color change */
    transition: 0.2s ease-in-out;
    /* Additional Styling */
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;
    margin: 0;
    padding: 0;
}

.btn{
    background-color: var(--primary);
    /* Additional Styling */
    color: #ffffff;
    border: none;
    border-radius: 8px;
    padding: 12px 16px;
    outline: none;
    cursor: pointer
}
const body = document.querySelector("body")
const toggleBtn = document.querySelector("#toggleBtn")

toggleBtn.addEventListener("click", (e) => {
    body.classList.toggle("dark")
})

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.