1 min

November 24, 2023

Finding the best way to create animated gradient borders in CSS

Want to elevate your website design with eye-catching animated gradient borders? Let's explore four techniques to achieve this stunning effect in CSS.

Creating a gradient border and animating it in CSS can be tricky. Let's dive into some ways to do it together in this experiment.

The effect

[@portabletext/react] Unknown block type "iframe", specify a component for it in the `components.types` prop

The markup

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

The border-image property

For a border without radius needs, use border-image:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Using an extra element

To maintain radius with no transparency:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Using multiple backgrounds

You can combine backgrounds and use more background properties to achieve the same effect.

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Using masks

If you need both transparency and radius, CSS masking is the way.

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Animating a gradient border

We can use CSS Houdini to animate a gradient border.

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

For browsers that don't support Houdini, you can utilize the CSS @supports feature. This ensures users still receive the gradient, even if they can't view the animation.

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

The pros and cons of each technique

[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

The border image technique

No radius, but allows a transparent background and eliminates the need for an extra element, avoiding z-index issues.

Using an extra element

Provides a radius but lacks transparency and may pose potential z-index challenges.

Multiple backgrounds method

Gives us the radius but doesn't offer transparency; it bypasses z-index complexities.

CSS Masking

Offers both radius and transparency but presents a z-index drawback.

How to choose the best way to create a gradient border

I made the following flowchart to illustrate how you can choose the best option.

[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

If a radius isn't necessary, opt for the border-image technique. Need a radius? If transparency isn't required, use multiple backgrounds. For a transparent background with a radius, CSS masking is the solution.

The future is bright

Join the conversation initiated by Lea on GitHub regarding a potential CSS value for gradient borders.

The video and demo

Do you want to learn all about these techniques? You can watch me explain them on YouTube.

[@portabletext/react] Unknown block type "iframe", specify a component for it in the `components.types` prop

For a live demo and source code, check out our GitHub repository: Bejamas Experiments.

Get in touch