Background Color Fade-in & Fade-out with HTML & CSS

12/31/2020

Contents

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Background Color Fade-in & Fade-out</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="container">
      <div class="bg-fade">
        <h1>Background Color<br>Fade-in & Fade-out</h1>
      </div>
    </div>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
}
.bg-fade {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #4285f4;
  animation: bgFade 16s infinite;
}
h1 {
  color: #fff;
  font-size: 3rem;
  line-height: 1.4;
  text-align: center;
}
@keyframes bgFade {
   0% {
     background-color: #4285f4;
   }
   25% {
     background-color: #f4b400;
   }
   50% {
     background-color: #0f9d58;
   }
   75% {
     background-color: #db4437;
   }
}
@media screen and (max-width: 480px) {
  h1 {
    font-size: 1.5rem;
  }
}