5

How can I make the last part of the spinner lighter (ie. fading):

 
#loader-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
}
#loader {
    display: block;
    position: relative;
    left: 50%;
    top: 50%;
    width: 150px;
    height: 150px;
    margin: -75px 0 0 -75px;
    border-radius: 50%;
    border: 5px solid transparent;
    border-top-color: #aaa;
    border-right-color: #aaa;    
    animation: spin 2s linear infinite;
}
 
@keyframes spin {
    0%   {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
<div id="loader-wrapper">
  <div id="loader"></div>
</div>

I tried using gradient but it converts it to a square

doğukan
  • 23,073
  • 13
  • 57
  • 69
Cornwell
  • 3,304
  • 7
  • 51
  • 84

2 Answers2

9

You can apply the gradient to a pseudo-element like so:

#loader-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
}
#loader {
  display: block;
  position: relative;
  left: 50%;
  top: 50%;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border-radius: 50%;
  border: 5px solid transparent;
  border-top-color: #aaa;
  border-right-color: #aaa;
  animation: spin 2s linear infinite;
}
#loader::after {
  content: '';
  width: 85%;
  height: 85%;
  background: linear-gradient(45deg, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 40%, rgba(255, 255, 255, 0.7) 60%, rgba(255, 255, 255, 0) 100%);
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-5%, -5%);
}
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div id="loader-wrapper">
  <div id="loader"></div>
</div>
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
0

Here is another idea with less code and without using pseudo element.

.loader {
  --border-width: 5px;
  
  height: 150px;
  width: 150px;
  border-radius: 50%;
  
  /* 0.5px's are needed to avoid hard-stopping */
  --mask: radial-gradient(
    farthest-side, 
    transparent calc(100% - var(--border-width) - 0.5px), 
    #000 calc(100% - var(--border-width) + 0.5px)
  );
  
  -webkit-mask: var(--mask);
          mask: var(--mask); 
  background: linear-gradient(#aaa 30%, transparent 80%) 0 0/50% 100% no-repeat; /* this is our border image */
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div class="loader"></div>

And this is the comparison of my answer with @Ricky's answer by setting background to body:

@Ricky's way:

body {
  background: pink; /* just added this */
}

#loader-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
}
#loader {
  display: block;
  position: relative;
  left: 50%;
  top: 50%;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border-radius: 50%;
  border: 5px solid transparent;
  border-top-color: #aaa;
  border-right-color: #aaa;
  animation: spin 2s linear infinite;
}
#loader::after {
  content: '';
  width: 85%;
  height: 85%;
  background: linear-gradient(45deg, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 40%, rgba(255, 255, 255, 0.7) 60%, rgba(255, 255, 255, 0) 100%);
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-5%, -5%);
}
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div id="loader-wrapper">
  <div id="loader"></div>
</div>

My way:

body {
  background: pink; /* just added this */
}

.loader {
  --border-width: 5px;
  
  height: 150px;
  width: 150px;
  border-radius: 50%;
  
  /* 0.5px's are needed to avoid hard-stopping */
  --mask: radial-gradient(
    farthest-side, 
    transparent calc(100% - var(--border-width) - 0.5px), 
    #000 calc(100% - var(--border-width) + 0.5px)
  );
  
  -webkit-mask: var(--mask);
          mask: var(--mask); 
  background: linear-gradient(#aaa 30%, transparent 80%) 0 0/50% 100% no-repeat; /* this is our border image */
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div class="loader"></div>
doğukan
  • 23,073
  • 13
  • 57
  • 69