日本語

Drawing Gradient Borders with CSS border-image

​​CSS​Playwright

You can draw borders with gradients or images using CSS border-image. For a gradient border, set the width and line style with border, then write the gradient and 1 in border-image. If you forget the final 1, color appears only at the four corners.

Note

I tested on Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened test pages with Playwright and checked the results by reading pixel colors from screenshots. I did not test Firefox or Safari. The browser versions listed for each browser come from the compatibility data (@mdn/browser-compat-data 8.1.8).

Two boxes stacked vertically. The top box, labeled in Japanese with the meaning "slice not written (100%)", has a light blue to pink gradient only in the four small squares at the corners, and the sides have no color. The bottom box, labeled in Japanese with the meaning "slice set to 1", has a gradient border on all four sides.
Top: slice not written. Bottom: slice set to 1.

Write a gradient border with border and border-image

To make a border that changes from light blue to pink from left to right, write the following.

css
.box {
  border: 10px solid #9aa5ab;
  border-image: linear-gradient(90deg, #00c8e8, #d85878) 1;
}

border-image is a shorthand for these five properties:

Property What it sets Initial value
border-image-source The image or gradient to use none
border-image-slice Where the image is cut into nine parts 100%
border-image-width The thickness of the border image 1 (1 times border-width)
border-image-outset How far the border image extends outside the border 0
border-image-repeat Whether the side images are stretched or tiled stretch

The 1 in the example above is the value of border-image-slice. While border-image is drawn, the color from border (#9aa5ab) is not shown. According to MDN, when the image cannot be displayed, or when border-image-source is none, the border drawn with border-style is shown instead.

Without 1, color appears only at the corners

border-image-slice cuts the image with four lines, each drawn inward from the edge. This divides the image into four corners, four sides, and a center, nine parts in total (CSS Backgrounds and Borders Level 3, the border-image-slice section). The corner parts are used for the corners of the border, and the side parts are used for the top, bottom, left, and right edges. The center is not drawn unless you write fill.

The initial value is 100%. The specification says that when the left and right slice widths add up to the image width or more, the top and bottom sides and the center become empty. The value 100% produces exactly this state.

I measured colors in the test environment with slice not written and with 1 written. The box had box-sizing: border-box, a width of 200px, a height of 100px, a border width of 10px, and a white background.

Measured point slice not written slice set to 1
Top-left corner #20b7d7 (colored) #00c8e8
Center of the top side #ffffff (white background) #6d90b0
Center of the left side #ffffff #01c8e8
Center of the right side #ffffff #d85979

Without slice, the sides stayed the white background, and the gray from border was not drawn either. With 1, the image is cut at 1 from the edge (1px), so there is a part to use for the sides.

Adding fill, as in border-image: linear-gradient(90deg, #00c8e8, #d85878) 1 fill;, also changed the center of the box to #6c8faf, and the gradient was drawn over the background behind the content.

The gradient direction decides which sides change

When slice is 1, the top and bottom borders use thin strips from the top and bottom edges of the image, and the left and right borders use thin strips from the left and right edges. Each strip is stretched to fill its side. For this reason, the gradient direction decides which sides show the change in color.

With a gradient from left to right (90deg), the top and bottom sides change color, while the left side was a single light blue color, #01c8e8, and the right side was a single pink color, #d85979. With a gradient from top to bottom (180deg), the top side was a single light blue color, #01c7e7, and the bottom side was a single pink color, #d75979. The centers of the left and right sides were the intermediate color #6e90b0.

Without border-style, nothing is drawn

If you write only border-image, no border is drawn. In the test environment, I wrote border-width: 10px and border-image without border-style. The value of border-top-width was 0px, and no color appeared anywhere on the border.

The specification's border-style section says that when border-style: none is set, the border width becomes 0. The initial value of border-image-width, which is 1 times the border width, also becomes 0. MDN also recommends specifying border-style as a fallback in case the image fails to load.

Be careful with the border shorthand too. As stated in the specification's border shorthand section, border resets border-image to its initial value.

css
/* border-image is lost */
.box {
  border-image: linear-gradient(90deg, #00c8e8, #d85878) 1;
  border: 10px solid #9aa5ab;
}

When written in this order, the value of border-image-source became none, and the border was the single gray color from border. Write border first, then border-image.

border-image does not round corners, so stack two backgrounds

A border drawn with border-image does not get rounded corners, even if you set border-radius. The specification's Corner Clipping section states that border-image is not affected by border-radius.

Two boxes stacked vertically. The top box, labeled in Japanese with the meaning "border-image and border-radius: 20px", has a gradient border whose corners stay square. The bottom box, labeled in Japanese with the meaning "stack two backgrounds", has the same gradient border with rounded corners.
Top: border-radius added to border-image. Bottom: two stacked backgrounds.

In the test environment, even after I added border-radius: 20px, the top-left corner of the box (1px from the top-left) was the light blue #00c8e8, and the corner was not cut away.

To get a rounded gradient border, do not use border-image. Stack two backgrounds instead.

css
.box {
  border: 10px solid transparent;
  border-radius: 20px;
  background:
    linear-gradient(#fff, #fff) padding-box,
    linear-gradient(90deg, #00c8e8, #d85878) border-box;
}

In background, the backgrounds listed first are drawn on top. The top layer is white. With padding-box, the area where the background is painted (background-clip) is limited to the inside of the border. The bottom layer is the gradient, and border-box makes it extend under the border. Because the border is transparent, the gradient below shows through in the border area. In the test environment, the top-left corner showed the page's gray background (#cccccc), so the corner was rounded. The center of the top side was #6d90b0, the same color as the border drawn with border-image.

With this approach, the background inside the box is the color of the single top layer (white in this example).

Drawing only the bottom side gives a gradient underline

If you set border-width so that only the bottom has thickness, you can draw a gradient line on the bottom side alone.

css
.heading {
  border-style: solid;
  border-width: 0 0 4px;
  border-image: linear-gradient(90deg, #00c8e8, #d85878) 1;
}

In the test environment, the left end of the bottom side was #03c6e6, the center was #6c8faf, and the right end was #d55a7a. The top side had no color.

Make the border image thicker or move it outward

border-image-width and border-image-outset change only where the border image is drawn. As stated at the start of the specification's Border Images chapter, the size of the box and the placement of its contents are determined only by border-width and border-style.

Inside border-image, write the width after slice, separated by /, and then write outset, separated by another /.

css
/* Set border-image-width to 30px */
.wide {
  border: 10px solid #9aa5ab;
  border-image: linear-gradient(90deg, #00c8e8, #d85878) 1 / 30px;
}

/* Set border-image-outset to 10px (width keeps its initial value of 1) */
.outset {
  border: 10px solid #9aa5ab;
  border-image: linear-gradient(90deg, #00c8e8, #d85878) 1 / 1 / 10px;
}

In the test environment, I kept border-width at 10px and set border-image-width to 30px. The top side was colored at 25px from the edge, and at 35px the color was the white background. The box width (offsetWidth) stayed at 200px, and the content width (clientWidth) stayed at 180px. The extra thickness of the image overlapped the content side.

With border-image-outset set to 10px, color appeared 5px outside the left edge of the box. The area 5px inside the edge, which is where the original border was, showed the white background. The specification says that border-image-outset extends the area where the border image is drawn outward from the box. The size of the box does not change. The position of a paragraph placed after the box was the same as without outset.

When tiling images, choose the stretching mode with border-image-repeat

Using an image lets you make a border with repeated decorations. For the test, I prepared a 90×90 SVG. It has a pink square in each 30px corner, and a light blue circle 20px in diameter at the center of each side tile.

css
.frame {
  border: 30px solid #9aa5ab;
  border-image: url("frame.svg") 30 round;
}

The 30 means the image is cut 30 units from the edge. These units are the SVG's internal coordinates, and the value matches the size of the corner square. In the test, I specified this SVG as a data: URL.

The original image and four boxes stacked vertically. The original image has a pink square in each corner and one light blue circle at the middle of each side. In the stretch box, the top and bottom sides each have one circle stretched wide. In the repeat box, the top and bottom sides have seven circles, and the circles at both ends are cut off where they meet the corners. In the round box, six circles are slightly widened across the side. In the space box, six circles in their original shape are placed with gaps between them.
The original image and the four values of border-image-repeat compared

In a box 250px wide, I scanned the top side horizontally (190px excluding the corners) and measured the ranges where circles were drawn.

Value Circles on the top side
stretch (initial value) One circle, stretched into a 128px-wide shape
repeat Tiled from the center. Circles at both ends are cut off where they meet the corners
round Six circles. The tiles are stretched, and each circle is 22–23px wide
space Six circles. Each is 21–22px wide, with gaps between them

With repeat, the center circle was at 115px, a 20px span, and copies were placed every 30px to the left and right. The circles at the ends were cut off at the edges of the side, and only the parts that connect to the corner squares were visible. round changes the number of tiles that fit in 190px (6.33) to the integer 6, and then stretches the tiles slightly. space places the six tiles at their original size and divides the leftover width into gaps. The widths of the round and space circles were measured by counting every pixel that was not close to white, so they include the faint edge colors of the circles.

The behavior of each value is described in MDN's border-image-repeat and in the specification's Drawing the Border Image section. The fact that repeat tiles from the center is described in the specification's procedure. If cut-off decorations bother you, choose round or space.

Supported browsers

According to the compatibility data, border-image is supported in Chrome 16, Edge 12, Firefox 15, Safari 6, Safari on iOS 6, and Chrome for Android 18. Gradient values are supported in Firefox from version 29. The space value of border-image-repeat is supported in Chrome 56, Edge 12, Firefox 50, and Safari 9.1.

​Posted by 86