Circle Loader Animation CSS

Circle Loader Animation CSS
Code Snippet:Conic Loader using @Property
Author: Jhey
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 786
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a Circle Loader Animation using CSS. It displays a spinning circular loader on a web page. The loader is designed for visual appeal and can be used to indicate background processes or loading times on websites.

It enhances user experience by providing a visually engaging and informative loading animation. You can use this loader animation code in your website’s loading screens, forms, or any page element that requires a loading indicator.

How to Create Circle Loader Animation CSS

1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your webpage.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

2. Create the HTML structure for the loader. The following code snippet creates a <div> element with a class of “loader,” which we will style with CSS.

<div class="loader"></div>

2. Now, add the following CSS code to your website’s CSS file or within a <style> tag in your HTML document.

@property --nose {
  syntax: "<percentage>";
  initial-value: 0%;
  inherits: true;
}

@property --tail {
  syntax: "<percentage>";
  initial-value: 0%;
  inherits: true;
}

:root {
  --size: 25;
}
body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  background: hsl(0, 0%, 8%);
}

.loader {
  height: calc(var(--size) * 1vmin);
  width: calc(var(--size) * 1vmin);
  position: relative;
  border-style: solid;
  border-width: 5vmin;
  border-color: transparent;
  border-radius: 50%;
  -webkit-mask: conic-gradient(
    from 45deg,
    transparent 0 var(--tail),
    white 0 var(--nose),
    transparent 0
  );
          mask: conic-gradient(
    from 45deg,
    transparent 0 var(--tail),
    white 0 var(--nose),
    transparent 0
  );
  -webkit-animation: load 2.5s both infinite ease-in-out, spin 3.25s infinite linear;
          animation: load 2.5s both infinite ease-in-out, spin 3.25s infinite linear;
}

.loader::after {
  content: "";
  position: absolute;
  inset: -5vmin;
  background: conic-gradient(
      from 95deg in hsl longer hue,
      hsl(240deg 100% 75%),
      hsl(240deg 100% 75%)
    ) -5vmin -5vmin / calc(100% + 10vmin) calc(100% + 10vmin);
  background: goldenrod;
  border-style: solid;
  border-width: 5vmin;
  border-color: transparent;
  border-radius: 50%;
  -webkit-mask: linear-gradient(white 0 0) padding-box,
    linear-gradient(white 0 0);
  -webkit-mask-composite: xor;
          mask: linear-gradient(white 0 0) padding-box exclude,
    linear-gradient(white 0 0);
}

@-webkit-keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@-webkit-keyframes load {
  0% {
    --tail: 0%;
    --nose: 0%;
  }
  40%,
  60% {
    --nose: 100%;
    --tail: 0%;
  }
  100% {
    --nose: 100%;
    --tail: 100%;
  }
}

@keyframes load {
  0% {
    --tail: 0%;
    --nose: 0%;
  }
  40%,
  60% {
    --nose: 100%;
    --tail: 0%;
  }
  100% {
    --nose: 100%;
    --tail: 100%;
  }
}

@supports (background: conic-gradient(from 95deg in hsl longer hue, red, red)) {
  .loader::after {
    background: conic-gradient(
        from 95deg in hsl longer hue,
        hsl(240deg 100% 75%),
        hsl(240deg 100% 75%)
      ) -5vmin -5vmin / calc(100% + 10vmin) calc(100% + 10vmin);
  }
}

4. To activate the loader, you need to trigger it when your website is loading content or performing background tasks. This can be done using JavaScript or other scripting methods.

// JavaScript to show the loader when content is loading
function showLoader() {
  document.querySelector('.loader').style.display = 'block';
}

// JavaScript to hide the loader when content is loaded
function hideLoader() {
  document.querySelector('.loader').style.display = 'none';
}

That’s all! hopefully, you have successfully created a loader animation. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X