CSS Border Images

Create stunning decorative borders using images

🖼️ What are Border Images?

CSS border images allow you to use images as borders instead of simple solid colors. This creates decorative, artistic borders for your elements.


/* Basic border image */
.decorative-border {
    border: 20px solid transparent;
    border-image: url('border-pattern.png') 30 round;
    padding: 20px;
}
                                    

Border Image Properties

🎨

border-image-source

Specifies the image to use

border-image-source: url('image.png');
✂️

border-image-slice

How to slice the image

border-image-slice: 30;
📏

border-image-width

Width of the border image

border-image-width: 20px;
🔄

border-image-repeat

How to repeat the image

border-image-repeat: round;

🔹 Basic Border Image Setup

Border images replace an element's standard border with a custom image or CSS gradient, sliced and applied to the border area for decorative or complex frame designs. The basic setup involves border-image-source (to set the image/gradient) and border-image-slice to define how the source is divided. For example, border-image: url(frame.png) 30 fill / 30px / 30px stretch; uses a shorthand. This technique is perfect for creating unique buttons, cards, or UI panels with artistic edges that scale perfectly without distortion, offering a powerful alternative to multiple background images or complex HTML structures for ornate border effects.

/* Using an image as border */
.image-border {
    border: 15px solid transparent;
    border-image: url('../../../assets/images/border-pattern.png') 30 round;
    padding: 20px;
    background: white;
}

/* Using gradient as border image */
.gradient-border {
    border: 5px solid transparent;
    border-image: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1) 1;
    padding: 20px;
    background: white;
}

/* Using repeating pattern */
.pattern-border {
    border: 15px solid transparent;
    border-image: repeating-linear-gradient(
        45deg,
        #ff6b6b,
        #ff6b6b 10px,
        #4ecdc4 10px,
        #4ecdc4 20px
    ) 15;
    padding: 20px;
}

Output:

Image Border
Gradient Border

🔹 Border Image Slice

The border-image-slice property divides the source image into nine regions: four corners, four edges, and a central middle, controlling which parts are used for the border. Values (1-4 numbers or a percentage) define inward offsets from the image edges. The optional fill keyword makes the central region visible as a background. Mastering slicing is key to using border images effectively; incorrect values can stretch or distort the design. This property allows for the creation of versatile, resizable graphical borders from a single small image asset, improving site performance by reducing HTTP requests for complex frame graphics.

/* Understanding border-image-slice */
.slice-example {
    border: 20px solid transparent;
    /* slice: top right bottom left */
    border-image-slice: 30 30 30 30;
    border-image-source: url('../../../assets/images/border-pattern.png');
    border-image-repeat: stretch;
    padding: 15px;
}

/* Different slice values for varied border effects */
.asymmetric-slice {
    border: 15px solid transparent;
    border-image-slice: 10 30 10 30;
    border-image-source: url('../../../assets/images/border-pattern.png');
    border-image-repeat: round;
    padding: 15px;
}

Output:

Even Slice Border
Asymmetric Slice Border

🔹 Border Image Repeat

Border image repeat dictates how the edge portions of the sliced image are scaled or repeated to fill the border area, with values like stretch, repeat, round, and space. Stretch scales a single slice, repeat tiles it (which may clip), while round tiles and scales to fit evenly. Choosing the right repeat mode is crucial for achieving the desired aesthetic—whether a seamless pattern, a stretched gradient, or a scaled graphic. This control ensures that decorative borders look consistent across elements of different sizes, maintaining design integrity and visual appeal in responsive layouts.

/* Stretch - default behavior */
.stretch-border {
    border: 10px solid transparent;
    border-image: linear-gradient(45deg, #ff9a9e, #fecfef) 10;
    border-image-repeat: stretch;
}

/* Round - adjusts to fit whole number of tiles */
.round-border {
    border: 10px solid transparent;
    border-image: linear-gradient(45deg, #a8edea, #fed6e3) 10;
    border-image-repeat: round;
}

/* Repeat - tiles the image */
.repeat-border {
    border: 10px solid transparent;
    border-image: linear-gradient(45deg, #ffecd2, #fcb69f) 10;
    border-image-repeat: repeat;
}

/* Space - distributes space between tiles */
.space-border {
    border: 10px solid transparent;
    border-image: linear-gradient(45deg, #a8caba, #5d4e75) 10;
    border-image-repeat: space;
}

Output:

Stretch
Round

🔹 Creative Border Designs

Creative border designs leverage border-image, gradients, and pseudo-elements to build artistic and decorative frames without relying on embedded image files. You can create dashed lines with custom shapes, intricate corner ornaments, or animated glowing edges using gradients and the border-image property combined with animations. This approach keeps the design vector-based, scalable, and lightweight, contributing to faster page loads—a positive SEO signal. Such creative borders enhance visual identity, draw attention to calls-to-action, and improve the overall user experience by making interfaces more engaging and aesthetically unique across all device screens.

/* Neon glow border */
.neon-border {
    border: 3px solid transparent;
    border-image: linear-gradient(45deg, #00f5ff, #ff00ff, #00f5ff) 1;
    box-shadow: 
        0 0 10px rgba(0, 245, 255, 0.5),
        0 0 20px rgba(255, 0, 255, 0.3);
    padding: 20px;
    background: #1a1a1a;
    color: white;
}

/* Rainbow border */
.rainbow-border {
    border: 8px solid transparent;
    border-image: linear-gradient(
        45deg,
        #ff0000, #ff8000, #ffff00, #80ff00,
        #00ff00, #00ff80, #00ffff, #0080ff,
        #0000ff, #8000ff, #ff00ff, #ff0080
    ) 8;
    padding: 20px;
}

/* Animated border */
.animated-border {
    border: 4px solid transparent;
    border-image: linear-gradient(45deg, #ff6b6b, #4ecdc4) 1;
    animation: borderAnimation 3s linear infinite;
    padding: 20px;
}

@keyframes borderAnimation {
    0% { border-image-source: linear-gradient(45deg, #ff6b6b, #4ecdc4); }
    50% { border-image-source: linear-gradient(45deg, #4ecdc4, #45b7d1); }
    100% { border-image-source: linear-gradient(45deg, #45b7d1, #ff6b6b); }
}

Output:

Neon Border
Rainbow Border

🔹 Practical Examples

Real-world applications of border images include creating custom-styled buttons, decorative photo frames, unique notification badges, or stylized data cards for dashboards. For instance, an e-commerce site might use a dotted "sale" border, or a portfolio could feature a polaroid-style image frame. These implementations enhance UI components with distinctive branding while maintaining accessibility and responsiveness. Using CSS for these effects, rather than static images, ensures sharp rendering on high-DPI screens, reduces server requests, and simplifies theme customization, leading to better site performance and an improved developer experience in maintaining the codebase.

/* Card with decorative border */
.decorative-card {
    border: 12px solid transparent;
    border-image: linear-gradient(135deg, #667eea 0%, #764ba2 100%) 12;
    background: white;
    padding: 24px;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

/* Button with gradient border */
.gradient-button {
    border: 2px solid transparent;
    border-image: linear-gradient(45deg, #ff6b6b, #4ecdc4) 1;
    background: white;
    padding: 12px 24px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.gradient-button:hover {
    background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
    color: white;
}

/* Image frame */
.image-frame {
    border: 20px solid transparent;
    border-image: linear-gradient(45deg, #f093fb, #f5576c, #4facfe, #00f2fe) 20;
    display: inline-block;
}

Output:

Decorative Card

Beautiful border image card design

🧠 Test Your Knowledge

Which property controls how border images are repeated?