CSS Border Images
Normally a border is a single colour in a style like solid or dashed. The border-image property swaps that for an image. The browser slices the image into nine pieces and uses them to paint the corners and edges. The source can be a real image file, an SVG, or a CSS gradient.
How slicing works
The border-image-slice value draws four cut lines into the source image, measured inward from the top, right, bottom and left edges. That divides it into nine regions:
+------+---------------+------+
|corner| top edge |corner|
+------+---------------+------+
| left | middle |right |
| edge | (dropped | edge |
| | unless fill) | |
+------+---------------+------+
|corner| bottom edge |corner|
+------+---------------+------+The corners are drawn as they are. The edges are stretched or repeated to fill the length of each side.
The properties
| Property | Purpose | Example |
|---|---|---|
border-image-source |
The image (url() or a gradient) |
linear-gradient(red, blue) |
border-image-slice |
Where to cut; numbers mean image pixels, % means percentage of the image. Add fill to keep the middle |
30, 25% fill |
border-image-width |
How thick to draw the image border | 20px |
border-image-outset |
How far the image extends beyond the border box | 10px |
border-image-repeat |
How edges fill: stretch, repeat, round, space |
round |
border-image |
Shorthand: source slice / width / outset repeat |
url(x.svg) 30 round |
Note: The element still needs a
border(a width plus any style,solidwill do).border-imagereplaces how the border is painted, but the border width still controls the space it takes up.
Gradient borders
A gradient is the simplest image source. Slicing at 1 uses the gradient across the whole border.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 20px; padding: 16px; }
.box { width: 140px; padding: 20px; text-align: center; border: 10px solid; }
.a { border-image: linear-gradient(45deg, #ff6b6b, #ffd93d, #6bcb77, #4d96ff) 1; }
.b { border-image: linear-gradient(to bottom, #8e2de2, #4a00e0) 1; }
.c { border-width: 4px 0; border-image: linear-gradient(90deg, transparent, #e91e63, transparent) 1; }
.d { border-image: repeating-linear-gradient(45deg, #222 0 10px, #ffeb3b 10px 20px) 10; }
</style>
</head>
<body>
<div class="box a">Rainbow</div>
<div class="box b">Purple fade</div>
<div class="box c">Fading lines</div>
<div class="box d">Caution tape</div>
</body>
</html>Tip:
border-imageignoresborder-radius. For a rounded gradient border, layer two backgrounds instead:background: linear-gradient(white, white) padding-box, linear-gradient(45deg, red, blue) border-box; border: 4px solid transparent;
Slicing an SVG image
Here the source is a 90×90 SVG with a coloured circle in each corner. A slice of 30 cuts 30px from every edge. The corners keep the circles, and the edges repeat the pattern between them.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 20px; padding: 10px; }
.frame {
width: 140px; padding: 14px; text-align: center;
border: 24px solid;
border-image-source: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='90' height='90'%3E%3Crect width='90' height='90' fill='%23fff3e0'/%3E%3Crect x='30' y='0' width='30' height='90' fill='%23ffb74d'/%3E%3Crect x='0' y='30' width='90' height='30' fill='%23ffb74d'/%3E%3Ccircle cx='15' cy='15' r='12' fill='%23e65100'/%3E%3Ccircle cx='75' cy='15' r='12' fill='%23e65100'/%3E%3Ccircle cx='15' cy='75' r='12' fill='%23e65100'/%3E%3Ccircle cx='75' cy='75' r='12' fill='%23e65100'/%3E%3C/svg%3E");
border-image-slice: 30;
}
.stretch { border-image-repeat: stretch; }
.round { border-image-repeat: round; }
.space { border-image-repeat: space; }
</style>
</head>
<body>
<div class="frame stretch">stretch</div>
<div class="frame round">round</div>
<div class="frame space">space</div>
</body>
</html>border-image-repeat |
Edge behaviour |
|---|---|
stretch (default) |
One copy, stretched to fit |
repeat |
Tiled, so tiles may be cut off at the ends |
round |
Tiled and scaled so a whole number fits |
space |
Whole tiles only, with the leftover space spread between them |
Width, outset and fill
border-image-width can draw the image thicker or thinner than the actual border. border-image-outset pushes it outward without changing layout. The fill keyword keeps the middle slice and paints it behind the content.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 40px; padding: 30px; }
.g { width: 120px; padding: 16px; text-align: center; border: 12px solid;
border-image: linear-gradient(135deg, #00c9ff, #92fe9d) 12; }
.out { border-image-outset: 12px; }
.fill { border-image: linear-gradient(135deg, #00c9ff, #92fe9d) 12 fill; color: #004d40; font-weight: bold; }
</style>
</head>
<body>
<div class="g">normal</div>
<div class="g out">outset 12px</div>
<div class="g fill">fill</div>
</body>
</html>Test yourself
Into how many regions does border-image-slice divide the source image?
- 4
- 6
- 8
- 9
Which border-image-repeat value tiles the edge image and scales it so a whole number of tiles fits?
- stretch
- repeat
- round
- space
What is a known limitation of border-image?
- It only works with PNG files
- It ignores border-radius
- It cannot use gradients
- It removes padding