搜索

CSS 教学

基本介绍

认识 CSS 开始使用 CSS CSS 语法规则 CSS 命名原则 CSS 常用样式属性

CSS 选择器

认识 CSS 选择器 优先级 ( 权重 ) 样式继承与联集 使用巢状结构语法 元素选择器 ID 和 Class 选择器 属性选择器 文件结构选择器 虚拟类别选择器 ( 结构 ) 虚拟类别选择器 ( 类型 ) 虚拟类别选择器 ( 输入 ) 虚拟类别选择器 ( 行为 ) 虚拟类别选择器 ( 超链接 ) 虚拟类别选择器 ( 逻辑 ) 虚拟类别选择器 ( 其他 ) 虚拟元素选择器 群组与组合选择器

数值与单位

全域关键字与文字数值 长度与角度单位 位置名称与时间单位 特殊样式属性 ( all、appearance)

规则与定义

变数 ( Variables ) 媒体查询 ( @media ) 容器查询 ( @container ) 自订属性值 ( @property ) 导入样式 ( @import ) 分层优先级 ( @layer ) 选择器作用域 ( @scope ) 定义字型 ( @font-face ) 计数规则 ( @counter-style ) 打印网页 ( @page )

函数类型

数学计算 文字与清单计数 形状与线段 路径 ( path )

颜色与滤镜

颜色单位 色彩模型 相对颜色 渐层色 图像滤镜 ( filter ) 背景滤镜 ( backdrop-filter ) 混合模式 ( mix-blend-mode )

文字与段落

设定字型 ( font-family ) 使用外部字型 文字尺寸 文字样式 ( 常用 ) 文字样式 ( 其他实用 ) 文字样式 ( 特殊用途 ) 文字换行 文字空白与 Tab 大小 文字行高与缩排 文字水平与垂直对齐 文字书写方向 文字自动分栏 使用 Color Font ( 彩色字型 ) 使用 Icon Font ( 图示文字 ) 制作自己的 Icon Font 显示生成内容与引号

元素容器

容器显示类型 ( display ) 元素 display 对照表 盒子模型 ( Box Model ) 宽高尺寸 ( width、height ) 显示与比例 ( box-sizing、aspect-ratio ) 内边距 ( padding ) 外边界 ( margin ) 边框 ( border ) 边框圆角 ( border-radius ) 图像边框 ( border-image ) 轮廓 ( outline ) 内容溢出与裁切 ( overflow ) 内容范围 ( contain ) 可见性与透明度 缩放与调整 ( zoom、resize )

背景与阴影

背景 ( 背景色、背景裁切 ) 背景图 ( 定位、尺寸 ) 背景图 ( 固定、重复 ) 背景图 ( 多重背景、混合 ) 背景缩写 ( background ) 容器阴影 ( box-shadow )

清单与表格

清单样式 清单计数器 表格基本样式 表格边框重叠与间距 表格内容宽度与对齐

基本排版与定位

元素排版方式 浮动 ( float ) 浮动形状 ( shape-* ) 定位 ( position ) 逻辑属性 ( logical properties ) 重置 CSS ( Reset CSS ) 水平置中技巧 垂直置中技巧

Flex 弹性排版

Flexbox 弹性盒子 Flex 对齐方式 Flex 弹性伸缩

Grid 网格排版

Grid 网格容器与格线 Grid 网格空间与命名 Grid 网格流向与间距 Grid 排列网格项目 Grid 项目对齐与顺序

转场与动画

转场 ( transition ) 转场触发事件 动画 ( animation ) 自订动画路径 ( offset ) 动画触发事件 多重动画的权重与顺序 动画进度控制 ( timeline )

变形、裁切与遮罩

裁切路径 ( clip-path ) 图像遮罩 ( mask ) 对象填满方式与定位 转换函数 ( transform ) 平移、旋转与缩放 3D 转换与透视

视窗与用户行为

卷轴样式 ( scrollbar ) 拉霸、滑杆样式 ( slider ) 鼠标光标图示 ( cursor ) 鼠标事件 ( pointer-events ) 用户选取 ( user-select ) 卷动行为 ( scroll、overscroll ) 打印换页 ( break-* )

CSS 预处理器

认识 CSS 预处理器 Less ( 安装、开始使用 ) Less ( 巢状结构、选择器 ) Less ( 变数、import ) Less ( Extend、Mixin ) Less ( 逻辑、循环、函数 )

范例效果

CSS 多重绕圈 3D 动画 CSS 圆饼图 CSS 跑马灯 Google 加载动画 电子时钟数字 渐层色的转场与动画 渐层色制作星空背景 渐层色边框 渐层色制作图案背景 不规则形状动画 弹跳的正方形动画 3D 正多面体 超链接底线动画效果 恶心黏黏球效果 CSS 视差滚动效果 卷轴控制放射形选单 卷轴改变文字背景色 CSS 页面卷动进度条 CSS 水波效果 圆点加载动画 ( 阴影动画 ) 字母翻牌效果 探照灯动画 立体双色跑马灯 楼梯文字动态效果 渐层色文字、双色文字 流动色彩文字 空心文字、文字水波背景 左右倾斜的背景效果 Icon 图示形状变换 故障文字效果 按钮的多种 hover 效果 点击展开或收折的选单 点击爱心图案放射效果 纯 CSS 数字加载进度条 纯 CSS 码表 ( 可计时 )

图像边框 ( border-image )

元素的边框除了基本的样式和颜色,也可以使用“图像”作为边框,这篇教学会介绍 border-image、border-image-slice、border-image-repeat 等边框图像的样式属性,运用丰富的图像素材,让边框更加特别,最后还会运用图像边框,实作“波浪形状”的边框效果。

请先阅读:边框 border

快速导览:

CSS 教学 - 图像边框 border-image

教学影片

搭配教学影片一起阅读,效果会更好!欢迎大家订阅 STEAM 教育学习网的 Youtube 频道。

前往:https://www.youtube.com/@steam.oxxostudio

border-image-source 边框的图像来源

如果元素具有较宽的边框,可以使用 border-image-source 设定边框的背景图片,用法和 background-image 完全相同,支援 jpg、gif、webp、png、svg 等常见的图像格式,也支援使用 CSS 渐层色函数产生的渐层色,写法如下:

图像边框不支援 border-collapse: collapse; 的表格元素。

div {
  border-image-source: url("圖片網址");
}

在单纯使用 border-image-source 且没有搭配其他样式的状况下,执行结果会将图片填满“四个角落”,在四个方向边框宽度相同时,图像缩放为“正方形”,如果四个方向边框宽度不同,则会缩放呈现不同的“长方形”,下方范例会呈现使用外部 jpg、svg 和内嵌 svg 的做法,并用浅灰色背景凸显元素范围。

线上展示:https://codepen.io/oxxo/pen/qBGezPP

<!-- HTML 程式碼 -->
<div class="a">使用外部 jpg</div>
<div class="b">使用外部 svg</div>
<div class="c">使用內嵌 svg</div>
<div class="d">使用漸層色</div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 5px;
    box-sizing:border-box;
    border: 50px solid #000;
    background: #eee;
    float: left;
  }
  .a {
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-1.jpg");
  }
  .b {
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-svg-1.svg");
  }
  .c {
    border-image-source: url('data:image/svg+xml,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 237 237" style="enable-background:new 0 0 237 237;" xml:space="preserve"><g><rect style="fill:%23FF5F5F;" width="79" height="79"/></g><g><rect x="79" style="fill:%23FFBD52;" width="79" height="79"/></g><g><rect x="158" style="fill:%2389FF00;" width="79" height="79"/></g><g><rect y="79" style="fill:%231B9E00;" width="79" height="79"/></g><g><rect x="79" y="79" style="fill:%237BCAFF;" width="79" height="79"/></g><g><rect x="158" y="79" style="fill:%230019FF;" width="79" height="79"/></g><g><rect y="158" style="fill:%23B440FF;" width="79" height="79"/></g><g><rect x="79" y="158" style="fill:%23CCCCCC;" width="79" height="79"/></g><g><rect x="158" y="158" style="fill:%233D3D3D;" width="79" height="79"/></g></svg>');
  }
  .d {
    border-image-source: conic-gradient(
      #500 0, #500 45deg,
      #f00 45deg, #f00 90deg,
      #f50 90deg, #f50 135deg,
      #ff0 135deg,#ff0 180deg,
      #0c0 180deg, #0c0 225deg,
      #09d 225deg, #09d 270deg,
      #00b 270deg, #00b 315deg,
      #909 315deg, #909 360deg
    );
  }
</style>

CSS 教学 - 图像边框 border-image - border-image-source 边框的图像来源

使用图像边框后,元素边框会有下列几种有特性:

  • 忽略边框颜色:border-color 相关样式失效。
  • 不会被边框圆角裁切:边框的图像覆盖在元素上,反而会覆盖在边框圆角上方。

下方的范例虽然有套用了边框圆角效果,但因为使用了图像边框,如果是“全部填满”的图像边框,圆角效果看起来就像是没有作用,如果是“背景透明”的图像边框,就可以观察到边框圆角效果。

线上展示:https://codepen.io/oxxo/pen/KKLOjyw

<!-- HTML 程式碼 -->
<div class="a">hello apple</div>
<div class="b">hello banana</div>
<div class="c">hello oxxo</div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 10px;
    box-sizing:border-box;
    border: 50px solid #000;
    background: orange;
    float: left;
    border-radius: 80px;
  }
  .a {border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-1.jpg");}
  .b {border-image-source: url("https://steam.oxxostudio.tw/download/css/border-svg-2.svg");}
  .c {
    border-radius: 50px;   /* 把邊框圓角半徑變小 */
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-1.jpg");
  }
</style>

CSS 教学 - 图像边框 border-image - 使用图像边框后,元素边框几种有特性

border-image-slice 边框的图像切片

border-image-slice 样式属性会将作为边框图像的图片,透过“切片”的方式分割成“九宫格”,九宫格的每个格子按照下图,分别对应元素容器和边框组成的九宫格:

CSS 教学 - 图像边框 border-image - 透过“切片”的方式分割成“九宫格”

border-image-slice 撰写语法和 border 相似,使用单一数值 ( 无单位 ) 或使用空白分隔多个数值,最后也可以加入 fill 关键字。

/* 使用 1~4 個數值 */
div {
  border-image-slice: (上下左右);
  border-image-slice: (上下) (左右);
  border-image-slice: 上 (左右) 下;
  border-image-slice: 上 右 下 左;
}
/* 使用 1~4 個數值,最後加上 fill 關鍵字 */
div {
  border-image-slice: (上下左右) fill;   
  border-image-slice: (上下) (左右) fill;
  border-image-slice: 上 (左右) 下 fill;
  border-image-slice: 上 右 下 左 fill;
}

属性数值表示“切片的长度”,意思为图片指定位置切线“由外往内”的移动距离,支援数字和百分比两种:

属性值 说明
数字 必须“无单位”数字,但实际单位为 px。
% ( 默认值,100% ) 乘以图片原始宽度的百分比。
fill ( 关键字 ) 默认不使用,表示不会用边框填满元素内容,使用后会九宫格的中间区域填满元素内容。

CSS 教学 - 图像边框 border-image - 指定位置切片“由外往内”的移动距离

下方的例子使用一张 300x300 九宫格正方形的九色图片,设定不同切片数字后会对应到不同的套用结果 ( 图片的左侧是图片切片的对照 )。

线上展示:https://codepen.io/oxxo/pen/RwmXzjY

<!-- HTML 程式碼 -->
<div class="a">apple</div>
<div class="b">banana</div>
<div class="c">coconut</div>
<div class="d">oxxo</div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 10px;
    padding: 10px;  
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
  }
  .a {border-image-slice: 100;}
  .b {border-image-slice: 100 50;}
  .c {border-image-slice: 100 50 20;}
  .d {border-image-slice: 100 50 20 10;}
</style>

CSS 教学 - 图像边框 border-image - 设定不同切片数字

从上图可以看出,如果将“非正方形”的切片套用到正方形的区域,就会发生拉伸变形的状况,同理如果将“正方形”切片套用到长方形的区域,也会发生拉伸变形的状况,下方范例将正方形九宫格的切片图案,套用到长方形容器的图像边框,除了四个正方形的角落,其他四个方向的边缘都会出现拉伸或压缩的状况 ( 如果是位图,将小图片到用到大容器时,还会发生分辨率不足的状况 )

线上展示:https://codepen.io/oxxo/pen/ZENgdvO

<!-- HTML 程式碼 -->
<div class="a"></div>
<div class="b"></div>

<!-- CSS 程式碼 -->
<style>
  div {
    margin: 10px;
    padding: 10px;  
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
    border-image-slice: 100;
  }
  .a {
    width: 300px;
    height: 150px;
  }
  .b {
    width: 100px;
    height: 300px;
  }
</style>

CSS 教学 - 图像边框 border-image - 将“正方形”切片套用到长方形的区域

通常设定切片时,必须注意“切片总尺寸”必须小于“图片尺寸”( 宽高分开计算 ),不然就会发生“无法切出图片”的空白状况,对应的边框也就不会有图像,相关规则如下:

状况 边框四个角 边框四个边
图片尺寸 > 切片总尺寸 填满切片 填满切片
图片尺寸 <= 切片总尺寸 填满切片 空白

下方范例的前两个 div 的切片总尺寸都小于图片尺寸,所以可以正常计算出图像边框的图片,但后两个 div 因为切片总尺寸太大,部分边框无法计算出要套用的图片尺寸,就会保留空白 ( 范例使用一个 span 作为实际内容区域 )。

线上展示:https://codepen.io/oxxo/pen/vYwoqda

<!-- HTML 程式碼 -->
<div class="a"><span>apple</span></div>
<div class="b"><span>banana</span></div>
<div class="c"><span>coconut</span></div>
<div class="d"><span>oxxo</span></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 20px;
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
  }
  span {
    display: block;
    width: 100%;
    height: 100%;
    padding: 10px;
    background: yellow;
    box-sizing:border-box;
  }
  .a {border-image-slice: 140;}             /* 140+140 < 300 */
  .b {border-image-slice: 100 249 100 50;}  /* 249+50 < 300 */
  .c {border-image-slice: 500;}             /* 500+500 > 300 */
  .d {border-image-slice: 100 250;}         /* 250+250 > 300 */
</style>

CSS 教学 - 图像边框 border-image - 切片总尺寸都小于图片尺寸

如果使用“百分比”为单位,表示“原始图片的尺寸的百分比”,使用方法和使用数字差不多,但因为是以百分比为单位,就不需要去知道原始图片尺寸,只要注意不要超过 50% 就都可以正常显示 ( 裁切总尺寸如果等于图片尺寸,仍然无法正常切片 )。

线上展示:https://codepen.io/oxxo/pen/NWVQZyQ

<!-- HTML 程式碼 -->
<div class="a"><span>apple</span></div>
<div class="b"><span>banana</span></div>
<div class="c"><span>coconut</span></div>
<div class="d"><span>oxxo</span></div>

<!-- CSS 程式碼 -->
<style>
  div {
      width: 200px;
      height: 200px;
      margin: 20px;
      box-sizing:border-box;
      border: 50px solid #000;
      border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
    }
    span {
      display: block;
      width: 100%;
      height: 100%;
      padding: 10px;
      background: yellow;
      box-sizing:border-box;
    }
    .a {border-image-slice: 33.3%;}   /* 33.3% + 33.3% < 100%  */
    .b {border-image-slice: 25% 10%;} /* 25% + 25% < 100%  */
    .c {border-image-slice: 50%;}     /* 50% + 50% = 100%  */
    .d {border-image-slice: 100%;}    /* 100% + 100% > 100%  */
</style>

CSS 教学 - 图像边框 border-image - 使用百分比为单位

border-image-slice 默认会将内容的部分“留空”,如果加上 fill 关键字,则会将切片后九宫格正中间的切片,填满内容区域,不过如果元素有内容,内容会覆盖在边框之上 ( 边框覆盖在背景色之上 ),此外,如果切片总尺寸大于图片尺寸,中间区域就会像四个边缘一样呈现空白。

线上展示:https://codepen.io/oxxo/pen/MWdNMVe

<!-- HTML 程式碼 -->
<div class="a"><span></span></div>
<div class="b"><span></span></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 20px;
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
  }
  span {
    display: block;
    width: 100%;
    height: 100%;
    padding: 10px;
    background: #ff0a;     /* 使用半透明黃色,凸顯內容 */
    box-sizing:border-box;
  }
  .a {border-image-slice: 33.33% fill;}
  .b {border-image-slice: 25% 10% fill;}
</style>

CSS 教学 - 图像边框 border-image - fill 关键字

border-image-repeat 边框的图像重复

border-image-repeat 可以设定图像边框四周图像的“重复模式”,有下列几个属性值:

属性值 说明
stretch 默认值,每个区域套用一个切片,“不等比例”缩放切片填满区域。
repeat “等比例”缩放并重复切片直到填满区域 ( 切片可能会被截断 )。
round “不等比例”缩放并重复切片直到填满区域 ( 切片不会被截断 )。
space “等比例”缩放并重复切片,搭配“空白”填满区域 ( 切片不会被截断 )。

下面使用四个 div 和同一张边框图像,呈现不同重复模式的效果。

线上展示:https://codepen.io/oxxo/pen/oNRKrqq

<!-- HTML 程式碼 -->
<div class="a">border-image-repeat: stretch;</div>
<div class="b">border-image-repeat: repeat;</div>
<div class="c">border-image-repeat: round;</div>
<div class="d">border-image-repeat: space;</div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 370px;
    height: 180px;
    margin: 5px;
    padding: 10px;
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
    border-image-slice: 100;
  }
  .a {border-image-repeat: stretch;}
  .b {border-image-repeat: repeat;}
  .c {border-image-repeat: round;}
  .d {border-image-repeat: space;}
</style>

CSS 教学 - 图像边框 border-image - border-image-repeat 边框的图像重复

border-image-outset 边框的图像扩展范围

border-image-outset 可以设定边框图像向外的“扩展范围”,设定后会将图像边框往外推,不过这个样式不会影响容器和其他容器的关系,也不会影响容器的行高,纯粹只影响显示的“样式”。

border-image-outset 的属性值使用“具有长度单位”或“不具有单位”的数字,并且和 border-width 一样,也有支援 1~4 个数值的写法。使用的单位如下:

属性值单位 说明
没有单位 border-width 的几倍。
px 像素。
em 元素本身字号的几倍。
rem 根元素字号的几倍。
pt、cm、mm、in 印刷单位。

下方范例会呈现不同 border-image-outset 产生的扩展效果,可以看到虽然 div 的前后有 span 元素,但扩展范围不会影响其他元素。

线上展示:https://codepen.io/oxxo/pen/QWReXrw

<!-- HTML 程式碼 -->
<div class="a"><span></span></div>
<span>hello world, I am oxxo!!</span>
<br>
<div class="b"><span></span></div>
<span>hello world, I am oxxo!!</span>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 20px 40px;
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
    border-image-slice: 100;
  }
  div span {
    display: block;
    width: 100%;
    height: 100%;
    background: yellow;
  }
  .a {
    border-width: 20px 40px 60px 80px;
    border-image-outset: 0.5;   /* 10px 20px 30px 40px */
  }
  .b {
    border-image-outset: 30px;
  }
</style>

CSS 教学 - 图像边框 border-image - border-image-outset 边框的图像扩展范围

border-image-width 边框的图像宽度

border-image-width 可以设定边框的“图像宽度”,通常使用图像边框时,默认都会将图像“填满”边框,但运用这个样式就能设定图像的尺寸,进而产生“裁切”或“留白”的效果,如果图像宽度小于边框宽度就会留白,如果大于边框宽度就会裁切,最大的图像宽度为元素宽高的一半。

border-image-width 的属性值使用“具有长度单位”或“不具有单位”的数字,和 border-width 一样,也支援 1~4 个数值的写法。使用的单位如下:

属性值单位 说明
没有单位 border-width 的几倍。
px 像素。
% 元素对应方向的宽度或高度。
em 元素本身字号的几倍。
rem 根元素字号的几倍。
pt、cm、mm、in 印刷单位。

下方范例使用不同的 border-image-width 数值呈现不同效果。

线上展示:https://codepen.io/oxxo/pen/ZENgdop

<!-- HTML 程式碼 -->
<div class="a"><span></span></div>
<div class="b"><span></span></div>
<div class="c"><span></span></div>
<div class="d"><span></span></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 200px;
    height: 200px;
    margin: 30px;
    box-sizing:border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg");
    border-image-slice: 100;
  }
  div span {
    display: block;
    width: 100%;
    height: 100%;
    background: yellow;
  }
  .a {border-image-width: 10%;}  /* 200px x 10 = 20px */
  .b {border-image-width: 20px;}
  .c {border-image-width: 20px 50px; }
  .d {border-image-width: 80px;}
</style>

CSS 教学 - 图像边框 border-image - border-image-width 边框的图像宽度

border-image 图像边框 ( 缩写格式 )

如果不要一个一个样式的设定图像边框,也可以使用 border-image 撰写缩写格式,缩写格式的样式会包含上述各个样式,撰写规则如下:

属性值 是否必要 分隔符号 按照顺序
border-image-source 必要 空白 不用按照顺序
order-image-repeat 非必要,默认 stretch 空白 不用按照顺序
border-image-slice 非必要,默认 100% / 按照顺序,顺序 1
border-image-width 非必要,默认 1 / 按照顺序,顺序 2
border-image-outset 非必要,默认 0 / 按照顺序,顺序 3

border-image 撰写语法如下,需要按照顺序的属性值必须按照写法顺序撰写。

div {
  border-image: border-image-source border-image-slice / border-image-width / border-image-outset border-image-repeat;
}

下方的范例会使用四个 div,透过不同的 border-image 撰写方式,产生四种截然不同的效果 ( 同一张图片 )。

线上展示:https://codepen.io/oxxo/pen/xxNvojX

<!-- HTML 程式碼 -->
<div class="a"><span>apple</span></div>
<div class="b"><span>banana</span></div>
<div class="c"><span>coconut</span></div>
<div class="d"><span>oxxo</span></div>

<!-- CSS 程式碼 -->
<style>
 div {
    width: 200px;
    height: 200px;
    margin: 30px;
    box-sizing: border-box;
    border: 50px solid #000;
    float: left;
  }
  div span {
    display: block;
    width: 100%;
    height: 100%;
    background: yellow;
  }
  .a {border-image: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg") 100;}
  .b {border-image: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg") 120 repeat;}
  .c {border-image: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg") 100 50 / 50px / 5px;}
  .d {border-image: url("https://steam.oxxostudio.tw/download/css/border-img-2.jpg") 50 30 fill / 10px / 10px 5px space;}
</style>

CSS 教学 - 图像边框 border-image - border-image 图像边框缩写格式

波浪造型边框效果

下方范例会利用波浪造型的图片,搭配 border-image-slice 和 border-image-repeat 的方式,做出波浪造型的边框 ( 类似邮票 )。

线上展示:https://codepen.io/oxxo/pen/vYwoqjz

<!-- HTML 程式碼 -->
<div class="a"><span>apple</span></div>
<div class="b"><span>banana</span></div>
<div class="c"><span>coconut</span></div>
<div class="d"><span>oxxo</span></div>

<!-- CSS 程式碼 -->
<style>
 div {
    width: 200px;
    height: 200px;
    box-sizing: border-box;
    border: 50px solid #000;
    border-image-source: url("https://steam.oxxostudio.tw/download/css/border-svg-3.svg");
    border-image-slice: 33.33% 38% 44.3%;
    border-image-width: 1 1 1.3 1;
    border-image-repeat: round;
    float: left;
  }
  div span {
    display: block;
    width: 100%;
    height: 100%;
    background: yellow;
  }
  .a {width: 300px;}
  .b {height: 300px;}
  .c {border-width: 20px;}
  .d {
    width: 100px;
    height: 100px;
    border-width: 20px;
  }
</style>

CSS 教学 - 图像边框 border-image - 波浪造型边框效果

小结

图像边框的样式大概是 border 相关样式中最为复杂的,同时也是需要理解图像和切片概念才能操作的样式,不过一但熟悉之后,就会是最容易做出特殊效果的样式。

意见反馈

微信二维码 微信扫码关注 抖音二维码 抖音扫码关注