- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Border Images
In CSS, the border-image property allows you to use an image as a border around an element. This property is quite powerful, enabling you to create visually appealing and unique border styles that go beyond the basic solid, dashed, or dotted lines. Instead of relying on simple strokes, you can use intricate patterns, hand-drawn textures, or even CSS gradients to frame your content.
Syntax
The border-image property is a shorthand property that combines the following properties into a single line. While you can define them individually, the shorthand is the most common way to write it in modern production code.
- border-image-source: The path to your image.
- border-image-slice: How to cut the image into pieces.
- border-image-width: The visual thickness of the border.
- border-image-outset: How far the border sits outside the element's edge.
- border-image-repeat: How the edges should scale or tile.
Shorthand Syntax
border-image: source slice width outset repeat;
Border Image Properties
1. border-image-source
Specifies the path to the image you want to use as the border. This works similarly to the background-image property.
border-image-source: url('path/to/image.png');
border-image-source: linear-gradient(to right, red, blue); /* Gradients work too! */
2. border-image-slice
Specifies how to slice the source image into regions. This is often the most confusing part for beginners. Think of it as a 3x3 grid (9-slice scaling) where the corners remain fixed, and the edges are stretched or repeated.
border-image-slice: 30; /* Slices 30px from all sides */
border-image-slice: 10% 20%; /* Top/Bottom 10%, Left/Right 20% */
border-image-slice: 10 20 30 40; /* Top, Right, Bottom, Left */
border-image-slice: 30 fill; /* The fill keyword preserves the middle part of the image as a background */
border-image-slice (e.g., 30px). In this property, raw numbers represent pixels in the original image file. Adding "px" can cause the property to fail.
3. border-image-width
Defines the width of the border image. This doesn't change the actual size of the element's box model, but rather how much space the image occupies visually.
border-image-width: 10px;
border-image-width: 1rem;
border-image-width: 5%;
border-image-width: auto;
4. border-image-outset
Specifies the amount by which the border image area extends beyond the border box. This allows the border to "float" outside the element without pushing nearby elements away.
border-image-outset: 10px;
border-image-outset: 1.5; /* Multiplier of the border-width */
border-image-outset renders outside the element's box, it can cause overlapping with neighboring elements or trigger unwanted scrollbars if it extends off-screen.
5. border-image-repeat
Determines how the border image is repeated. This is crucial for making a small border image fit a large container.
- stretch: Stretches the edge image to fill the area (default).
- repeat: Tiles the image until the area is filled.
- round: Tiles the image but stretches them slightly so they fit perfectly without being cut off.
- space: Tiles the image and adds spacing between them to fit perfectly.
border-image-repeat: round; /* Usually the best looking option */
border-image-repeat: stretch repeat; /* Stretch the top/bottom, repeat the sides */
Example
Here’s a basic example of how to combine these properties for a simple framed effect:
div {
border: 10px solid transparent; /* Required for border-image to work */
border-image-source: url('border-frame.png');
border-image-slice: 30;
border-image-width: 10px;
border-image-repeat: round;
}
Detailed Example
Let's create a real-world scenario where we want a decorative "fancy" box. This is common for pull-quotes or special announcements on a blog.
Image Preparation: For this example, assume you have a 90x90 pixel image where each corner is a 30px decorative element.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pro Border Image Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="premium-card">
<h2>Exclusive Content</h2>
<p>This element uses a custom border-image to create a unique UI look.</p>
</div>
</body>
</html>
- CSS (styles.css):
.premium-card {
width: 350px;
padding: 30px;
margin: 50px auto;
text-align: center;
/* 1. We must define a border-style and width first */
border: 20px solid transparent;
/* 2. Apply the border image */
border-image-source: url('fancy-frame.svg');
/* 3. Slice 30 pixels from each side of the source image */
border-image-slice: 30;
/* 4. Make the visual border 20px wide */
border-image-width: 20px;
/* 5. Ensure the pattern repeats beautifully without cutting off */
border-image-repeat: round;
}
Notes and Tips
border property (like border: 10px solid transparent;). If you don't define a border-width and style, the border-image will usually not appear at all because the element has no "border area" to draw on.
- Fallbacks: Always provide a standard
border-colororborderfallback for very old browsers (like IE 10) that do not supportborder-image. - Gradients as Borders: You can use
border-image-source: linear-gradient(...)to create stunning multicolored borders that are much more flexible than standard hex codes. - High-Resolution Images: If using raster images, use a file that is at least twice the size of your
border-image-widthto ensure it looks sharp on mobile devices. - Testing: Use the browser's developer tools to toggle
border-image-repeatvalues. Oftenroundlooks much more professional thanstretchfor patterns.
Using border-image in CSS allows you to create custom and intricate borders, enhancing the visual appeal of your web elements. Whether you're building a fantasy-themed website or a modern landing page with gradient borders, this property gives you the creative freedom to break away from standard rectangular boxes.