搜索

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

背景缩写 ( background )

透过 CSS 设定元素背景时,除了一个个的设定每种背景样式,也可使用“缩写格式”设定背景,这篇教学会介绍如何使用背景缩写格式 background 设定元素背景。

快速导览:

CSS 教学 - CSS background 背景缩写格式

教学影片

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

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

使用 background

background 可以设定元素的“背景颜色”和“背景图”,没有继承特性,使用 background 相当方便,可以不按照顺序使用背景图的样式,但因为部分样式属性的属性值会重复,所以有下列的几个注意事项:

  • 使用 background-size 和 background-position:
    • 只有一个数值 ( 长度数值 ) 代表 background-position 属性值。
    • 同时使用两个属性值,第一个是 background-position,第二个是background-size ,并使用“/”分隔。
  • 使用 background-origin 和 background-clip:
    • 只有一个属性值代表 background-origin。
    • 同时使用两个属性值,第二个是 background-clip。
  • 使用 background-color 背景色一定位于背景图下层。
  • 使用 background 可能会覆盖掉对应的背景图样式。
    • 其他背景样式也可能会覆盖 background 里对应的样式。
  • 不支援 background-blend-mode。

下方列出一些符合规范的 background 写法:

div {
  background: red;  
  /* 等同 */
  /* background-color: red */

  background: red url("圖片網址");
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */

  background: red url("圖片網址") space;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */

  background: red url("圖片網址") space padding-box;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */

  background: red url("圖片網址") space padding-box padding-box;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */

  background: red url("圖片網址") space padding-box padding-box top right;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */

  background: red url("圖片網址") space padding-box padding-box top right/50%;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */
  /* background-size: 50%; */

  background: red url("圖片網址") space padding-box padding-box top right/50% fixed;
  /* 等同 */
  /* background-color: red; */
  /* background-image: url("圖片網址"); */
  /* background-repeat: space; */
  /* background-origin: padding-box; */
  /* background-clip: padding-box; */
  /* background-position: top right; */
  /* background-size: 50%; */
  /* backgroud-attachment: fixed; */
}

使用 background 会根据权重规则,覆盖对应的样式或被对应的样式覆盖,下方的范例会使用 background 样式覆盖 background-size 样式 ( cover 变成 30% ),接着再透过 background-color 覆盖 background 里的背景色 ( 红色变成黄色 )。

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

<!-- HTML 程式碼 -->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 300px;
    height: 300px;
    border: 1px solid #000;
    background-size: cover;
    background: red top left/30% round url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png");
    background-color: yellow;
  }
</style>

CSS 教学 - 背景缩写格式 background -  background 覆盖对应的样式或被对应的样式覆盖

background 设定多重背景

background 也可以设定“多重背景”,使用方式与背景图的多重背景相同,使用“逗号 ,”分隔不同的背景,不同背景之间属性不会互相影响,例如第一张图使用 no-repeat 而第二张图没有设定,则第二张图会使用默认值 repeat,此外,使用时需要注意,只要某一项背景写错,就会发生样式错误而无法显示背景。

特别注意,“背景颜色只能出现一次”,如果多重背景的属性值组合中出现了背景色,这个属性值组合且必须出现在多重背景的“最后一项”。

下方的范例会使用 background 呈现多重背景的效果。

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

<!-- HTML 程式碼 -->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 300px;
    height: 300px;
    border: 1px solid #000;
    background: url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png") no-repeat center/60%, 
      top left/20% space url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png"),
      red top left/5% space url("https://steam.oxxostudio.tw/download/css/function-filter-demo.png");
  }
</style>

CSS 教学 - 背景缩写格式 background - background 设定多重背景

小结

背景缩写格式 background 是相当方便好用的样式,使用时只需要注意一些细节规则,就可以大幅减少背景图样式的程序码。

意见反馈

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