Brifing

Animating CSS Border Images

Bring your borders to life with animated effects

CSS-Tricks2 min

1Basic usage of border-image

The CSS border-image property allows using images or gradients instead of standard border styles. It enables creative solutions in interface design. Andy Clarke's article demonstrates its potential. We start by defining a gradient with border-image-source. The border-image-slice property controls how the image is sliced. Border-image-width determines the physical thickness of the border. This creates visually interesting borders beyond basic lines.

2Preparation for animation

Animating border images requires defining a custom CSS property (@property). This enables animation of gradient start and end points. We create a --p variable with percentage type. On hover, we can animate this variable from 0% to 100%. This creates a drawing effect where the border appears to draw itself. The technique works with both images and CSS gradients. It's particularly effective for interactive UI elements.

3Linear gradient implementation

Starting with a simple linear-gradient, we can create an animated red border. Border-image-outset pushes the border away from the element. When both red and transparent colors start at 0%, the border is initially invisible. During animation, the --p variable increases, expanding the red portion. Using border-image-slice:1 ensures smooth gradient application across the entire border. This creates a clean, animated border effect.

4Conic gradient variation

Using conic-gradient instead of linear creates a circular animation effect. We use border-image-repeat: round for proper tiling of slices. A second custom variable --a controls the conic gradient's angle. This produces a sharp, expanding color wheel effect. During animation, slices slightly stretch to maintain the round pattern. The result is a more dynamic, eye-catching border animation.

5Performance advantages

The border-image approach is more efficient than other border animation methods. It works automatically for all borders, ensuring consistency. Compared to CSS masks, it requires less computation. A single image slice can be used across all borders. This provides significant performance benefits in complex interfaces. It also takes advantage of browser optimizations effectively.

6Limitations and solutions

A major limitation is border-image's inability to follow border shapes. This causes issues with rounded corners. Using border-image-outset can mitigate this problem. Adjusting animation speed and direction also helps. Temani Afif's CSS mask approach offers an alternative. For curved borders, SVG-based solutions might be more appropriate. Each method has its ideal use cases.

7Practical implementation example

We create a card element with 150px width and 0.69 aspect ratio. Adding a background image and border animation completes the effect. Text content is positioned to avoid overlap with background-image. On hover, the --p variable animates to 100%. The example uses a Batman-themed card but works with any content. This technique is ideal for portfolio items or interactive buttons. It combines visual appeal with clean implementation.

8Creative applications

Border animations are powerful tools for highlighting user interaction. They work well for loading indicators, interactive navigation, or emphasized elements. Different gradient types (linear, radial, conic) create varied effects. Adjusting animation speed and direction enables unique experiences. This technique offers maximum visual impact with minimal effort. It's a performance-friendly solution aligned with modern web design trends.

Original article: CSS-Tricks — CSS Border Görsellerini Animasyonlu Hale Getirme