border-image 属性是一个简写属性,用于设置以下属性:
默认值: none 100% 1 0 stretch
除了border-image-source外,每一个属性均可以填写4个值,分别表示上、右、下、左的相应量;也可以简写为2个值,分别表示上下、左右;或者只写1个值,表示上下左右均为该大小。
border-image: url(border.png) 25 30 repeat;
MDN https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-source
The border-image-source CSS property defines the
to use instead of the style of the border. If this property is set to none, the style defined by border-style is used instead.
CSS 定义了图片属性 border-image-source 来代替边框的风格。如果该属性被设置为 none,该样式就会被border-style取而代之。
/* no border-image, use the specified border-style */
border-image-source: none;
/* the image.jpg is used as image */
border-image-source: url(image.jpg);
/* a gradient is used as image */
border-image-source: linear-gradient(to top, red, yellow);
/* Global values */
border-image-source: inherit;
border-image-source: initial;
border-image-source: unset;
与CSS2中background-image属性一样,border-image的背景图使用url()调用,图片可以是相对路径或是绝对路径,也可以不使用图片,即border-image:none。
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
margin: 20px;
display: inline-block;
border-style: solid;
border-color: black;
border-image-source: url(border.png);
}
.thin {
border-width: 10px;
}
.fat {
border-width: 30px;
}
style>
head>
<body>
<div class="thin">div>
<div class="fat">div>
body>
html>
MDN https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-slice
The border-image-slice CSS property divides the image specified by border-image-source in nine regions: the four corners, the four edges and the middle. It does this by specifying 4 inwards offsets.
Four values control the position of the slice lines. If some are not specified, they are inferred from the other with the usual 4-value syntax of CSS.
The middle is not used by the border itself but is used as a background-image if the keyword fill is set. The keyword can be set at any position in the property (before, after or between the other values).
The border-image-repeat, border-image-width, border-image-outset properties define how these images will be used.
The shorthand CSS property border-image may reset this property to its default value.
CSS 属性 border-image-slice 将 border-image-source 定义的图片划分为九个区域:四个角,四个边和中间部分。它是通过指定四个向内的偏移来实现的。
四个值控制了分割线的位置。如果有某些未指定,它将会通过CSS常用的四值语法,通过其它值推断出。
中间部分将不会被边框使用,但是如果fill关键字被设置的话,会当作是背景图片。该关键字可以被设置在该属性的任何位置(之前,之后或者值得中间)。
border-image-repeat, border-image-width, border-image-outset 属性定义了如何使用那些图片。
CSS 属性 border-image 的简写发可能会重置该属性为它的默认值。
/* border-image-slice: slice */
border-image-slice: 30%;
/* border-image-slice: horizontal vertical */
border-image-slice: 10% 30%;
/* border-image-slice: top horizontal bottom */
border-image-slice: 30 30% 45;
/* border-image-slice: top right bottom left */
border-image-slice: 7 12 14 5;
/* border-image-slice: … fill */
/* The fill value can be placed between any value */
border-image-slice: 10% fill 7 12;
/* Global values */
border-image-slice: inherit;
border-image-slice: initial;
border-image-slice: unset;
注意:border-image-slice 不能带单位,否则无效(自动会被按像素(px)设置)
<number-percentage>{1,4} && fill?
where
<number-percentage> = <number> | <percentage>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
margin: 20px;
display: inline-block;
border-style: solid;
border-color: black;
border-image-source: url(border2.png);
}
.thin {
border-width: 10px;
}
.fat {
border-width: 30px;
}
.sliceSmall {
border-image-slice: 26;
}
.sliceMiddle {
border-image-slice: 39;
}
.sliceBig {
border-image-slice: 52;
}
.sliceSmallFill {
border-image-slice: 26 fill;
}
.sliceMiddleFill {
border-image-slice: 39 fill;
}
.sliceBigFill {
border-image-slice: 52 fill;
}
style>
head>
<body>
<img src="border2.png">
<br>
<div class="thin sliceSmall">div>
<div class="thin sliceMiddle">div>
<div class="thin sliceBig">div>
<br>
<div class="fat sliceSmall">div>
<div class="fat sliceMiddle">div>
<div class="fat sliceBig">div>
<br>
<div class="fat sliceSmallFill">div>
<div class="fat sliceMiddleFill">div>
<div class="fat sliceBigFill">div>
body>
html>
可以看到:
MDN https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-width
The border-image-width CSS property defines the width of the border image by defining inward offsets from the border edges. If the border-image-width is greater than the border-width, then the border image extends beyond the padding (and/or content) edge.
CSS 属性 border-image-width 通过定义从边框边缘向内的位移量,定义了边框图像的长度。如果 border-image-width 比 border-width 大,那么边框图像将会越过填充(或者内容)边缘。
/* border-image-width: all */
border-image-width: 3;
/* border-image-width: vertical horizontal */
border-image-width: 2em 3em;
/* border-image-width: top horizontal bottom */
border-image-width: 5% 15% 10%;
/* border-image-width: top right bottom left */
border-image-width: 5% 2em 10% auto;
/* Global values */
border-image-width: inherit;
border-image-width: initial;
border-image-width: unset;
[ <length-percentage> | <number> | auto ]{1,4}
where
<length-percentage> = <length> | <percentage>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
margin: 20px;
display: inline-block;
border-style: solid;
border-color: black;
border-image-source: url(border.png);
}
.thin {
border-width: 10px;
}
.fat {
border-width: 30px;
}
.sliceSmall {
border-image-slice: 26;
}
.sliceMiddle {
border-image-slice: 39;
}
.sliceBig {
border-image-slice: 52;
}
.borderImgWidth {
border-image-width: 30px;
}
style>
head>
<body>
<br>
<div class="thin borderImgWidth sliceSmall">div>
<div class="thin borderImgWidth sliceMiddle">div>
<div class="thin borderImgWidth sliceBig">div>
<br>
<div class="fat sliceSmall">div>
<div class="fat sliceMiddle">div>
<div class="fat sliceBig">div>
body>
html>
MDN https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-outset
The border-image-outset property describes by what amount the border image area extends beyond the border box.
Portions of the border image that are rendered outside the border box as a result of this property do not cause scrolling to be triggered. These areas also don’t capture or cause mouse events to occur on behalf of the bordered element.
border-image-outset 属性介绍了边框图像区域可以超出边框盒多大长度。
作为该属性的结果,边框图像的一部分会在边框盒之外被渲染,但不会触发滚动事件。这些区域也不会捕获或者造成发生在代表边框元素上的鼠标事件。
/* border-image-outset: sides */
border-image-outset: 1.5;
/* border-image-outset: vertical horizontal */
border-image-outset: 1 1.2;
/* border-image-outset: top horizontal bottom */
border-image-outset: 30px 2 45px;
/* border-image-outset: top right bottom left */
border-image-outset: 7px 12px 14px 5px;
border-image-outset: inherit;
[ <length> | <number> ]{1,4}
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
margin: 20px;
display: inline-block;
border-style: solid;
border-color: black;
border-width: 26px;
border-image-source: url(border.png);
border-image-slice: 26;
border-image-width: 26px;
border-image-outset: 26px;
}
style>
head>
<body>
<div>div>
body>
html>
MDN https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-repeat
The border-image-repeat CSS property defines how the middle part of a border image is handled so that it can match the size of the border. It has a one-value syntax that describes the behavior of all the sides, and a two-value syntax that sets a different value for the horizontal and vertical behavior.
CSS 属性 border-image-repeat 定义了如何处理边框图像的中间部分,以至于它可以满足边框的大小。它有一个单值的语法,描述了所有边的行为,和双值得的语法,它为水平和垂直的行为设置了一个不同的值。
- stretch
Keyword indicating that the image must be stretched to fill the gap between the two borders.- repeat
Keyword indicating that the image must be repeated until it fills the gap between the two borders.- round
Keyword indicating that the image must be repeated until it fills the gap between the two borders. If the image doesn’t fit after being repeated for an integral number of times, the image is rescaled to fit.- space
The image is tiled (repeated) to fill the area. If it does not fill the area with a whole number of tiles, the extra space is distributed around the tiles.
/* border-image-repeat: type */
border-image-repeat: stretch;
/* border-image-repeat: horizontal vertical */
border-image-repeat: round stretch;
/* Global values */
border-image-repeat: inherit;
border-image-repeat: initial;
border-image-repeat: unset;
[ stretch | repeat | round | space ]{1,2}
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
margin: 20px;
display: inline-block;
border-style: solid;
border-color: black;
border-width: 26px;
border-image-source: url(border.png);
border-image-slice: 26;
border-image-width: 26px;
}
.borderStretch {
border-image-repeat: stretch;
}
.borderRepeat {
border-image-repeat: repeat;
}
.borderRound {
border-image-repeat: round;
}
.borderSpace {
border-image-repeat: space;
}
style>
head>
<body>
<div>div>
<div class="borderStretch">div>
<div class="borderRepeat">div>
<div class="borderRound">div>
<div class="borderSpace">div>
body>
html>
下面这个小方格的大小是 72 x 72。单元格 3 等分,正好切成 9 宫格,每份 26px。
图片的基本规格与“素材一”相同,只不过在中间的空白区域补了一个大小为 26 x 26 的黑色块。然后黄线和绿点的宽度均为 2px。