CSS 备忘清单

这是一份关于 CSS 优点的快速参考备忘单,列出了选择器语法、属性、单位和其他有用的信息

入门

介绍

CSS 功能丰富,不仅仅是布局页面

外部样式表

  1. <link
  2. href="./path/to/stylesheet/style.css"
  3. rel="stylesheet"
  4. type="text/css"
  5. >

内部样式表

  1. <style>
  2. body {
  3. background-color: linen;
  4. }
  5. </style>

内联样式

  1. <h2 style="text-align: center;">
  2. 居中文本
  3. </h2>
  4. <p style="color: blue; font-size: 18px;">
  5. 蓝色,18像素文本
  6. </p>

添加 class 类

  1. <div class="classname"></div>
  2. <div class="class1 ... classn"></div>

支持一个元素上的多个类

!important

  1. .post-title {
  2. color: blue !important;
  3. }

覆盖所有以前的样式规则

选择器

  1. h1 { }
  2. #job-title { }
  3. div.hero { }
  4. div > p { }

查看: CSS 选择器

文本颜色

  1. color: #2a2aff;
  2. color: green;
  3. color: rgb(34, 12, 64, 0.6);
  4. color: hsla(30 100% 50% / 0.6);

查看: Colors

背景

  1. background-color: blue;
  2. background-image: url("nyan-cat.gif");
  3. background-image: url("../image.png");

查看: Backgrounds

字体

  1. .page-title {
  2. font-weight: bold;
  3. font-size: 30px;
  4. font-family: "Courier New";
  5. }

查看: Fonts

定位

  1. .box {
  2. position: relative;
  3. top: 20px;
  4. left: 20px;
  5. }

另见: Position

动画

  1. animation: 300ms linear 0s infinite;
  2. animation: bounce 300ms linear infinite;

查看: Animation

注释

  1. /* 这是一行注释 */
  2. /* 这是
  3. 多行注释 */

Flex 布局

  1. div {
  2. display: flex;
  3. justify-content: center;
  4. }
  5. div {
  6. display: flex;
  7. justify-content: flex-start;
  8. }

查看: Flexbox | Flex Tricks

Grid 布局

  1. #container {
  2. display: grid;
  3. grid: repeat(2, 60px) / auto-flow 80px;
  4. }
  5. #container > div {
  6. background-color: #8ca0ff;
  7. width: 50px;
  8. height: 50px;
  9. }

查看: Grid Layout

变量和计数器

  1. counter-set: subsection;
  2. counter-increment: subsection;
  3. counter-reset: subsection 0;
  4. :root {
  5. --bg-color: brown;
  6. }
  7. element {
  8. background-color: var(--bg-color);
  9. }

查看: 动态内容

CSS 选择器

示例

组选择器

  1. h1, h2 {
  2. color: red;
  3. }

链选择器

  1. h3.section-heading {
  2. color: blue;
  3. }

属性选择器

  1. div[attribute="SomeValue"] {
  2. background-color: red;
  3. }

第一个子选择器

  1. p:first-child {
  2. font-weight: bold;
  3. }

无子选择器

  1. .box:empty {
  2. background: lime;
  3. height: 80px;
  4. width: 80px;
  5. }

基础

选择器 说明
* 所有元素
div 所有 div 标签
.classname 具有类的所有元素
#idname 带 ID 的元素
div,p 所有 div 和段落
#idname * #idname 中的所有元素

另见: 元素 / / ID / 通配 选择器

组合器

选择器 说明
div.classname 具有特定类名的 div
div#idname 具有特定 ID 的 div
div p div 中的段落
div > p div 子节点中的所有 P 标签
div + p div 之后的 P 标签
div ~ p div 前面的 P 标签

另见: 相邻兄弟 / 通用兄弟 / 选择器

属性选择器

选择器 说明
a[target] 带有 target 属性 #
a[target="_blank"] 在新标签中打开 #
a[href^="/index"] /index 开头 #
`[class\ =”chair”]` chair开头 #
[class*="chair"] 包含chair #
[title~="chair"] 包含单词 chair #
a[href$=".doc"] .doc 结尾 #
[type="button"] 指定类型 #

另见: 属性选择器

用户操作伪类

选择器 说明
a:link 链接正常 #
a:active 链接处于点击状态 #
a:hover 鼠标悬停链接 #
a:visited 访问链接 #

  1. /* 未访问链接 */
  2. a:link { color: blue; }
  3. /* 已访问链接 */
  4. a:visited { color: purple; }
  5. /* 用户鼠标悬停 */
  6. a:hover { background: yellow; }
  7. /* 激活链接 */
  8. a:active { color: red; }

伪类

选择器 说明
p::after 在 p 之后添加内容 #
p::before 在 p 之前添加内容 #
p::first-letter p中的第一个字母 #
p::first-line p 中的第一行 #
::selection 由用户选择 #
::placeholder 占位符 属性 #
:root 文档根元素 #
:target 突出显示活动锚点 #
div:empty 没有子元素的元素 #
p:lang(en) 带有 en 语言属性的 P #
:not(span) 不是跨度的元素 #
:host shadowDOM 中选择自定义元素 #
::backdrop 处于全屏模式的元素样式 #
::marker li 项目符号或者数字 #
::file-selector-button type=”file” input 按钮 #

输入伪类

选择器 说明
input:checked 检查 input #
input:disabled 禁用 input #
input:enabled 启用的 input #
input:default 有默认值的元素 #
input:blank 空的输入框 #
input:focus input 有焦点 #
input:in-range 范围内的值 #
input:out-of-range input 值超出范围 #
input:valid input 有效值 #
input:invalid input 无效值 #
input:optional 没有必需的属性 #
input:required 带有必需属性的 input #
input:read-only 具有只读属性 #
input:read-write 没有只读属性 #
input:indeterminate 带有 indeterminate 状态 #

结构伪类

选择器 说明
p:first-child 第一个孩子 #
p:last-child 最后一个孩子 #
p:first-of-type 首先是某种类型 #
p:last-of-type 某种类型的最后一个 #
p:nth-child(2) 其父母的第二个孩子 #
p:nth-child(3n42) Nth-child(an + b) 公式 #
p:nth-last-child(2) 后面的二孩 #
p:nth-of-type(2) 其父级的第二个 p #
p:nth-last-of-type(2) …从后面 #
p:only-of-type 其父级的唯一性 #
p:only-child 其父母的唯一孩子 #
:is(header, div) p 可以选择的元素 #
:where(header, div) p :is 相同 #
a:has(> img) 包含 img 元素的 a 元素 #
::first-letter 第一行的第一个字母 #
::first-line 第一行应用样式 #

CSS 字体

属性

属性 说明
font-family: 字体族名或通用字体族名 #
font-size: 字体的大小 #
letter-spacing: 文本字符的间距 #
line-height: 多行文本间距 #
font-weight: 粗细程度 #
font-style: 字体样式 #
text-decoration: 文本的修饰线外观 #
text-align: 相对它的块父元素对齐 #
text-transform: 指定文本大小写 #

另见: Font

速记

  1. font: italic 400 14px / 1.5 sans-serif
  2. ┈┈┬┈┈┈ ┈┬┈ ┈┬┈┈ ┈┬┈ ┈┬┈┈┈┈┈┈┈┈
  3. 样式 粗细 大小(必需的) 行高 字体(必需的)

示例

  1. font-family: Arial, sans-serif;
  2. font-size: 12pt;
  3. letter-spacing: 0.02em;

大小写

  1. div {
  2. /* 首字母大写 Hello */
  3. text-transform: capitalize;
  4. /* 字母大写 HELLO */
  5. text-transform: uppercase;
  6. /* 字母小写 hello */
  7. text-transform: lowercase;
  8. }

@font-face

  1. @font-face {
  2. font-family: 'Glegoo';
  3. src: url('../Glegoo.woff');
  4. }

CSS 颜色

命名颜色

  1. color: red;
  2. color: orange;
  3. color: tan;
  4. color: rebeccapurple;

更多标准颜色名称

十六进制颜色

  1. color: #090;
  2. color: #009900;
  3. color: #090a;
  4. color: #009900aa;

rgb() 颜色

  1. color: rgb(34, 12, 64, 0.6);
  2. color: rgba(34, 12, 64, 0.6);
  3. color: rgb(34 12 64 / 0.6);
  4. color: rgba(34 12 64 / 0.3);
  5. color: rgb(34.0 12 64 / 60%);
  6. color: rgba(34.6 12 64 / 30%);

HSL 颜色

  1. color: hsl(30, 100%, 50%, 0.6);
  2. color: hsla(30, 100%, 50%, 0.6);
  3. color: hsl(30 100% 50% / 0.6);
  4. color: hsla(30 100% 50% / 0.6);
  5. color: hsl(30.0 100% 50% / 60%);
  6. color: hsla(30.2 100% 50% / 60%);

其它

  1. color: inherit;
  2. color: initial;
  3. color: unset;
  4. color: transparent;
  5. color: currentcolor; /* 关键字 */

全局值

  1. /* 全局值 */
  2. color: inherit;
  3. color: initial;
  4. color: unset;

CSS 背景

属性

属性 说明
background: (速记)
background-color: 查看: Colors
background-image: 一个或者多个背景图像
background-position: 背景图片设置初始位置
background-size: 背景图片大小
background-clip: 背景(图片或颜色)是否延伸到边框、内边距盒子、内容盒子下面
background-repeat: 图像重复方式
background-attachment: scroll/fixed/local

速记

  1. background: #ff0 url(a.jpg) left top / 100px auto no-repeat fixed;
  2. #abc url(b.png) center center / cover repeat-x local;
  3. ┈┬┈┈ ┈┬┈┈┈┈┈┈┈ ┈┬┈┈ ┈┬┈ ┈┈┬┈┈┈┈┈┈┈ ┈┈┬┈┈┈┈┈┈ ┈┈┬┈┈┈
  4. 颜色 图片 位置x 位置x 图片大小 图像重复方式 位置是在视口内固定

示例

  1. background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
  2. background: url(img_man.jpg) no-repeat center;
  3. background: rgb(2,0,36);
  4. background: linear-gradient(90deg, rgba(2,0,36,1) 0%,
  5. rgba(13,232,230,1) 35%,
  6. rgba(0,212,255,1) 100%);

CSS 盒子模型

最大值/最小值

  1. .column {
  2. max-width: 200px; /* 最大宽度 200 像素 */
  3. width: 500px; /* 宽度 500 像素 */
  4. }

另见: max-width / min-width / max-height / min-height

边距/补白

  1. .block-one {
  2. margin: 20px; /* 边距 20 像素 */
  3. padding: 10px; /* 补白 10 像素 */
  4. }

另见: 边距(margin) / 补白(padding)

Box-sizing

  1. .container {
  2. /* 设置的边框和补白的值是包含在 width 内的 */
  3. box-sizing: border-box;
  4. }

另见: box-sizing

能见度

  1. .invisible-elements {
  2. visibility: hidden; /* 隐藏元素 */
  3. }

另见: Visibility

Auto 关键字

  1. div {
  2. /* 览器自己选择一个合适的外边距 */
  3. margin: auto;
  4. }

另见: 边距(margin)

溢出(Overflow)

  1. .small-block {
  2. /* 浏览器总是显示滚动条 */
  3. overflow: scroll;
  4. }

另见: 溢出(overflow)

CSS 动画

速记

  1. animation: bounce 300ms linear 100ms infinite alternate-reverse both reverse
  2. ┈┬┈┈ ┈┬┈┈┈ ┈┬┈┈┈┈ ┈┈┬┈┈ ┈┈┈┬┈┈┈┈ ┈┈┬┈┈┈┈┈┈┈┈┈┈┈┈┈┈ ┈┈┬┈┈┈ ┈┈┬┈┈┈
  3. 动画名 动画时间 缓动函数 延迟 运行的次数 动画是否反向播放 如何将样式应用于其目标 是否运行或者暂停

属性

属性 说明
animation: (速记)
animation-name: 动画名 #
animation-duration: 动画周期的时长 #
animation-timing-function: 缓动函数 #
animation-delay: 延迟 #
animation-iteration-count: 运行的次数 #
animation-direction: 动画是否反向播放 #
animation-fill-mode: 如何将样式应用于其目标 #
animation-play-state: 是否运行或者暂停 #

另见: 动画(Animation)

示例

  1. /* @keyframes duration | timing-function | delay |
  2. iteration-count | direction | fill-mode | play-state | name */
  3. animation: 3s ease-in 1s 2 reverse both paused slidein;
  4. /* @keyframes duration | timing-function | delay | name */
  5. animation: 3s linear 1s slidein;
  6. /* @keyframes duration | name */
  7. animation: 3s slidein;
  8. animation: 4s linear 0s infinite alternate move_eye;
  9. animation: bounce 300ms linear 0s infinite normal;
  10. animation: bounce 300ms linear infinite;
  11. animation: bounce 300ms linear infinite alternate-reverse;
  12. animation: bounce 300ms linear 2s infinite alternate-reverse forwards normal;

Javascript 事件

  1. .one('webkitAnimationEnd oanimationend msAnimationEnd animationend')

CSS Flexbox

简单实例

  1. .container {
  2. display: flex;
  3. }
  1. .container > div {
  2. flex: 1 1 auto;
  3. }

容器

```css {2-3,5-8,10-11,13-16,18-23} .container { display: flex; display: inline-flex;

flex-direction: row; / ltr - 行(左向右) ▶ / flex-direction: row-reverse; / rtl - 行(右向左) ◀ / flex-direction: column; / top-bottom ▼ / flex-direction: column-reverse; / bottom-top ▲ /

flex-wrap: nowrap; / 摆放到一行 / flex-wrap: wrap; / 被打断到多个行中 /

align-items: flex-start; / 垂直对齐 - 顶部 / align-items: flex-end; / 垂直对齐 - 底部 / align-items: center; / 垂直对齐 - 中间 / align-items: stretch; / 所有人都一样的高度 (默认) /

justify-content: flex-start; / [◀◀◀ ] / justify-content: center; / [ ■■■ ] / justify-content: flex-end; / [ ▶▶▶] / justify-content: space-between; / [◀ ■ ▶] / justify-content: space-around; / [ ■ ■ ■ ] / justify-content: space-evenly; / [ ■ ■ ■ ] / }

  1. ### 子元素
  2. ```css
  3. .container > div {
  4. /* 这个: */
  5. flex: 1 0 auto;
  6. /* 相当于这个: */
  7. flex-grow: 1;
  8. flex-shrink: 0;
  9. flex-basis: auto;
  10. order: 1;
  11. align-self: flex-start; /* left */
  12. margin-left: auto; /* right */
  13. }

justify-content

  1. justify-content: flex-start | flex-end | center | space-between

flex-start:左对齐(默认值)

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ┆╭┈┈╮╭┈╮╭┈┈┈╮
  3. ┆╰┈┈╯╰┈╯╰┈┈┈╯
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

flex-end:右对齐

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ╭┈┈╮╭┈╮╭┈┈┈╮┆
  3. ╰┈┈╯╰┈╯╰┈┈┈╯┆
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

center: 居中

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ╭┈┈╮╭┈╮╭┈┈┈╮
  3. ╰┈┈╯╰┈╯╰┈┈┈╯
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

space-between:两端对齐,项目之间的间隔都相等

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ┆╭┈┈╮ ╭┈╮ ╭┈┈┈╮┆
  3. ┆╰┈┈╯ ╰┈╯ ╰┈┈┈╯┆
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

space-around:每个项目两侧的间隔相等,项目之间的间隔比项目与边框的间隔大一倍

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ╭┈┈╮ ╭┈╮ ╭┈┈┈╮
  3. ╰┈┈╯ ╰┈╯ ╰┈┈┈╯
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

上面示例,假设主轴为从左到右

flex-wrap

  1. flex-wrap: nowrap | wrap | wrap-reverse;

nowrap:不换行(默认)

  1. 1╮╭2╮╭3╮╭4╮╭5╮╭6╮╭7╮╭8╮╭9╮╭10
  2. ╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈┈╯

wrap:换行,第一行在 上方

  1. 1┈╮ 2┈╮ 3┈╮ 4┈╮ 5┈╮ 6┈╮ 7┈╮
  2. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
  3. 8┈╮ 9┈╮ 10
  4. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯

wrap-reverse:换行,第一行在 下方

  1. 8┈╮ 9┈╮ 10
  2. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
  3. 1┈╮ 2┈╮ 3┈╮ 4┈╮ 5┈╮ 6┈╮ 7┈╮
  4. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯

项目都排在一条线(又称”轴线”)上

flex-direction

  1. flex-direction: row | row-reverse | column | column-reverse;

  1. ╭┈┈╮ ╭┈┈╮
  2. ╰┈┈╯ ╰┈┈╯
  3. ╭┈┈╮ ╭┈┈╮
  4. ╰┈┈╯ ╰┈┈╯ ┈┈┈┈┈┈┈┈┈┈┈▶ ◀┈┈┈┈┈┈┈┈┈┈┈
  5. ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
  6. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
  7. ┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈
  8. column-reverse column row row-reverse

属性决定主轴的方向(即项目的排列方向)

align-items

  1. align-items: flex-start | flex-end | center | baseline | stretch;

  1. flex-start(起点对齐) flex-end(终点对齐)
  2. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  3. ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
  4. ╰┈┈╯ ╭┈┈╮
  5. ╰┈┈╯ ╰┈┈╯ ╭┈┈╮ ╭┈┈╮
  6. ╰┈┈╯ ╭┈┈╮
  7. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
  8. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  9. center(中点对齐) stretch(占满整个容器的高度)
  10. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  11. ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
  12. ╭┈┈╮ ╭┈┈╮
  13. ╭┈┈╮
  14. ╰┈┈╯
  15. ╰┈┈╯ ╰┈┈╯
  16. ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
  17. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  18. baseline(第一行文字的基线对齐)
  19. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  20. ╭┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈╮
  21. ╭┈┈┈┈╮ ╭┈┈┈┈╮ ╭┈┈┈┈╮╭┈┈┈┈╮┆
  22. text text text text text┆┆text┆┆
  23. ╰┈┈┈┈╯ ╰┈┈┈┈╯┆ ┆┆
  24. ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯┆
  25. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

align-content

  1. align-content: flex-start | flex-end | center | space-between | space-around | stretch;

  1. flex-start(起点对齐) flex-end(终点对齐)
  2. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  3. ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮
  4. ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮
  5. ╭┈┈┈╮╭╮ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯
  6. ╰┈┈┈╯╰╯ ╭┈┈┈╮╭╮
  7. ╰┈┈┈╯╰╯
  8. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  9. center(中点对齐) stretch(满整个交叉轴)
  10. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  11. ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮
  12. ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆┆ ┆┆ ┆┆┆┆
  13. ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯
  14. ╭┈┈┈╮╭╮ ╭┈┈┈╮╭╮╭┈╮
  15. ╰┈┈┈╯╰╯ ┆┆┆┆
  16. ╰┈┈┈╯╰╯╰┈╯
  17. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  18. space-between(两端对齐) space-around(均匀分布项目)
  19. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  20. ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮
  21. ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮
  22. ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯
  23. ╭┈┈╮
  24. ╭┈┈╮ ╰┈┈╯
  25. ╰┈┈╯
  26. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

order

  1. .item {
  2. order: <integer>;
  3. }

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈╮
  2. 1┈╮ 1┈┈╮ 1┈╮ 2┈╮ 3┈┈┈┈┈┈╮ 2┈┈┈┈╮
  3. ╰┈┈╯ ╰┈┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈╯
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ 2┈┈┈┈╮
  5. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╰┈┈┈┈┈╯
  6. ╭-┈┈╮ ╭┈┈┈╮ ╭┈┈┈┈┈┈┈┈╮ ╭┈┈┈╮ 99┈┈┈╮
  7. ┆-1 1 2 5
  8. ╰┈┈┈╯ ╰┈┈┈╯ ╰┈┈┈┈┈┈┈┈╯ ╰┈┈┈╯ ╰┈┈┈┈┈╯
  9. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈╯

属性 order 定义项目的排列顺序。数值越小,排列越靠前,默认为 0

flex-grow

  1. .item {
  2. flex-grow: <number>; /* default 0 */
  3. }

  1. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  2. ╭┈┈1┈┈╮╭┈┈2┈┈╮╭┈┈1┈┈╮
  3. ╰┈┈┈┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈┈╯
  4. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  5. ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
  6. ╭┈1┈╮╭┈┈┈┈2┈┈┈┈╮╭┈1┈╮
  7. ╰┈┈┈╯╰┈┈┈┈┈┈┈┈┈╯╰┈┈┈╯
  8. ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

属性 flex-grow 定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大

CSS Flexbox 技巧

垂直中心

  1. .container {
  2. display: flex;
  3. }
  4. .container > div {
  5. width: 100px;
  6. height: 100px;
  7. margin: auto;
  8. }

垂直中心 (2)

  1. .container {
  2. display: flex;
  3. /* 垂直的 */
  4. align-items: center;
  5. /* 水平的 */
  6. justify-content: center;
  7. }

重新排序

  1. .container > .top {
  2. order: 1;
  3. }
  4. .container > .bottom {
  5. order: 2;
  6. }

移动布局

  1. .container {
  2. display: flex;
  3. flex-direction: column;
  4. }
  5. .container > .top {
  6. flex: 0 0 100px;
  7. }
  8. .container > .content {
  9. flex: 1 0 auto;
  10. }

一个固定高度的顶部栏和一个动态高度的内容区域

Table-like 像表格

  1. .container {
  2. display: flex;
  3. }
  4. /* 这里的“px”值只是建议的百分比 */
  5. .container > .checkbox { flex: 1 0 20px; }
  6. .container > .subject { flex: 1 0 400px; }
  7. .container > .date { flex: 1 0 120px; }

这会创建具有不同宽度的列,但会根据情况相应地调整大小

Vertical 垂直的

  1. .container {
  2. align-items: center;
  3. }

垂直居中所有项目

左和右

  1. .menu > .left { align-self: flex-start; }
  2. .menu > .right { align-self: flex-end; }

CSS Grid 网格布局

网格模板列

  1. #grid-container {
  2. display: grid;
  3. width: 100px;
  4. grid-template-columns: 20px 20% 60%;
  5. }

fr 相对单位

  1. .grid {
  2. display: grid;
  3. width: 100px;
  4. grid-template-columns: 1fr 60px 1fr;
  5. }

Grid Gap 网格间隙

  1. /* 行间距为 20px */
  2. /* 列之间的距离是 10px */
  3. #grid-container {
  4. display: grid;
  5. grid-gap: 20px 10px;
  6. }

CSS 网格行

CSS 语法:

  • grid-row: grid-row-start / grid-row-end;

实例

  1. .item {
  2. grid-row: 1 / span 2;
  3. }

CSS 块级网格

  1. #grid-container {
  2. display: block;
  3. }

CSS 内联级别网格

  1. #grid-container {
  2. display: inline-grid;
  3. }

CSS 网格行间隙

  1. grid-row-gap: length;

任何合法的长度值,例如 px%0 是默认值

CSS 网格区域

  1. .item1 {
  2. grid-area: 2 / 1 / span 2 / span 3;
  3. }

minmax() 函数

```css {.wrap} .grid { display: grid; grid-template-columns: 100px minmax(100px, 500px) 100px; }

  1. <!--rehype:className=wrap-text -->
  2. 定义了一个长宽范围的闭区间
  3. ### grid-row-start & grid-row-end
  4. CSS 语法:
  5. - grid-row-start: auto|row-line;
  6. - grid-row-end: auto|row-line|span n;
  7. #### 实例
  8. ```css
  9. grid-row-start: 2;
  10. grid-row-end: span 2;

对齐项目

  1. #container {
  2. display: grid;
  3. justify-items: center;
  4. grid-template-columns: 1fr;
  5. grid-template-rows: 1fr 1fr 1fr;
  6. grid-gap: 10px;
  7. }

CSS 网格模板区域

  1. .item {
  2. grid-area: nav;
  3. }
  4. .grid-container {
  5. display: grid;
  6. grid-template-areas:
  7. 'nav nav . .'
  8. 'nav nav . .';
  9. }

Justify Self

  1. #grid-container {
  2. display: grid;
  3. justify-items: start;
  4. }
  5. .grid-items {
  6. justify-self: end;
  7. }

网格项目位于行的右侧(末尾)

对齐项目

  1. #container {
  2. display: grid;
  3. align-items: start;
  4. grid-template-columns: 1fr;
  5. grid-template-rows: 1fr 1fr 1fr;
  6. grid-gap: 10px;
  7. }

CSS 动态内容

变量

定义 CSS 变量

  1. :root {
  2. --first-color: #16f;
  3. --second-color: #ff7;
  4. }

变量用法

  1. #firstParagraph {
  2. background-color: var(--first-color);
  3. color: var(--second-color);
  4. }

另见: CSS Variable

计数器

  1. /* Set "my-counter" to 0 */
  2. counter-set: my-counter;
  1. /* Increment "my-counter" by 1 */
  2. counter-increment: my-counter;
  1. /* Decrement "my-counter" by 1 */
  2. counter-increment: my-counter -1;
  1. /* Reset "my-counter" to 0 */
  2. counter-reset: my-counter;

另见: Counter set

使用计数器

  1. body { counter-reset: section; }
  2. h3::before {
  3. counter-increment: section;
  4. content: "Section." counter(section);
  5. }
  1. ol {
  2. counter-reset: section;
  3. list-style-type: none;
  4. }
  5. li::before {
  6. counter-increment: section;
  7. content: counters(section, ".") " ";
  8. }

Css 3 技巧

滚动条平滑

  1. html {
  2. scroll-behavior: smooth;
  3. }

点击我页面会平滑滚动到入门

忽略用作间距的换行符 \

  1. br + br {
  2. display: none;
  3. }

使用 :empty 隐藏空 HTML 元素

  1. :empty {
  2. display: none;
  3. }

CSS 重置

  1. html {
  2. box-sizing: border-box;
  3. }
  4. *, *::before, *::after {
  5. box-sizing: border-box;
  6. margin: 0;
  7. padding: 0;
  8. }

有助于在不同的浏览器之间强制样式一致性,并为样式元素提供干净的盒子

使用图像作为光标

  1. div {
  2. cursor: url('path-to-image.png'), url('path-to-fallback-image.png'), auto;
  3. /* 表情符号作为光标 */
  4. cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>🚀</text></svg>"), auto;
  5. }

文本溢出显示省略号

  1. .overflow-ellipsis {
  2. width: 200px;
  3. white-space: nowrap;
  4. overflow: hidden;
  5. text-overflow: ellipsis;
  6. }

一行文本截断显示省略号 (…)

将文本截断到特定的行数

  1. .overflow-truncate {
  2. display: -webkit-box;
  3. -webkit-box-orient: vertical;
  4. -webkit-line-clamp: 3;
  5. overflow: hidden;
  6. }

多行文本截断到特定的行数,末尾显示省略号 (…)

计算函数

  1. div {
  2. width: calc(100% - 30px);
  3. height: calc(100% - 30px);
  4. }

calc() CSS 函数允许您在指定 CSS 属性值时执行计算

粘性定位元素

  1. .sticky {
  2. position: sticky;
  3. top: 0;
  4. }

属性 sticky 能在滚动到顶部的位置固定住元素

使用带有空链接的属性选择器

  1. a[href^="http"]:empty::before {
  2. content: attr(href);
  3. }

如果你的 <a> 标签里面没有内容,将 href 的值作为内容展示

使用 :root 表示灵活类型

响应式布局中的字体大小应该能够根据每个视口进行调整,您可以使用 :root 根据视口高度和宽度计算字体大小

  1. :root {
  2. font-size: calc(1vw + 1vh + .5vmin);
  3. }

现在您可以根据 :root 计算的值使用根 em 单位:

  1. body {
  2. font: 1rem/1.6 sans-serif;
  3. }

另见