CSS Border Images

The `border-image` property in CSS allows you to use an image as the border of an element. This can create intricate and visually appealing borders that go beyond simple solid colors or patterns.

Understanding `border-image`

The `border-image` property is a shorthand for specifying border images. It consists of several sub-properties: `border-image-source`, `border-image-slice`, `border-image-width`, and `border-image-repeat`.

Syntax

The syntax for the `border-image` property is:


/* Border Image Syntax */
border-image:    ;

        
  • Source: Defines the image to use as the border. This can be a URL or an image from a data URI. Example: url('border-image.png')
  • Slice: Specifies how the image should be sliced. It determines which parts of the image are used for the border. Example: 30%
  • Width: Sets the width of the border image. This can be in pixels, percentages, or keywords like auto. Example: 10px
  • Repeat: Defines how the border image should be repeated. Common values are stretch, repeat, and round. Example: repeat

Examples

Example 1: Basic Border Image


/* Basic Border Image */
.border-image-basic {
    border: 10px solid transparent;
    border-image: url('border-image.png') 30 stretch;
}

            
This element has a basic border image.

Example 2: Border Image Slice


/* Border Image Slice */
.border-image-slice {
    border: 15px solid transparent;
    border-image: url('border-image-slice.png') 20% 20% 30% 30% repeat;
}

            
This element has a border image with different slices and repeats.

Example 3: Border Image Repeat


/* Border Image Repeat */
.border-image-repeat {
    border: 20px solid transparent;
    border-image: url('border-image-repeat.png') 30 repeat;
}

            
This element has a border image with repeating pattern.