TL;DR – CSS border images refer to images that you add as borders of HTML elements.
Contents
CSSÂ Border Image:Â Main Tips
- The
border-image
shorthand sets images as borders of elements. - For the shorthand to work, an element needs to have a border.
- Border images can be sliced or repeated in to fill in the lines.
How border-image Is Used
The CSS border-image
 property sets an image for the border of an element.
Here we see the original 60 x 60px image:
Here we see the image as a border!
This shorthand property combines these parameters in one declaration:
Property | Description |
---|---|
border-image | Shorthand property you can use for creating an image border |
border-image-source | Specifies the directory path for the required image |
border-image-slice | Specifies how the border image should be sliced |
border-image-width | Specifies the width of the border image |
border-image-outset | Specifies the distance between the border and the surrounding elements |
border-image-repeat | Specifies the way image is repeated in the border: rounded, stretched, spaced |
Remember: the shorthand requires only border-image-source. Other properties are optional.
Adding Image Borders to Elements
Here is a code example for setting up an image border for an HTML element:
#borderimage { Â Â Â
border: 15px solid transparent;Â Â Â
padding: 5px;Â Â Â
border-image: url('https://cdn.bitdegree.org/learn/border-img-5.png?229dfb2d') 20 stretch;
}
See the common steps of adding the CSS image borders:
- The
border-image
 property loads the image by using the provided file path. - Then, the image is sliced into sections. As a result, CSS generates separate corners and side border images.
- Then, CSS places the image corners at the corners of the border.
- The sections in between the corners are then stretched out from one corner to another.
Note: the border-image will work properly only if the element has border property.
Creating Borders From One Repeating Image
You do not have to stretch images to fill in the borders. Instead, this example repeats the image border to fill the line:
#borderimage { Â Â Â
border: 30px solid transparent;
padding: 5px;
border-image: url('https://cdn.bitdegree.org/learn/border-img-5.png?229dfb2d') 20 round;
}
Here we see that image acting as a border!
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Changing Slice Values
Understanding the border-image-slice
can be difficult. The property divides the border images into nine parts that become the element borders.
In the short code examples below, you see that the first line indicates the slicing in pixels. It means that all images are sliced the same regardless of their size.
When CSS accepts percentages, the slicing is relative to the image size.
Example No.1
border-image: url(doggo.png) 20 round;
Example no.2
border-image: url(doggo.png) 10% round;
Example no.3
border-image: url(doggo.png) 30% round;
Here is the full code with the examples above:
#borderimage1 {Â Â Â
border: 5px solid transparent;Â Â Â
padding: 10px;Â Â Â
border-image: url('https://cdn.bitdegree.org/learn/border-img-5.png?229dfb2d') 20 round;
}
#borderimage2 {Â
border: 15px solid transparent;Â Â Â
padding: 20px;Â Â Â
border-image: url('https://cdn.bitdegree.org/learn/border-img-5.png?229dfb2d') 10% round;
}
#borderimage3 {Â Â Â
border: 20px solid transparent;Â Â Â
padding: 10px;Â Â Â
border-image: url('https://cdn.bitdegree.org/learn/border-img-5.png?229dfb2d') 20% round;
}
CSS Border Image: Summary
- You can use CSS gradients as border images.
- The
border-image
does not work with table elements whenborder-collapse
has thecollapse
value.