Master single-element CSS art by creating a classic spinning Yin-Yang symbol. This tutorial breaks down how to use gradients, pseudo-elements, and CSS keyframes to create complex geometry without images.
hibuno
••2 min read•
#css#animation#yin yang#pseudo-elements#gradients
Preview
Thanks for reading!
If you found this article helpful, maybe you could help share it.
In this tutorial, we will create a spinning Yin-Yang symbol. While this may look like a complex graphic, it can be constructed entirely using CSS and a single HTML element. This is a perfect exercise for understanding pseudo-elements (::before and ::after) and CSS gradients.
The Logic
To build a Yin-Yang without images, we need to think about its geometry:
The Base: A main circle split into two colors (black and white).
The Swirls: Two smaller circles placed on the centerline. One matches the top color but has a contrasting dot; the other matches the bottom color.
1. The HTML
We only need a single container. We will use a div with the class yin-yang.
<div class="balance"></div>
2. The CSS
First, we style the main body. Instead of using complex border hacks, we use a linear-gradient to split the background perfectly in half.
Now we use ::before and ::after to create the two inner circles. We use a border trick here: the background-color creates the small inner dot, while the border creates the swirling part of the circle.
/* The top white/black swirl with a black dot */
.balance::before,
.balance::after {
content: "";
position: absolute;
top: 50%;
left: 0;
width: 12.5px; /* The size of the inner dot */
height: 12.5px;
background-color: white;
border: 18.8px solid black; /* The border creates the black head */
border-radius: 100%;
box-sizing: content-box;
}
.balance::after {
left: 50%;
background-color: black;
border-color: white; /* The border creates the white head */
}
4. The Animation
Finally, we define the keyframes to make the symbol rotate continuously.
By combining a linear gradient on the parent with centered pseudo-elements using thick borders, we achieved a scalable, resolution-independent Yin-Yang animation with zero JavaScript.
Building a Yin-Yang Loading Animation with CSS | Hibuno