Cookie Coder
Cookie Coder

Reputation: 33

How to fix CSS toggling for background color?

I am trying to make the background color of body to change by adding the "rainbow" class to it using javascript toggled by a button.

I have tried using my past code to toggle classes which has worked and I have added the class manually to check if my CCS was correct. I am unsure what is the cause of the problem.

<!DOCTYPE html>
<html>
<head>
<style>
    @keyframes bgcolor {
        0% {
            background-color: #45a3e5
        }

        30% {
            background-color: #66bf39
        }

        60% {
            background-color: #eb670f
        }

        90% {
            background-color: #f35
        }

        100% {
            background-color: #864cbf
        }
    }

    button {
        color: black;
        font-size: 48px;
        font-style: italic;
        font-family: "Comic Sans MS", cursive, sans-serif;
        text-align: center;
        padding: 20px;
        border-radius: 50px;
        background-color: gray;
    }

    .rainbow {
        -webkit-animation: bgcolor 20s infinite;
        animation: bgcolor 10s infinite;
        -webkit-animation-direction: alternate;
        animation-direction: alternate;
    }
</style>
<head>
<body id="body">
<button id="button">hello world</button>
<script>
function rainbowTime() {
        body.classList.toggle('rainbow');
    }
    button.onclick = rainbowTime;
</body>
</html>

When the button is pressed, the background is supposed to change color as it does if I edit the html and add the "rainbow" class. If I could have any help that would be great!

Upvotes: 1

Views: 90

Answers (2)

Asiya Fatima
Asiya Fatima

Reputation: 1438

In your code your not closing your tags, You should close the head tag and script tag

 <!DOCTYPE html>
 <html>
<head>
<style>
   @keyframes bgcolor {
      0% {
          background-color: #45a3e5
      }

    30% {
        background-color: #66bf39
    }

    60% {
        background-color: #eb670f
    }

    90% {
        background-color: #f35
    }

    100% {
        background-color: #864cbf
    }
}

button {
    color: black;
    font-size: 48px;
    font-style: italic;
    font-family: "Comic Sans MS", cursive, sans-serif;
    text-align: center;
    padding: 20px;
    border-radius: 50px;
    background-color: gray;
}

.rainbow {
    -webkit-animation: bgcolor 20s infinite;
    animation: bgcolor 10s infinite;
    -webkit-animation-direction: alternate;
    animation-direction: alternate;
}
</style>
</head>
<body id="body">
<button id="button">hello world</button>
 </body>
  </html>
  <script>
    function rainbowTime() {
      body.classList.toggle('rainbow');
    }
     button.onclick = rainbowTime;
  </script>

Upvotes: 1

Sankar
Sankar

Reputation: 7107

You have some unclosed tags.

head was not closed. script was not closed.

whenever you face problem with JavaScript. checkout the browser console for more information.

<!DOCTYPE html>
<html>

<head>
  <style>
    @keyframes bgcolor {
      0% {
        background-color: #45a3e5
      }
      30% {
        background-color: #66bf39
      }
      60% {
        background-color: #eb670f
      }
      90% {
        background-color: #f35
      }
      100% {
        background-color: #864cbf
      }
    }
    
    button {
      color: black;
      font-size: 48px;
      font-style: italic;
      font-family: "Comic Sans MS", cursive, sans-serif;
      text-align: center;
      padding: 20px;
      border-radius: 50px;
      background-color: gray;
    }
    
    .rainbow {
      -webkit-animation: bgcolor 20s infinite;
      animation: bgcolor 10s infinite;
      -webkit-animation-direction: alternate;
      animation-direction: alternate;
    }
  </style>

</head>

<body id="body">
  <button id="button">hello world</button>
  <script>
    function rainbowTime() {
      body.classList.toggle('rainbow');
    }
    button.onclick = rainbowTime;
  </script>
</body>

</html>

Upvotes: 2

Related Questions