Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can build a rainbow infinity symbol with just one HTML element: two pseudo-elements form its halves, conic gradients supply the color, and masks cut the loops into a ribbon shape. To animate the colors, rotate a registered CSS custom property.

How the one-element infinity works

The markup can be a single empty element, such as <div class="infinity"></div>. Its ::before and ::after pseudo-elements create the two visible halves. Each pseudo-element is a circle with border-radius: 50%; flex layout places the circles side by side.

A conic-gradient() paints each circle with a rainbow sweep. The two gradients start at opposite angles—90 degrees and 270 degrees—so their colors can align where the loops cross. Masks remove the parts of each circle that should not appear, leaving the curved ribbon segments that make up the infinity shape.

This approach uses gradient backgrounds and masks rather than border-image: the latter does not combine reliably with border-radius for this shape. The CSS-Tricks demonstration shows the technique and its one-element markup: 1 Element CSS Rainbow Gradient Infinity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Build the shape in these steps

  1. Add one empty element. Give it a class for styling; the symbol itself does not need child elements.
  2. Create two circular pseudo-elements. Set equal dimensions, apply border-radius: 50%, and use the parent’s flex layout to position them side by side.
  3. Paint the circles with conic gradients. Use mirrored starting angles—90 degrees for one half and 270 degrees for the other—to orient the color sweeps toward the crossover.
  4. Apply masks to cut the ribbon. Combine radial or other mask shapes to hide the center and outer portions that would otherwise leave complete discs. Adjust the cutouts until the visible segments read as an infinity loop.
  5. Check the crossover. Make sure the two halves meet cleanly and that the color transition does not leave an obvious seam.

Animate the rainbow rotation

For moving color, animate an angle custom property with linear keyframes, then use that angle in the conic gradients. Registering the custom property lets the browser interpolate its value smoothly; the related demos use this pattern alongside conic gradients and masks. See Animated Rainbow Gradient Border and Animated CSS Mask.

Keep the rotation separate from the geometry: the masks define the infinity ribbon, while the animated angle changes the gradient’s orientation. This makes it easier to tune the crossover and shape without changing the animation logic.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What to check when adapting the effect

  • Element count: the markup stays at one element, while pseudo-elements provide the two halves.
  • Shape technique: masks make the ribbon cutouts; changing to a clip path is a different implementation choice and may require retuning the geometry.
  • Motion: a static gradient is simpler; rotating an animated custom property adds continuous color movement.
  • Browser behavior: verify support for registered custom properties and the mask-compositing behavior your CSS uses in the browsers you need to support.
  • Seam quality: inspect the crossover at the final rendered size, since alignment depends on both gradient angles and the mask geometry.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.