搜索

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 码表 ( 可计时 )

自订动画路径 ( offset )

如果已经熟悉了 CSS 动画的基本语法,接下来还能透过一系列 CSS offset 样式属性,使用类似 SVG 的 path 路径绘制独一无二的动画路线,这篇教学除会介绍与“自订动画路径”相关的 offset、offset-anchor、offset-distance、offset-path、offset-position 和 offset-rotate 样式属性。

阅读更多:CSS 动画 animation、CSS animation 触发事件、CSS 多重动画的权重与顺序

快速导览:

CSS 教学 - 自订动画路径 offset

offset-path 动画路径

offset-path 表示“设定动画路径”的样式属性,没有继承特性,需要搭配 offset-distance 样式属性共同操作,如果没有使用 offset-distance,位置会一直保持在路径的起点,offset-path 所使用的属性值为“形状、线段、路径”相关的 CSS 函数 ( 点击函数名称可以查看详细教学 ):

CSS 函数 说明
circle() 圆形
ellipse() 椭圆形
rect() 四边形
polygon() 多边形
inset() 内缩边距和圆角
xywh() 虚拟矩形
ray() 放射线形状
path() 路径

下方的范例会呈现不同路径的元素位置 ( 使用同样路径的 SVG 放在下方 )。

注意,因为 CSS 的 path() 没办法像 SVG 可以透过 viewpoint 缩放,因此在定义路径时就必须要设定好长宽尺寸。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-miterlimit="10" d="m35 11c-30.1 10.9-41.8 95.7-7 131 34.8 35.3 79 31.6 46 64-33 32.4 101.2 114.2 118 32 16.8-82.2 47.8-158.9-28-136-75.8 22.9-128.6 28.1-82-17 46.6-45.1 14.5-96.3-47-74z"/></svg>
  <div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-miterlimit="10" d="m148.9 281.8c-64.5-11.6-95.1-36.8-119.4-88.4-24.3-51.6 5-110.8 53.9-144.8 48.9-34 105.6-37.6 154 8 32 30.1 33.6 80.3 28.9 100-10.8 46-33.7 57.4-62 70-36.7 16.3-76.4 15.6-111.6-21.2-16.5-17.3-23.7-63.6-6.6-90.9 14.2-22.8 56-58.8 87.3-47.9 31.4 11 39.3 29.6 50.6 57.1 9.3 22.7-10.3 48.4-44.2 63.2-34 14.7-58.7-13.3-57-29.3 4.5-41.6 46.4-33.7 46.4-33.7"/></svg>
  <div></div>
</div>
<div class="c">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="d">
  <svg viewBox="0 0 300 300"><rect fill="none" stroke="#000" stroke-miterlimit="10" x="50" y="50" rx="20" ry="20" width="200" height="150"/></svg>
  <div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 300px;
    float: left;
  }
  div {margin:0 -50px -20px 0;}
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 20px;
    height: 20px;
    background: red;
    float: none;
    offset-distance: 50%;  /* 調整後可以觀察位置 */
  }
  /* 自訂路徑 */
  .a div {
    offset-path: path("m35 11c-30.1 10.9-41.8 95.7-7 131 34.8 35.3 79 31.6 46 64-33 32.4 101.2 114.2 118 32 16.8-82.2 47.8-158.9-28-136-75.8 22.9-128.6 28.1-82-17 46.6-45.1 14.5-96.3-47-74z");
  }
  /* 自訂路徑 */
  .b div {
    offset-path: path("m148.9 281.8c-64.5-11.6-95.1-36.8-119.4-88.4-24.3-51.6 5-110.8 53.9-144.8 48.9-34 105.6-37.6 154 8 32 30.1 33.6 80.3 28.9 100-10.8 46-33.7 57.4-62 70-36.7 16.3-76.4 15.6-111.6-21.2-16.5-17.3-23.7-63.6-6.6-90.9 14.2-22.8 56-58.8 87.3-47.9 31.4 11 39.3 29.6 50.6 57.1 9.3 22.7-10.3 48.4-44.2 63.2-34 14.7-58.7-13.3-57-29.3 4.5-41.6 46.4-33.7 46.4-33.7");
  }
  .c div {offset-path: circle(100px at 150px 150px);}             /* 圓形 */
  .d div {offset-path: xywh(50px 50px 200px 150px round 20px); }  /* 矩形 */
</style>

CSS 教学 - 自订动画路径 offset - offset-path 动画路径

offset-distance 移动距离

offset-distance 表示“元素在路径中移动的距离”,没有继承特性,只能使用“百分比”作为单位,0% 表示起点,100% 表示末端点,执行顺序“必须位于 offset-path 后方”,下方范例会搭配 animation 动画,让红色 div 在不同线段中移动。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-miterlimit="10" d="m35 11c-30.1 10.9-41.8 95.7-7 131 34.8 35.3 79 31.6 46 64-33 32.4 101.2 114.2 118 32 16.8-82.2 47.8-158.9-28-136-75.8 22.9-128.6 28.1-82-17 46.6-45.1 14.5-96.3-47-74z"/></svg>
  <div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-miterlimit="10" d="m148.9 281.8c-64.5-11.6-95.1-36.8-119.4-88.4-24.3-51.6 5-110.8 53.9-144.8 48.9-34 105.6-37.6 154 8 32 30.1 33.6 80.3 28.9 100-10.8 46-33.7 57.4-62 70-36.7 16.3-76.4 15.6-111.6-21.2-16.5-17.3-23.7-63.6-6.6-90.9 14.2-22.8 56-58.8 87.3-47.9 31.4 11 39.3 29.6 50.6 57.1 9.3 22.7-10.3 48.4-44.2 63.2-34 14.7-58.7-13.3-57-29.3 4.5-41.6 46.4-33.7 46.4-33.7"/></svg>
  <div></div>
</div>
<div class="c">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="d">
  <svg viewBox="0 0 300 300"><rect fill="none" stroke="#000" stroke-miterlimit="10" x="50" y="50" rx="20" ry="20" width="200" height="150"/></svg>
  <div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 300px;
    float: left;
  }
  div {margin:0 -50px -20px 0;}
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 20px;
    height: 20px;
    background: red;
    float: none;
    animation: oxxo 2s infinite alternate; /* 執行動畫 */
  }
  .a div {
    offset-path: path("m35 11c-30.1 10.9-41.8 95.7-7 131 34.8 35.3 79 31.6 46 64-33 32.4 101.2 114.2 118 32 16.8-82.2 47.8-158.9-28-136-75.8 22.9-128.6 28.1-82-17 46.6-45.1 14.5-96.3-47-74z");
  }
  .b div {
    offset-path: path("m148.9 281.8c-64.5-11.6-95.1-36.8-119.4-88.4-24.3-51.6 5-110.8 53.9-144.8 48.9-34 105.6-37.6 154 8 32 30.1 33.6 80.3 28.9 100-10.8 46-33.7 57.4-62 70-36.7 16.3-76.4 15.6-111.6-21.2-16.5-17.3-23.7-63.6-6.6-90.9 14.2-22.8 56-58.8 87.3-47.9 31.4 11 39.3 29.6 50.6 57.1 9.3 22.7-10.3 48.4-44.2 63.2-34 14.7-58.7-13.3-57-29.3 4.5-41.6 46.4-33.7 46.4-33.7");
  }
  .c div {offset-path: circle(100px at 150px 150px); }
  .d div {offset-path: xywh(50px 50px 200px 150px round 20px);}

  @keyframes oxxo {
    0% {offset-distance: 0%;}
    100% {offset-distance: 100%;}
  }
</style>

CSS 教学 - 自订动画路径 offset - offset-distance 移动距离

offset-anchor 对齐路径

offset-anchor 表示“元素如何对齐路径”,没有继承特性,会使用 1~2 个属性值,定义元素的“对齐点”和“路径”之间的关系,属性值支援三种写法:

下方范例会使用比较粗的线条,呈现这些写法以及对齐路径的差异。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="100" d="m22.4 88h244.2z"/></svg><div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="100" d="m22.4 88h244.2z"/></svg><div></div>
</div>
<div class="c">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="100" d="m22.4 88h244.2z"/></svg><div></div>
</div>
<div class="d">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="100" d="m22.4 88h244.2z"/></svg><div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 200px;
    float: left;
  }
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 50px;
    height: 50px;
    background: #f00a;
    float: none;
    offset-path: path("m22.4 88h244.2z");           /* 使用直線路徑 */
  }
  .a div {offset-anchor: center;}                   /* 中心點對齊路徑起始位置與中線 */
  .b div {offset-anchor: left bottom;}              /* 左下點對齊路徑起始位置與中線  */
  .c div {offset-anchor: 10px 10px;}                /* 左上內縮 10px 10px 對齊路徑起始位置與中線  */
  .d div {offset-anchor: right 20px bottom 50px;}   /* 右下內縮 20px 50px 對齊路徑起始位置與中線  */
</style>

CSS 教学 - 自订动画路径 offset - offset-anchor 对齐笔画

offset-rotate 旋转方向

offset-rotate 表示“元素在路径上的旋转方向”,没有继承特性,有下列几种属性值写法

属性值 说明
auto 默认值,表示旋转方向和路径方向相同。
auto angle 在路径的方向上额外加上角度 ( 使用角度单位 ),顺时针为正。
angle 沿着 X 轴的水平方向为 0deg,完全按照指定角度旋转 ( 使用角度单位 ),顺时针为正。
reverse auto 反转 180deg,表示旋转方向和路径方向相反。
reverse angle 在路径相反方向上额外加上角度 ( 使用角度单位 ),顺时针为正。

下方范例会展示这五种旋转方向的差异。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="2" d="m22 21l116.5 116.5z"/></svg>
  <div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="2" d="m22 21l116.5 116.5z"/></svg>
  <div></div>
</div>
<div class="c">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="2" d="m22 21l116.5 116.5z"/></svg>
  <div></div>
</div>
<div class="d">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="2" d="m22 21l116.5 116.5z"/></svg>
  <div></div>
</div>
<div class="e">
  <svg viewBox="0 0 300 300"><path fill="none" stroke="#000" stroke-width="2" d="m22 21l116.5 116.5z"/></svg>
  <div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 200px;
    float: left;
    margin: 10px -100px 10px 10px;
  }
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 0;
    height: 0;
    float: none;
    offset-path: path("m22 21l116.5 116.5z");
    border-width: 10px 0 10px 50px;
    border-color: #0000 #0000 #0000 #f00b;
    border-style: solid;
  }
  .a div {offset-rotate: auto}
  .b div {offset-rotate: 45deg;}
  .c div {offset-rotate: auto 45deg;}
  .d div {offset-rotate: reverse;}
  .d div {offset-rotate: reverse 45deg;}
</style>

CSS 教学 - 自订动画路径 offset - offset-rotate 旋转方向

下方范例使用动画的方式,呈现 auto 和固定角度的差异。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 200px;
    float: left;
    margin: 20px -20px;
  }
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 0;
    height: 0;
    float: none;
    offset-path: circle(100px at 150px 150px);
    border-width: 20px 0 20px 100px;
    border-color: #0000 #0000 #0000 #f00b;
    border-style: solid;
    animation: oxxo 2s infinite linear;
  }
  .a div {offset-rotate: auto;}
  .b div {offset-rotate: 45deg;}
  @keyframes oxxo {
    0% {offset-distance: 0%;}
    100% {offset-distance: 100%;}
  }
</style>

CSS 教学 - 自订动画路径 offset - offset-rotate auto 和固定角度的差异

offset-position 路径起点位置

offset-position 表示“路径起点位置”,没有继承特性,会使用 1~2 个属性值定义元素的“起点位置”,目前主要支援 ray() 函数产生的路径,定义方式如下:

参考:ray() 放射线

下方范例会使用四个 div,呈现这些写法以及不同起点的差异。

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

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

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 200px;
    height: 200px;
    float: left;
    margin: 50px;
    border: 1px solid #000;
  }
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 50px;
    height: 50px;
    background: #f00a;
    float: none;
    offset-path: ray(30deg);
  }
  .a div {offset-position: normal;}
  .b div {offset-position: right;}
  .c div {offset-position: bottom left;}  
  .d div {offset-position: 30% 30%;}
</style>

CSS 教学 - 自订动画路径 offset - offset-position 路径起点位置

offset 动画路径缩写格式

offset 表示“动画路径的缩写格式”,没有继承特性,是由上述所有的样式属性缩写而成,相关写法如下:

div {
  offset: offset-path;
  offset: offset-path offset-distance; 
  offset: offset-path offset-distance offset-rotate;
  offset: offset-path offset-distance offset-rotate / offset-anchor; /* offset-anchor 前方要用斜線分隔  */
}

下方范例会使用四个 div 呈现不同的写法。

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

<!-- HTML 程式碼-->
<div class="a">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="b">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="c">
  <svg viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>
<div class="d">
  <svg id="s" viewBox="0 0 300 300"><circle fill="none" stroke="#000" stroke-miterlimit="10" cx="150" cy="150" r="100"/></svg>
  <div></div>
</div>

<!-- CSS 程式碼 -->
<style>
  div {
    position: relative;
    width: 300px;
    height: 200px;
    float: left;
    margin: 20px -20px;
  }
  div div {
    top: 0;
    left: 0;
    position: absolute;
    width: 0;
    height: 0;
    float: none;
    border-width: 20px 0 20px 100px;
    border-color: #0000 #0000 #0000 #f00b;
    border-style: solid;
    animation: oxxo 2s infinite linear;
  }
  .a div {offset: circle(100px at 150px 150px);}
  .b div {offset: circle(100px at 150px 150px) 45deg;}
  .c div {offset: circle(100px at 150px 150px) 45deg / left;}
  .d div {offset: circle(100px at 150px 150px) auto 90deg / right;}
  @keyframes oxxo {
    0% {offset-distance: 0%;}
    100% {offset-distance: 100%;}
  }
</style>

CSS 教学 - 自订动画路径 offset - offset 动画路径缩写格式

小结

透过动画路径的做法,可以让原本不容易控制位置的动画效果,突然变成可以很简单的操控移动的方向和位置,如果手边有些 CSS 动画需要实作,建议可以开始使用自订动画路径的方式,创造出更加吸引眼球的动画效果。

阅读更多:CSS 动画 animation、CSS animation 触发事件、CSS 多重动画的权重与顺序

意见反馈

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