Search code examples
htmlcsscss-animationsloader

Loader with five column bars


I have created a loader in css with three bars, the code is as given below. The bars are based on :before and :after. But if I want a five bar loader how can I do that ?

.loader,
.loader:before,
.loader:after {
    background: black;
    -webkit-animation: load1 1s infinite ease-in-out;
    animation: load1 1s infinite ease-in-out;
    width: 1em;
    height: 4em;
}

.loader {
    color: black;
    text-indent: -9999em;
    margin-top: 10px;
    margin-right: auto;
    margin-bottom: 10px;
    margin-left: auto;
    position: relative;
    font-size: 8px;
    -webkit-transform: translateZ(0);
    -ms-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-animation-delay: -0.16s;
    animation-delay: -0.16s;
}

.loader:before,
.loader:after {
    position: absolute;
    top: 0;
    content: '';
}

.loader:before {
    left: -2em;
    -webkit-animation-delay: -0.32s;
    animation-delay: -0.32s;
}

.loader:after {
    left: 2em;
}

@-webkit-keyframes load1 {
    0%,
    80%,
    100% {
        box-shadow: 0 0;
        height: 4em;
    }
    40% {
        box-shadow: 0 -2em;
        height: 5em;
    }
}

@keyframes load1 {
    0%,
    80%,
    100% {
        box-shadow: 0 0;
        height: 4em;
    }
    40% {
        box-shadow: 0 -2em;
        height: 5em;
    }
}

.loader-wrapper {
    display: block;
    position: relative;
    height: 56px;
}
<div class="loader-wrapper">
  <div class="loader">Loading...</div>
</div>


Solution

  • You could use the CSS propriety ntnchild. Your HTML and CSS will be like:

    .loading-bar {
      display: inline-block;
      width: 4px;
      height: 18px;
      border-radius: 4px;
      animation: loading 1s ease-in-out infinite;
    }
    
    .loading-bar:nth-child(1) {
      background-color: #3498db;
      animation-delay: 0;
    }
    
    .loading-bar:nth-child(2) {
      background-color: #c0392b;
      animation-delay: 0.09s;
    }
    
    .loading-bar:nth-child(3) {
      background-color: #f1c40f;
      animation-delay: .18s;
    }
    
    .loading-bar:nth-child(4) {
      background-color: #27ae60;
      animation-delay: .27s;
    }
    
    .loading-bar:nth-child(5) {
      background-color: #000000;
      animation-delay: .36s;
    }
    
    @keyframes loading {
      0% {
        transform: scale(1);
      }
    
      20% {
        transform: scale(1, 2.2);
      }
    
      40% {
        transform: scale(1);
      }
    }
    <div class="loading">
        <div class="loading-bar"></div>
        <div class="loading-bar"></div>
        <div class="loading-bar"></div>
        <div class="loading-bar"></div>
        <div class="loading-bar"></div>
    </div>