Search code examples
htmlcssanimationmarkup

Display CSS loader and text on the same line


I have a custom CSS loader animation which I would like to display next to a text element on the same line, but so far I haven't managed it.

#loader{
  display:none;
}

#loader.active{
  display:block;
}
.lds-ring {
  display: inline-block;
  position: relative;
  width: 80px;
  height: 80px;
}

.lds-ring div {
  box-sizing: border-box;
  display: block;
  position: absolute;
  width: 64px;
  height: 64px;
  margin: 8px;
  border: 8px solid black;
  border-radius: 50%;
  animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
  border-color: black transparent transparent transparent;
}

.lds-ring div:nth-child(1) {
  animation-delay: -0.45s;
}

.lds-ring div:nth-child(2) {
  animation-delay: -0.3s;
}

.lds-ring div:nth-child(3) {
  animation-delay: -0.15s;
}

@keyframes lds-ring {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<p class="mytext"> LOADING </p>

<div id="loader">
  <div class="lds-ring">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

How can I display the animation just to the right of my 'mytext' element on the same line?


Solution

  • You can wrap both elements in a div, we'll give it a class name of 'flex':

    <div className="flex">
          <p className="mytext"> LOADING </p>
    
          <div class="loader">
            <div class="lds-ring">
              <div></div>
              <div></div>
              <div></div>
              <div></div>
            </div>
          </div>
        </div>
    

    And then add this CSS property to that div:

    .flex {
      display: flex;
      align-items: center;
    }
    

    That should align them as you wanted.