惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
美团技术团队
博客园 - 聂微东
博客园 - 叶小钗
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
Spread Privacy
Spread Privacy
J
Java Code Geeks
雷峰网
雷峰网
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
量子位
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
罗磊的独立博客
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Project Zero
Project Zero
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
GbyAI
GbyAI
博客园 - 【当耐特】
O
OpenAI News
Schneier on Security
Schneier on Security
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
博客园 - 司徒正美

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient
Build your CSS loader with only one div — The Dots
Temani Afif · 2021-09-15 · via CSS Articles by Temani Afif
Cover image for Build your CSS loader with only one div — The Dots

Temani Afif

Temani Afif

Posted on • Edited on

Yes, another post about CSS loaders but this time you will build the loader! In this article, I will share with you a few tricks I used to build some of my loaders.

In case you have missed it, I created a collection of 500+ single div loaders where you will find 50 loading dots. We will see the magic behind some of them!

There are a lot of techniques to create loading dots. I will focus on my favorite one which is radial-gradient and background animation.

Well, another boring tutorial where we will have a ton of code, complex @keyframes, and insane animation delays, right?

No, at all. In this tutorial you will have:

✔️ One div
✔️ One @keyframes
✔️ No delay and No complex time calculation
✔️ We will not make one animation but many of them using the same code structure
✔️ Not boring because this article will end before you even have time to blink


1) Building the dot

We first create one dot using the following code (demo link):

.dot {
  background:radial-gradient(farthest-side,currentColor 90%,red);
  background-size:30px 30px;
  background-position:top center;
  background-repeat:no-repeat;
}

CSS one dot

The logic is simple: I create a background layer having a size equal to 30x30 placed at the top center and I disable the repetition to have only one instance. That layer is a radial-gradient() having two colors, the currentColor which is the color you define within the color property (useful to update the dot coloration later), and a transparent color (I am using red here to see the limit of the gradient layer).

farthest-side allows me to consider the farthest side for my percentage reference and since we are dealing with a square gradient (30x30), all the sides are the farthest one so by using currentColor 100%, I draw a circle that touches the sides. no complex value like 66% or 72.5%.

But you are using 90%, not 100%!

True and this is because gradients aren't anti-aliasing so we have to consider a smaller value to have a smooth edge.

Below is a difference between 90% and 100%

smooth edge vs jagged edges

That's all! Now, we have our dot so let's animate it.


2) Animating the dot

To animate the dot we simply animate the background-position. Let's create a @keyframes that update the position from top center to bottom center

Our code will become (demo link):

.dot {
  background:radial-gradient(farthest-side,currentColor 90%,#0000);
  background-size:30px 30px;
  background-position:top center;
  background-repeat:no-repeat;
  animation:m 1s infinite alternate;
}
@keyframes m{
  to {background-position:bottom center}
}

I think the above is self-explanatory

We can also transform the code like the one below. It will make things easier for the next steps.

.dot {
  background:radial-gradient(farthest-side,currentColor 90%,#0000);
  background-size:30px 30px;
  background-repeat:no-repeat;
  animation:m 1s infinite alternate;
}
@keyframes m{
  0%   {background-position:top    center}
  100% {background-position:bottom center}
}

Now you can animate one dot. All you have to do is to update the positions to whatever you want.

For anyone not familiar with background-position, I highly recommend reading my previous article and more precisely the section talking about the percentage values.

Later, I will consider percentage values instead of keywords like top center (which is equivalent to 50% 0). In my previous article, I am giving all the equivalence.


3) Adding more dots

To add more dots we simply add more divs placed next to each other. Then each one will have a background layer and its own @keyframes animation and...

noooo

Of course, NO!

To add more dots we simply add more background layers. We can have as many layers as we want so one div can hold a lot of dots!

Let's try with 2 (demo link):

.dot {
  background:
    radial-gradient(farthest-side,currentColor 90%,#0000),
    radial-gradient(farthest-side,currentColor 90%,#0000);
  background-size:30px 30px;
  background-repeat:no-repeat;
  animation:m 1s infinite alternate;
}
@keyframes m{
  0%  {background-position:top    left,bottom right}
  100%{background-position:bottom left,top    right}
}

One dot will animate from top left to bottom left and another one from bottom right to top right. As simple as that!

Notice that I am keeping only one background-size because all my dots will have the same size.

Let's optimize the code using CSS variable and percentage values (demo link):

.dot {
  --d:radial-gradient(farthest-side,currentColor 90%,#0000);
  background:var(--d),var(--d);
  background-size:30px 30px;
  background-repeat:no-repeat;
  animation:m 1s infinite alternate;
}
@keyframes m{
  0%  {background-position:0 0   ,100% 100%}
  100%{background-position:0 100%,100% 0   }
}

The variable --d will avoid the repetition of the same gradient syntax. For the percentage values, it's more a preference than an optimization. It is easier for me to deal with values than keywords.

Did you already blink?

No problem because we are almost done. We have all the ingredients to build any kind of loading dots.


4) Creating the loader

To build the loader you will first need a pen & paper. Yes, I am not joking. People tend to jump into their favorite Code Editor and start writing code trying to imagine in their head how things should work. No, no, it won't work that way.

You put everything on a paper and then you translate it into a code.

Step 1: define your structure

In this step, we decide about the number of dots, their size, the gaps, etc.

So let's say I will be using 4 dots having a size of 20x20 and a gap of 5px. This will give me a total width of 95px. I don't want them to jump a lot so let's have a height of 40px

Dot structure

Step 2: write the code

We translate what we defined in (1) into a code:

.dot {
  width:95px;
  height:40px;
  --d:radial-gradient(farthest-side,currentColor 90%,#0000);
  background:var(--d),var(--d),var(--d),var(--d);
  background-size:20px 20px;
  background-repeat:no-repeat;
}

Don't worry about the result you get so far. You will see only one dot because all of them are above each other. We didn't define any background-position yet.

Step 3: build your timeline

In this step, you need to use your imagination to define each frame of your full animation. Draw a step-by-step illustration of the animation you have in mind.

Here is an example where my animation will move all the dots from the bottom to the top one by one then move them back to the bottom.

Timeline dots

I have 9 frames and it's important to note that the last one is the same as the first one to have a continuous animation.

Step 4: translate the timeline into background-positions

Now that you have your drawing, simply define the background-position for each frame. For each dot, we have a fixed X coordinate and only the Y will change.

For X, we have the following values: 0%, 33%, 66%, 100%. Not very intuitive so let's use another syntax: calc(0*100%/3), calc(1*100%/3), calc(2*100%/3), calc(3*100%/3). The logic is easy: N dots so we loop from 0 to N-1 and we divide by N-1.

The code for our animation will be:

@keyframes m {
  ??%{background-position:calc(0*100%/3) 100%, calc(1*100%/3) 100%, calc(2*100%/3) 100%, calc(3*100%/3) 100%}
  ??%{background-position:calc(0*100%/3) 0   , calc(1*100%/3) 100%, calc(2*100%/3) 100%, calc(3*100%/3) 100%}
  ??%{background-position:calc(0*100%/3) 0   , calc(1*100%/3) 0   , calc(2*100%/3) 100%, calc(3*100%/3) 100%}
  ??%{background-position:calc(0*100%/3) 0   , calc(1*100%/3) 0   , calc(2*100%/3) 0   , calc(3*100%/3) 100%}
  ??%{background-position:calc(0*100%/3) 0   , calc(1*100%/3) 0   , calc(2*100%/3) 0   , calc(3*100%/3) 0   }
  ??%{background-position:calc(0*100%/3) 100%, calc(1*100%/3) 0   , calc(2*100%/3) 0   , calc(3*100%/3) 0   }
  ??%{background-position:calc(0*100%/3) 100%, calc(1*100%/3) 100%, calc(2*100%/3) 0   , calc(3*100%/3) 0   }
  ??%{background-position:calc(0*100%/3) 100%, calc(1*100%/3) 100%, calc(2*100%/3) 100%, calc(3*100%/3) 0   }
  ??%{background-position:calc(0*100%/3) 100%, calc(1*100%/3) 100%, calc(2*100%/3) 100%, calc(3*100%/3) 100%}
}

It may look complex at first glance but it's very simple. Inside each frame, I define the position of each dot. The X is always the same and I update the Y from 0 (top) to 100% (bottom).

We have our @keyframes!

Step 5: find the percentage of the keyframes

The last step is to fill the ?? with percentage values. We have 9 frames so we consider 8 (always N-1) and we divide 100% by 8 to get 12.5%. We start for 0% and we increment by 12.5% until we reach 100%.

You are done!

Now put everything together and enjoy:

The only limit to this technique is your imagination. If you start playing with it you will find yourself creating a lot of fancy dot loaders.


You want more examples?

Ok, let's do two more.

CSS loding dots

This time I will consider 3 dots (always the same sizing) and the movement of each one is center -> top -> bottom -> center.

Notice the arrow in my drawing. It means that I want an alternate animation.

CSS loading dots

A 4 dots loader where I will update the X coordinate as well.

I think everyone got the idea 😉


Homework time!

Now, it's your turn to build a CSS loader, or even better a full collection of loaders! Share your creation in the comment section. 👇

I will highlight the best ones in my next article and will share them on Twitter.

Waiting for your CSS loaders!

loading your loader


Conclusion & self-promotion

The above is not the only technique I am using to build loading dots. There are more than this but putting everything into one article will make it hard to understand.

If you want a follow up to this article with more tricks don't forget to ❤🦄

You can also support me on patreon

Become a patron

For only 3€/month, you encourage me to provide more content
For 8€/month, you can become a better developer because you will have access to a private chat where you can ask me any CSS-related question at any time. I may also give you some of my secret tricks 🤫
For 20€/month, I can help you with any project you are working on!

You can also buy me a coffee

buy me a coffee

Thank you