← Back to CSS Course | Chapter 3: Box Model | Lesson 7 of 7

CSS Border Images

Think of a picture frame shop that can take any image, slice it into nine pieces like a tic-tac-toe grid, and wrap those pieces perfectly around a canvas of any size -- the corners stay crisp while the edges stretch or repeat to fit. border-image does exactly that slicing trick, letting an ordinary image become a scalable decorative border.
Syntax
css
selector {
  border-image: source slice / width / outset repeat;
}

The border-image Shorthand

border-image is a shorthand that combines a source image, how it's sliced, its rendered width, and how the sliced pieces are repeated or stretched along the edges, replacing a plain solid/dashed border with a graphic one.

Note:
  • Accepted values:
  • border-image-source — url() or a gradient
  • border-image-slice — number or percentage, optionally with fill
  • border-image-width — length, number, or auto
  • border-image-outset — length or number
  • border-image-repeat — stretch | repeat | round | space

Example: The border-image Shorthand

css
<style>
.box {
  border: 20px solid transparent;
  border-image: linear-gradient(45deg, red, blue) 20;
}
</style>
<div class="box">Graphic border instead of solid or dashed</div>

border-image-source

border-image-source specifies the image to use, which can be a raster image URL or even a CSS gradient -- gradients are a common lightweight alternative to an actual image file for simple decorative borders.

Note:
  • Accepted values:
  • url("...") — an image file
  • linear-gradient() — a gradient used as the border
  • radial-gradient() — a radial gradient border
  • none — no border image (initial value)
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: border-image-source

css
<style>
.box {
  border: 20px solid transparent;
  border-image-source: linear-gradient(90deg, orange, purple);
  border-image-slice: 20;
}
</style>
<div class="box">Gradient used as the border source</div>

border-image-slice

border-image-slice divides the source image into a 3x3 grid using four inward offsets (top, right, bottom, left), producing four corner pieces (never stretched), four edge pieces (stretched or repeated along one axis), and a center piece (used only if the fill keyword is added).

Note:
  • Accepted values:
  • number — pixels (or SVG units) cut from each edge, e.g. 30
  • percentage — relative to the image size
  • fill — also keeps the middle slice
  • one to four values — top, right, bottom, left order
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: border-image-slice

css
<style>
.box {
  border: 20px solid transparent;
  border-image-source: url('https://placehold.co/60x60');
  border-image-slice: 20 20 20 20;
}
</style>
<div class="box">Image sliced into a 3x3 grid</div>

border-image-width and Outset

border-image-width controls the rendered thickness of the border-image independently of the underlying border-width, and border-image-outset lets the image extend beyond the element's normal border box for decorative overhang effects.

Note:
  • Accepted values:
  • number — multiple of border-width (border-image-width)
  • length — e.g. 20px
  • percentage — relative to the border image area
  • auto — the slice's intrinsic size
  • border-image-outset — extends the image beyond the border box (number or length)
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: border-image-width and Outset

css
<style>
.box {
  border: 10px solid transparent;
  border-image-source: url('https://placehold.co/60x60');
  border-image-slice: 20;
  border-image-width: 15px;
  border-image-outset: 5px;
}
</style>
<div class="box">Rendered thickness and overhang set independently</div>

border-image-repeat: stretch vs round vs repeat

The repeat keyword controls how the edge pieces fill the space between corners: stretch scales them (may distort), repeat tiles them (may clip at the boundary), and round tiles them while scaling slightly so a whole number of copies fits without clipping.

Note:
  • Accepted values:
  • stretch — slices are stretched to fit (initial value)
  • repeat — slices are tiled and may be clipped
  • round — slices are tiled and scaled to fit whole
  • space — slices are tiled with even gaps
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: border-image-repeat: stretch vs round vs repeat

css
<style>
.box {
  border: 20px solid transparent;
  border-image-source: url('https://placehold.co/60x60');
  border-image-slice: 20;
}
.stretch { border-image-repeat: stretch; }
.round { border-image-repeat: round; }
.repeat { border-image-repeat: repeat; }
</style>
<div class="box stretch">stretch</div>
<div class="box round">round</div>
<div class="box repeat">repeat</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Setting border-image without a border width or style, so nothing appears.
  2. Using pixel units in border-image-slice, when it uses unitless numbers (or percentages) for image pixels.
  3. Using an image that is too small to slice correctly.
Chapter Summary
  • Margins and padding control space outside and inside an element, and margins can collapse.
  • Height and width set element size, and the box model explains how these parts combine.
  • Rounded corners and border images decorate the box.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.