-2
<html>
   <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <style>
         .loader {   
         border: 16px solid #f3f3f3;
         border-radius: 50%;
         border-top: 16px solid #3498db;
         width: 120px;
         height: 120px;
         -webkit-animation: spin 2s linear infinite; /* Safari */
         animation: spin 2s linear infinite;
         }
         @-webkit-keyframes spin {
         0% { -webkit-transform: rotate(0deg); }
         100% { -webkit-transform: rotate(360deg); }
         }
         @keyframes spin {
         0% { transform: rotate(0deg); }
         100% { transform: rotate(360deg); }
         }
      </style>
   </head>
   <body>
      <h2>How To Create A Loader</h2>
      <div class="loader"></div>
   </body>
</html>

I am not able to align the loader at the center of the screen. I have tried putting align:center in the loader class but that did not work.

Boken
  • 4,825
  • 10
  • 32
  • 42

3 Answers3

0

Use flex box it would be simple and no extra css.

display: flex;
align-items: center;

You can study about flex box for further details, here and here

Wahab Shah
  • 2,066
  • 1
  • 14
  • 19
-1

<html>
   <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <style>
         body {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          height: 100vh;
         }
         .loader {   
           border: 16px solid #f3f3f3;
           border-radius: 50%;
           border-top: 16px solid #3498db;
           width: 120px;
           height: 120px;
           -webkit-animation: spin 2s linear infinite; /* Safari */
           animation: spin 2s linear infinite;
           }
           @-webkit-keyframes spin {
           0% { -webkit-transform: rotate(0deg); }
           100% { -webkit-transform: rotate(360deg); }
           }
           @keyframes spin {
           0% { transform: rotate(0deg); }
           100% { transform: rotate(360deg); }
         }
      </style>
   </head>
   <body>
      <h2>How To Create A Loader</h2>
      <div class="loader"></div>
   </body>
</html>
Spimy
  • 340
  • 2
  • 11
-2

Welcome to Stack Overflow, Himanshu.

You can use the margin property to center the loader on the screen. The margin property sets the margins for an element (i.e. how far to the top, right, bottom and/or left an item is in relation to another on the DOM). If you supply auto to margin-left and margin-right, the loader will be positioned on the center of the screen.

.loader {
    border: 16px solid #f3f3f3;
    border-radius: 50%;
    border-top: 16px solid #3498db;
    width: 120px;
    height: 120px;
    -webkit-animation: spin 2s linear infinite; /* Safari */
    animation: spin 2s linear infinite;
    margin-left: auto;
    margin-right: auto;
}

A shortened version would be margin: 0 auto 0 auto;.