搜索

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

Icon 图示形状变换

这篇教学会使用虚拟元素和 clip-path 这两种方式,制作六种不同的 ICON 图示形状,并搭配 CSS 动画让这些图示形状互相变换。

快速导览:

CSS 教学 - CSS ICON 图示形状变换

使用虚拟元素产生不同的图示形状

使用虚拟元素 before 和 after 是制作 ICON 图示最简单的方式,只要运用背景色、宽度高度、定位,甚至在搭配一些边框、transform 旋转或阴影,就能产生更多图示形状,下方范例会展示六种图示形状。

参考:虚拟元素选择器 ( 伪元素 )、定位 position、边框 border ( 样式、粗细、颜色

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

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


<!-- CSS 程式碼 -->
<style>
  .icon {
    --w: 150px;            /* 使用變數方便後續計算 */
    position: relative;
    width: var(--w);
    height: var(--w);
    background: #f55;
    border-radius: 50%;   /* 使用圓角讓底圖變成圓形 */
    margin: 20px 10px;
    float: left;
  }
  /* 虛擬元素共用樣式 */
  .icon::before, .icon:after {
    position: absolute;
    content: "";
    z-index: 2;
    background: #fff;
  }
  /* 第一組正方形圖案,由兩個虛擬元素組成,目的在於之後要變化時更有彈性 */
  .icon::before {
    width: 25%;
    height: 50%;
    top: 25%;
    left: 25%;
  }
  .icon::after {
    width: 25%;
    height: 50%;
    top: 25%;
    right: 25%;
  }
  /* 第二組圓形圖案,由左右兩個半圓組成,寬高和第一組都相同,只是運用了不同半徑的圓角樣式 */
  .icon.a::before {
    border-radius: 200% 0 0 200%/ 100% 0 0 100%;
  }
  .icon.a::after {
    border-radius: 0 200% 200% 0/ 0 100% 100% 0;
  }
  /* 第三組圓形圖案,和第一組類似,差別在於左右兩半長方形的寬度不同 */
  .icon.b::before {
    width: 18%;
    height: 50%;
  }
  .icon.b::after {
    width: 18%;
    height: 50%;
  }
  /* 第四組三條槓圖案,運用旋轉 90 度和改變位置,加上陰影的方式產生第三條槓 */
  .icon.c::before {
    width: 12%;
    height: 50%;
    top: 5%;
    left: 45%;
    transform: rotateZ(90deg);
    box-shadow: #fff calc(var(--w)*0.2) 0 0;
  }
  .icon.c::after {
    width: 12%;
    height: 50%;
    top: 45%;
    right: 43%;
    transform: rotateZ(90deg);
  }
  /* 第五組叉叉圖案,透過旋轉和定位,產生叉叉圖案 */
  .icon.d::before {
    top: 20%;
    left: 40%;
    width: 18%;
    height: 60%;
    transform: rotateZ(45deg);
  }
  .icon.d::after {
    top: 20%;
    right: 40%;
    width: 18%;
    height: 60%;
    transform: rotateZ(-45deg);
  }

  /* 第六組三角形圖案,透過旋轉和定位,並搭配邊框製作三角形,組合後就會是完整的三角形 */
  .icon.e::before {
    top: 10%;
    left: 42%;
    width: 0;
    height: 0;
    background: #fff0;
    border-style: solid;
    border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
    border-color: #fff0 #fff #ffff #fff0;
    transform: rotateZ(90deg);
  }
  .icon.e::after {
    top: 40%;
    right: 28%;
    width: 0;
    height: 0;
    background: #fff0;
    border-style: solid;
    border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
    border-color: #fff0 #fff0 #ffff #fff;
    transform: rotateZ(90deg);
  }
</style>

CSS 教学 - CSS ICON 图示形状变换 - 使用虚拟元素产生不同的图示形状

使用虚拟元素产生不同的图示形状动画

如果要使用虚拟元素制作“动画效果”会稍微麻烦一点,必须要特别注意动画影格进度中“固定”与“改变”的样式,下方范例会将上述的静态图示,改成动态变化的效果。

参考:CSS 动画 animation

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

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


<!-- CSS 程式碼 -->
<style>
  .icon {
    --w: 250px;
    position: relative;
    width: var(--w);
    height: var(--w);
    background: #f55;
    border-radius: 50%;
    margin: 50px;
  }
  .icon::before, .icon:after {
    position: absolute;
    content: "";
    z-index: 2;
    background: #fff;
    border-style: solid;
    border-width: 0;
    border-color: #fff;
  }
  .icon::before {animation: oxxo-b 12s infinite;}
  .icon::after {animation: oxxo-a 12s infinite;}

  @keyframes oxxo-b {
    /* 第一組,正方形 */
    0%, 100% {
      width: 25%;
      height: 50%;
      top: 25%;
      left: 25%;
      background: #fff;
      box-shadow: #fff 0 0 0;
      border-width: 0;
      border-color: #fff0;
      transform: rotateZ(0);
    }
    13% {
      border-radius: 0;
    }  
    /* 第二組,圓形 */
    16.5% {
      border-radius: 200% 0 0 200%/ 100% 0 0 100%;
    }      
    29.5% {
      width: 25%;
      height: 50%;
      top: 25%;
      left: 25%;
      border-radius: 200% 0 0 200%/ 100% 0 0 100%;
    } 
    /* 第三組,兩條槓 */
    33% {
      width: 18%;
      height: 50%;
      top: 25%;
      left: 25%;
      border-radius: 0;
    } 
    46% {
      width: 18%;
      height: 50%;
      top: 25%;
      left: 25%;
      transform: rotateZ(0);
      box-shadow: #fff 0 0 0;
    }
    /* 第四組,三條槓 */
    49.5% {
      width: 12%;
      height: 50%;
      top: 5%;
      left: 45%;
      transform: rotateZ(90deg);
      box-shadow: #fff calc(var(--w)*0.2) 0 0;
    }
    62.5% {
      width: 12%;
      height: 50%;
      top: 5%;
      left: 45%;
      transform: rotateZ(90deg);
      box-shadow: #fff calc(var(--w)*0.2) 0 0;
    }
    /* 第五組,叉叉 */
    66% {
      top: 20%;
      left: 40%;
      width: 18%;
      height: 60%;
      box-shadow: #fff 0 0 0;
      transform: rotateZ(45deg);
    }
    79% {
      top: 20%;
      left: 40%;
      width: 18%;
      height: 60%;
      background: #fff;
      box-shadow: #fff 0 0 0;
      border-width: 0;
      border-color: #fff0;
      transform: rotateZ(45deg);
    }
    /* 第六組,三角形 */
    82.5% {
      top: 10%;
      left: 42%;
      width: 0;
      height: 0;
      background: #fff0;
      border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
      border-color: #fff0 #fff #ffff #fff0;
      transform: rotateZ(90deg);
    }
    95.5% {
      top: 10%;
      left: 42%;
      width: 0;
      height: 0;
      background: #fff0;
      border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
      border-color: #fff0 #fff #ffff #fff0;
      transform: rotateZ(90deg);
    }
    
  }
  @keyframes oxxo-a {
    /* 第一組,正方形 */
    0%, 100% {
      width: 25%;
      height: 50%;
      top: 25%;
      right: 25%;
      background: #fff;
      box-shadow: #fff 0 0 0;
      border-width: 0;
      border-color: #fff0;
    }  
    13% {
      border-radius: 0;
    } 
    /* 第二組,圓形 */
    16.5% {
      border-radius: 0 200% 200% 0/ 0 100% 100% 0;
    } 
    29.5% {
      width: 25%;
      height: 50%;
      top: 25%;
      right: 25%;
      border-radius: 0 200% 200% 0/ 0 100% 100% 0;
    } 
    /* 第三組,兩條槓 */
    33% {
      width: 18%;
      height: 50%;
      top: 25%;
      right: 25%;
      border-radius: 0;
    }
    46% {
      width: 18%;
      height: 50%;
      top: 25%;
      right: 25%;
      transform: rotateZ(0);
    }
    /* 第四組,三條槓 */
    49.5% {
      width: 12%;
      height: 50%;
      top: 45%;
      right: 43%;
      transform: rotateZ(90deg);
    }
    62.5% {
      width: 12%;
      height: 50%;
      top: 45%;
      right: 43%;
      transform: rotateZ(90deg);
    }
    /* 第五組,叉叉 */
    66% {
      top: 20%;
      right: 40%;
      width: 18%;
      height: 60%;
      transform: rotateZ(-45deg);
    }  
    79% {
      top: 20%;
      right: 40%;
      width: 18%;
      height: 60%;
      background: #fff;
      box-shadow: #fff 0 0 0;
      border-width: 0;
      border-color: #fff0;
      transform: rotateZ(-45deg);
    }
    /* 第六組,三角形 */
    82.5% {
      top: 40%;
      right: 28%;
      width: 0;
      height: 0;
      background: #fff0;
      border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
      border-color: #fff0 #fff0 #ffff #fff;
      transform: rotateZ(90deg);
    }
    95.5% {
      top: 40%;
      right: 28%;
      width: 0;
      height: 0;
      background: #fff0;
      border-width: calc(var(--w)*0.25) calc(var(--w)*0.15);
      border-color: #fff0 #fff0 #ffff #fff;
      transform: rotateZ(90deg);
    }
  }
</style>

CSS 教学 - CSS ICON 图示形状变换 - 使用虚拟元素产生不同的图示形状动画

使用 clip-path 产生不同的图示形状

相较于虚拟元素需要不断计算位置尺寸才能组合出特定形状,使用 clip-path 就能快速产生不同的图示形状,程序码也会减少非常多,下方范例会运用 polygon 的做法,搭配圆角半径,实作六种不同的图示形状。

参考:CSS 裁切路径 clip-path

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

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

<!-- CSS 程式碼 -->
<style>
  .icon {
    --w: 150px;
    position: relative;
    width: var(--w);
    height: var(--w);
    background: #f55;
    border-radius: 50%;
    margin: 20px 10px;
    float: left;
  }
  /* 預設全部都是正方形 */
  .icon::before {
    position: absolute;
    content: "";
    z-index: 2;
    background: #fff;
    width: 50%;
    height: 50%;
    top: 25%;
    left: 25%;
  }
  /* 圓形 */
  .b::before {
    border-radius: 50%;
  }
  /* 兩條槓 */
  .c::before {
    border-radius: 0;
    clip-path: polygon(0 0, 40% 0, 40% 50%, 60% 50%, 60% 0, 100% 0,
                100% 100%, 60% 100%, 60% 50%, 40% 50%, 40% 100%,0 100%);
  }
  /* 三條槓 */
  .d::before {
    border-radius: 0;
    clip-path: polygon(0 0, 100% 0, 100% 25%, 0% 25%,
                      0 37.5%, 100% 37.5%, 100% 62.5%, 0 62.5%,
                      0 75%, 100% 75%, 100% 100%, 0 100%);
  }
  /* 叉叉 */
  .e::before {
    border-radius: 0;
    clip-path: polygon(20% 0%, 50% 30%, 80% 0%, 100% 20%, 70% 50%, 
      100% 80%, 80% 100%, 50% 70%,20% 100%, 0% 80%, 30% 50%, 0% 20%);
  }
  /* 三角形 */
  .f::before {
    border-radius: 0;
    clip-path: polygon(20% 0, 20% 0, 20% 0, 20% 0,
                      100% 50%, 100% 50%, 100% 50%, 100% 50%,
                      20% 100%,20% 100%,20% 100%,20% 100%);
  }
</style>

CSS 教学 - CSS ICON 图示形状变换 - 使用 clip-path 产生不同的图示形状

使用 clip-path 产生不同的图示形状动画

能够运用 clip-path 产生形状后,就能将这些形状组合成动画效果,但需要自行调整不同节点的位置,使其更符合动画的过程,下方范例会让六种形状互相变换。

参考:CSS 动画 animation

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

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

<!-- CSS 程式碼 -->
<style>
  .icon {
    --w: 300px;
    position: relative;
    width: var(--w);
    height: var(--w);
    background: #f55;
    border-radius: 50%;
    margin: 50px;
  }
  .icon::before {
    position: absolute;
    content: "";
    z-index: 2;
    background: #fff;
    width: 50%;
    height: 50%;
    top: 25%;
    left: 25%;
    animation: oxxo 12s infinite linear;
  }

  @keyframes oxxo {
    0%, 100% {
      border-radius: 0;
      clip-path: polygon(0 0, 0 0, 0 0,
                        100% 0, 100% 0, 100% 0,
                        100% 0100%, 100% 100%, 100% 100%,
                        0 100%, 0 100%, 0 100%);
    }
    13% {
      border-radius: 0;
      clip-path: polygon(0 0, 0 0, 0 0,
                        100% 0, 100% 0, 100% 0,
                        100% 0100%, 100% 100%, 100% 100%,
                        0 100%, 0 100%, 0 100%);
    }
    
    16.5% {
      border-radius: 50%;
      clip-path: polygon(0 0, 0 0, 0 0,
                        100% 0, 100% 0, 100% 0,
                        100% 0100%, 100% 100%, 100% 100%,
                        0 100%, 0 100%, 0 100%);
    }
    29.5% {
      border-radius: 50%;
      clip-path: polygon(0 0, 0 0, 0 0,
                        100% 0, 100% 0, 100% 0,
                        100% 0100%, 100% 100%, 100% 100%,
                        0 100%, 0 100%, 0 100%);
    }
    
    33% {
      border-radius: 0;
      clip-path: polygon(0 0, 40% 0, 40% 50%, 60% 50%, 60% 0, 100% 0,
                  100% 100%, 60% 100%, 60% 50%, 40% 50%, 40% 100%,0 100%);
    }
    46% {
      border-radius: 0;
      clip-path: polygon(0 0, 40% 0, 40% 50%, 60% 50%, 60% 0, 100% 0,
                  100% 100%, 60% 100%, 60% 50%, 40% 50%, 40% 100%,0 100%);
    }
    
    49.5% {
      border-radius: 0;
      clip-path: polygon(0 0, 100% 0, 100% 25%, 0% 25%,
                        0 37.5%, 100% 37.5%, 100% 62.5%, 0 62.5%,
                        0 75%, 100% 75%, 100% 100%, 0 100%);
    }
    62.5% {
      border-radius: 0;
      clip-path: polygon(0 0, 100% 0, 100% 25%, 0% 25%,
                        0 37.5%, 100% 37.5%, 100% 62.5%, 0 62.5%,
                        0 75%, 100% 75%, 100% 100%, 0 100%);
    }
    
    66% {
      border-radius: 0;
      clip-path: polygon(20% 0%, 50% 30%, 80% 0%, 100% 20%, 70% 50%, 
        100% 80%, 80% 100%, 50% 70%,20% 100%, 0% 80%, 30% 50%, 0% 20%);
    }
    79% {
      border-radius: 0;
      clip-path: polygon(20% 0%, 50% 30%, 80% 0%, 100% 20%, 70% 50%, 
        100% 80%, 80% 100%, 50% 70%,20% 100%, 0% 80%, 30% 50%, 0% 20%);
    }
    
    82.5% {
      border-radius: 0;
      clip-path: polygon(20% 0, 20% 0, 20% 0, 20% 0,
                        100% 50%, 100% 50%, 100% 50%, 100% 50%,
                        20% 100%,20% 100%,20% 100%,20% 100%);
    }
    95.5% {
      border-radius: 0;
      clip-path: polygon(20% 0, 20% 0, 20% 0, 20% 0,
                        100% 50%, 100% 50%, 100% 50%, 100% 50%,
                        20% 100%,20% 100%,20% 100%,20% 100%);
    }
  }
</style>

CSS 教学 - CSS ICON 图示形状变换 - 使用 clip-path 产生不同的图示形状动画

小结

透过 CSS 制作图示形状其实不困难,通常都是花费许多时间在“绘制形状”,只要知道形状的节点或位置,就可以轻松地透过 CSS 控制形状变换啰。

意见反馈

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