CSS Border Images
CSS Border Images allow you to use an image as the border for an element. This feature is implemented using the border-image
property, which provides control over the image source, slicing, width, and repeat behavior.
Key Topics
Basic Usage of border-image
The border-image
property sets an image as the border for an element. You need to specify the image source and can optionally set additional parameters like slice and repeat.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic border-image</title>
<style>
.border-image-box {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 30 round;
text-align: center;
line-height: 100px;
margin: 20px;
}
</style>
</head>
<body>
<div class="border-image-box">Border Image</div>
</body>
</html>
Explanation: The border-image
property sets an image as the border. The 30
specifies the slice value, and round
ensures the border is repeated and stretched evenly.
Slicing the Image
Slicing divides the border image into nine sections: four corners, four edges, and the middle (which is usually discarded). You can control how the slices behave.
<style>
.sliced-border {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 50 stretch;
text-align: center;
line-height: 100px;
margin: 20px;
}
</style>
<div class="sliced-border">Sliced Border</div>
Explanation: The 50
slice value divides the border image into sections, and the stretch
keyword ensures the slices stretch to fill the border area.
Repeat Behavior
The border-image-repeat
property controls how the image is repeated along the edges of the border. Options include stretch
, repeat
, and round
.
<style>
.repeat-border {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 30 repeat;
text-align: center;
line-height: 100px;
margin: 20px;
}
</style>
<div class="repeat-border">Repeated Border</div>
Explanation: The repeat
keyword ensures the border image is tiled repeatedly along the border edges.
Complete Example
This example demonstrates how to use border-image
with slicing, repeating, and stretching in one cohesive layout:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Border Images</title>
<style>
.border-image-box {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 30 round;
text-align: center;
line-height: 100px;
margin: 20px;
}
.sliced-border {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 50 stretch;
text-align: center;
line-height: 100px;
margin: 20px;
}
.repeat-border {
width: 200px;
height: 100px;
border: 20px solid;
border-image: url('https://via.placeholder.com/150') 30 repeat;
text-align: center;
line-height: 100px;
margin: 20px;
}
</style>
</head>
<body>
<div class="border-image-box">Rounded Image Border</div>
<div class="sliced-border">Sliced Image Border</div>
<div class="repeat-border">Repeated Image Border</div>
</body>
</html>
Explanation: This example combines different uses of the border-image
property to show its flexibility and design potential.
Key Takeaways
- Basic Usage: Use
border-image
to set an image as a border. - Slicing: Divide the image into sections for precise border control.
- Repeat Behavior: Adjust how the image repeats along the border edges with keywords like
stretch
,repeat
, andround
. - Flexibility: The
border-image
property enables creative and unique border designs.