Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What am I doing wrong on this dark mode button?

I started coding today and I’m trying to make a simple static dark mode button, but the background of the button doesn’t change his color, i already fixed one thing on the line 36 on CSS it was "–bg: var(–gren);" but the screen would be black when i clicked (https://i.stack.imgur.com/CYUB4.png):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>site</title>
    <link rel="stylesheet" href="main.css">
</head>
<body class="light-theme">
    <h1>Task List</h1>
    <p id="msg">Current tasks:</p>
    <ul>
        <li class="list">Add visual styles</li>
        <li class="list">Add light and dark themes</li>
        <li> Enable switching the theme</li>
    </ul>
    <div>
        <button class="btn">Dark</button>
    </div>
    <script src="app.js" defer></script>
    <noscript>You need to enable JavaScript to view the full site.</noscript>
</body>
</html>

body {
    font-family: monospace;
}
ul {
    font-family: Arial, Helvetica, sans-serif;
}

li {
    list-style: circle;
}

.list {
    list-style: square;
}

.light-theme {
    color: black;
    background: #00FF00;
    
}

:root {
    --green: #00FF00;
    --white: #ffffff;
    --black: #000000;
}

.light-theme {
    --bg: var(--green);
    --fontColor: var(--black);
    --btng: var(--black)
    --btnFontColor: var(--white)
}

.dark-theme {
    background: var(--black);
    --fontColor: var(--green);
    --btnBg: var(--white)
    --btnFontColor: var(--black)
}

* {
    color: var(--fontColor);
    font-family: Arial, Helvetica, sans-serif;
}

body  {
    background: var(--bg);
}

.btn {
    color: var(--btnFontColor);
    background-color: var(--btnBg);
}

.btn {
    position: absolute;
    top: 20px;
    left: 250px;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    border: none;
    color: var(--btnFontColor);
    background-color: var(--btnBg);
}


.btn:focus { outline: none; }




 'use scrict'

const switcher = document.querySelector('.btn');

switcher.addEventListener('click', function() {
    document.body.classList.toggle('dark-theme')

    var className = document.body.className;
    if(className == "light-theme") {
        this.textContent = "Dark";
    }   
    else {
        this.textContent = "Light"
    }

    console.log('current class name: ' + className);
});
enter image description here

how it was supposed to be: (https://i.stack.imgur.com/SGUMf.png)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You forgot to add the ";" at the end of the CSS properties in .light-theme and .dark-theme, and in .light-theme you had –btng rather than –btnBg.

.light-theme {
    --bg: var(--green);
    --fontColor: var(--black);
    --btnBg: var(--black);
    --btnFontColor: var(--white);
}

.dark-theme {
background: var(--black);
    --fontColor: var(--green);
    --btnBg: var(--white);
    --btnFontColor: var(--black);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading