border-image 讓邊框使用圖片,支援九宮格切割(四角保持、四邊拉伸/平鋪、中心可選)。國內教程鮮少講透,這裡完整拆解。
素材:whiteButton.png(含四角圓角、邊框漸層)

目標:寬度自適應的按鈕/區塊,邊框完美貼合圖片風格

.btn {
-webkit-border-image: url(images/whiteButton.png) 0 12 0 12 stretch stretch;
-moz-border-image: url(images/whiteButton.png) 0 12 0 12 round stretch;
border-image: url(images/whiteButton.png) 0 12 0 12 stretch stretch;
display: block;
width: 600px;
border-width: 0 12px; /* 左右邊框寬 12px,上下 0 */
padding: 10px;
text-align: center;
font: 16px sans-serif;
color: white;
}
border-image: none | <image> [ <number> | <percentage> ]{1,4} [ / <border-width>{1,4} ]? [ stretch | repeat | round ]{0,2}
| 部分 | 說明 |
|---|---|
<image> | 圖片 URL(相對/絕對路徑) |
<number> | 切片寬度(像素),對應圖片上右下左四邊向內切的距離 |
<percentage> | 切片寬度百分比(相對圖片尺寸) |
/ <border-width>{1,4} | 可選,覆蓋 border-width 設定邊框實際渲染寬度 |
stretch | 拉伸填滿(預設) |
repeat | 平鋪重複 |
round | 平鋪並自動縮放以完整填滿(不截斷) |
0 12 0 12stretch stretch:水平拉伸、垂直拉伸(垂直這裡上下切片為 0,實際由內容區高度決定)border-width: 0 12px:實際渲染的邊框寬度對應切片寬度支援:Safari 3+、Firefox 3.5+、Chrome 5+、Opera 10.53+、IE11+(IE10- 需 PIE 或放棄)。