skip to Main Content

I have a fully functional spinning loading indicator in pure CSS, works great with a non-white background (the page background is gray in the example below). The circles are apparently white by default so if I set the page background to white, nothing shows. How would I modify the CSS so that it works with a white background too? For the sake of an example let’s say the circles should be green.

<html>
    <head>
<style>
.loading {
  display: inline-block;
  position: relative;
  width: 80px;
  height: 80px;
}
.loading div {
  position: absolute;
  border: 4px solid #fff;
  opacity: 1;
  border-radius: 50%;
  animation: loading 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
}
.loading div:nth-child(2) {
  animation-delay: -0.5s;
}
@keyframes loading {
  0% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  4.9% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  5% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    top: 0px;
    left: 0px;
    width: 72px;
    height: 72px;
    opacity: 0;
  }
}
</style>
    </head>
    <body style="background-color: gray;">


        <p>beginning</p>
        <div class="loading">
            <div></div>
            <div></div>
        </div>
        <p>end</p>

    </body>
</html>

2

Answers


  1. Chosen as BEST ANSWER

    From @jme11: just changing the border color like this did the trick:

    .loading div {
      border: 4px solid green;
    }
    

  2. If you add a blend mode, then your WHITE circles will be visible on any background:

    .loading {
      display: inline-block;
      position: relative;
      width: 80px;
      height: 80px;
    }
    
    /* For example only --> */ .loading:nth-of-type(1) { background-image: linear-gradient(#000 50%, #000 50%); }.loading:nth-of-type(2) { background-image: linear-gradient(#000 50%, #fff 50%); }.loading:nth-of-type(3) { background-image: linear-gradient(#fff 50%, #fff 50%); }.loading:nth-of-type(4) { background-image: linear-gradient(#fff 0%, #000 100%); }.loading:nth-of-type(5) { background-image: linear-gradient(45deg, #f00, #f000, #080), linear-gradient(-45deg, #fa0, #f000, #08a); }
    
    .loading div {
      position: absolute;
      border: 4px solid #fff;
      opacity: 1;
      border-radius: 50%;
      
      mix-blend-mode: difference; /* or "exclusion" */
      
      animation: loading 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
    }
    
    .loading div:nth-child(2) {
      animation-delay: -0.5s;
    }
    
    @keyframes loading {
      0% {
        top: 36px;
        left: 36px;
        width: 0;
        height: 0;
        opacity: 0;
      }
      4.9% {
        top: 36px;
        left: 36px;
        width: 0;
        height: 0;
        opacity: 0;
      }
      5% {
        top: 36px;
        left: 36px;
        width: 0;
        height: 0;
        opacity: 1;
      }
      100% {
        top: 0px;
        left: 0px;
        width: 72px;
        height: 72px;
        opacity: 0;
      }
    }
    <body style="background-color: gray;">
      <p>beginning</p>
    
      <div class="loading"><div></div><div></div></div>
      <div class="loading"><div></div><div></div></div>
      <div class="loading"><div></div><div></div></div>
      <div class="loading"><div></div><div></div></div>
      <div class="loading"><div></div><div></div></div>
    
      <p>end</p>
    </body>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search