Previously, adding decorative elements to web pages (such as fancy borders) required slicing the image and patiently adjusting the CSS code until the effect was satisfactory.
CSS now simplifies this process. Just a few lines of code to add rather complex borders to your website. This article will show you how to do this.
border-image-source
property. border-image-slice
Properties apply the selected image to the border, dividing the image into nine areas: four corners, four sides, and one middle area. You can choose to specify one to four numbers or percentage values for the offset. . border-image-width
Properties design the internal offset within the border image area, while the border-image-outset
property allows the border image area to be pushed outside the border frame. border
property to reset the border-image
property, which quickly resets the width, color, and style of all four borders of an element. At the time of writing, border-image
is fully supported in almost all major browsers. A common method of setting border styles is to use preset border-style
rules. These rules include: dotted
, dashed
, solid
, double
, groove
, ridge
, inset
, outset
,
.
border-image-source
Attributes
element { border-image-source: url('myimage.png'); }
element { border-image-source: linear-gradient(10deg, #fe01f5 0%, #2e113d 100%); }
In the browser, it looks like this:
none
border-style
If you set this property to a border-style
value, or the image cannot be displayed, the browser will use the value set for the
as a backup plan.
border-image-slice
Attributes border-image-source
border-image-slice
After selecting an image with the
element { border-image-source: url('myimage.png'); }
Let's learn more in more detail. This property design comes from the internal offsets on the top, right, bottom, and left. These offsets will eventually cut your small image into nine areas: four corners, four sides, and one middle area.
You can specify one to four numbers or percentage values. When you specify four values, they are applied to the top, right, bottom, and left offsets. If you skip the left offset, this will be the same as the right one. If you missed the bottom offset, this will be the same as the top. Omitting the value of the offset to the right will make it the same as the top. If you only use one value, it will be used for all four offsets.
Percentage value refers to the percentage of image size—the image width of the horizontal offset and the image height of the vertical offset.
Numbers represent pixels in the image, or in the case of vector images, coordinates. Another point, don't add px
after the number, this won't work!
The following is how you can use border-image-slice
:
element { border-image-source: linear-gradient(10deg, #fe01f5 0%, #2e113d 100%); }
element { border-image-slice: 19; }
Use an image of size 100 x 100 pixels as the border, and its appearance is as follows:
The final effect is as follows:
The middle area appears completely transparent and is therefore invisible. If you want to make it visible, add the
fill
keyword.
For example, using an image with a completely opaque middle area without adding the fill
keyword will be exactly the same as the example above. However, please apply the fill
keyword as follows:
<div class="box"> Border Image Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div>
Use an image containing details in the middle area:
Then we find that the middle area of the image is completely visible on the page, albeit a little blurry and compressed:
border-image-width
Attributes This property is drawn within the so-called border image area. By default, the boundary of this area is the boundary of the border box. Like the border-image-slice
property, border-image-width
designs internal offsets, dividing the image into nine areas.
This property accepts one to four values (top, right, bottom, left) and can use numbers or percentages. The percentage is relative to the size of the border image area, i.e. the area width of the horizontal offset and the area height of the vertical offset. If you use numbers without px
units, these numbers will equal multiples of the corresponding calculated border width. For example, the following code:
element { border-image-source: url('myimage.png'); }
…Set the width of the border image to 3 times the border width value, i.e. 19 pixels. The results are as follows:
I found that assigning the same value to the
border-image-width
and border-image-slice
properties ensures that your border image is displayed in the best condition without unwanted deformation.
border-image-outset
Attributes All the properties I've used so far have defaulted to the embedded border image area. However, you can choose to push the border image area outside the border box. You can use the border-image-outset
attribute to implement it.
This property takes one to four values (top, right, bottom, left) and is expressed in numbers or units of length (such as px
, em
, etc.). If you use numbers, the result will be that the border image is pushed outside the border box, with multiples being the calculated border width.
To further illustrate, I have drawn a green dotted outline to represent the border. The border image area contains a pink border image. In its default embedded state, the border image is located within the green outline. This means that the border image area is within the border frame.
Adding
border-image-outset: 19px;
to the CSS rule set pushes the pink border image beyond the dotted green outline. This means that the border image area is drawn outside the border:
Please note that the border image part located outside the border box will not trigger scrolling, nor will the mouse event be captured.
border-image-repeat
Attributes This property provides some choices about how to scale and tile image slices on the sides and middle sections of the border. The first value is applied to the horizontal side (top and bottom) and the second value is applied to the vertical side (right and left). If you set only one value, the value will be applied to both horizontal and vertical sides.
Available values include:
stretch
– If you do not use the border-image-repeat
attribute, this is the default value. This keyword stretches the image to fill the available area. repeat
– Image tiling repeats to fill the available area. If the available area cannot be divisible by the tiled width, the image may be cut off. round
– Same as repeat
, but if the space is not enough to accommodate tiles, the tiles are scaled until they all fit. This ensures that the tiling will never be cut off, but the image may look a bit compressed. space
– Same as repeat
, but if the space is not an exact multiple of the tile width, the extra white space will be evenly distributed around each tile. As of this writing, Firefox appears to render space
as the same as stretch
, while Chrome renders space
as the same as repeat
.
border-image
Abbreviation attributes You can compress all the individual properties discussed above into border-image
abbreviation properties as follows:
border-image-source
border-image-slice
border-image-width
border-image-outset
border-image-repeat
The following is a code snippet:
element { border-image-source: url('myimage.png'); }
The best way to reset the border is to use the abbreviation border
attribute. Using border
, you can quickly reset the same width, color, and style of all four borders of an element. There is no need to specify a border-image: none
rule, nor do you need to override any single border-image
attribute.
At the time of writing, border-image
is fully supported in almost all major browsers. Only Firefox cannot stretch SVG images across elements, Opera Mini supports abbreviation syntax with the -o-
prefix, but does not support a single attribute.
This article mainly introduces the border-image
attributes: the values it accepts, the best way to use, and the browser support level at the time of writing.
You can find more details in the CSS Background and Border Level 3 specification documentation.
If you use the border-image
attribute in your project, why not share the end result with the community?
Looking forward to your reply!
Creating a CSS border image involves using the border-image
attribute. This property allows you to specify an image that is used as the border around the element. The syntax of this property is as follows:
element { border-image-source: url('myimage.png'); }
source
is the URL of the image you want to use. slice
Defines the inner offset of the image. width
Sets the width of the border. outset
Determines the distance from the border image area beyond the border. repeat
Specifies how the image is tiled or repeated.
CSS provides several border styles that you can use to customize the appearance of web elements. These include: none
, hidden
, dotted
, dashed
, solid
, double
, groove
, ridge
, inset
, outset
, dotted
, double
,
creates a double-line border. inset
element { border-image-source: linear-gradient(10deg, #fe01f5 0%, #2e113d 100%); }
border-top-style
border-right-style
Can I use different border styles for different sides of an element? border-bottom-style
border-left-style
Yes, CSS allows you to apply different border styles to different sides of an element. You can specify the styles on each side using the
properties. border-image-width
property can be used to control the size of the CSS border image. This property sets the width of the border image by defining the size of the border area. You can specify width in pixels or as a percentage of the element box. linear-gradient
border-image
Can I use gradient images as borders in CSS?
property. border-image-repeat
stretch
How to make my CSS border image repeat? repeat
round
The space
attribute in CSS controls how border images are repeated. Possible values are stretch
(default), repeat
, round
, and space
.
border
Tile the image, but scale it to match exactly. border-image
Tile the image, but leave space between the tiles. attribute and the border
attribute in border-image
CSS?
Yes, you can use CSS border images with rounded corners. You can create rounded corners using the border-radius
property, and then apply the border image using the border-image
property.
Dashed borders can be created in CSS using the dashed
border style. The syntax is as follows:
element { border-image-source: url('myimage.png'); }
This will create a border with series of short lines or dotted lines.
The above is the detailed content of Decorating the Web with CSS Border Images. For more information, please follow other related articles on the PHP Chinese website!