CSS Border Images
The border-image CSS property draws an image around a given element. It replaces the element's regular border.
The border-image property is a shorthand property for setting the following individual border properties:
border-image-sourceborder-image-sliceborder-image-widthborder-image-outsetborder-image-repeat
As an example:
Editor
In this example:
- The
borderproperty is set to20px solid transparentto create a solid border with a width of20pixels. - The
border-imageproperty is then used to specify the image to use for the border,url(border.png), and how it should be sliced and repeated. - The
30value in theborder-imageproperty indicates how much of the image to use for the corners of the border. - In this case,
30pixels of the image will be used for each corner. - The
roundvalue indicates how to repeat the image to fill the border.
You can also use other values for the border-image property, such as stretch to stretch the image to fill the border, repeat to repeat the image to fill the border, or round stretch to repeat and scale the image to fill the border.
As an example:
.box {
border: 20px solid transparent;
border-image: url(border.png) 30 repeat;
}
In the above example:
- The
repeatvalue is used to repeat the image to fill the border.
Border images may not be supported in all browsers, and they may also affect the performance of your webpage if the image is large and needs to be loaded.
CSS border-image - Different Slice Values
When using the border-image property in CSS, the slice value is used to divide the image into nine sections that will be used to create the border. The slice value can be a single value, four values, or nine values, depending on how you want to slice the image.
If you use different slice values for each side of the border, it can result in a non-uniform border.
As an example:
border-image-source: url(border-image.png);
border-image-slice: 30% 50% 20% 70%;
border-image-width: 20px;
border-image-repeat: round;
In this example:
- We're using a border image named
border-image.png, and setting differentslicevalues for each side of the border. - The
border-image-widthproperty is set to20pxto specify the width of the border, andborder-image-repeatis set toroundto ensure that the image is stretched to fill the border.