这是一份关于 CSS 优点的快速参考备忘单,列出了选择器语法、属性、单位和其他有用的信息
h1 { }
#job-title { }
div.hero { }
div > p { }
查看: CSS 选择器
color: #2a2aff;
color: green;
color: rgb(34, 12, 64, 0.6);
color: hsla(30 100% 50% / 0.6);
查看: Colors
background-color: blue;
background-image: url("nyan-cat.gif");
background-image: url("../image.png");
查看: Backgrounds
.page-title {
font-weight: bold;
font-size: 30px;
font-family: "Courier New";
}
查看: Fonts
.box {
position: relative;
top: 20px;
left: 20px;
}
另见: Position
animation: 300ms linear 0s infinite;
animation: bounce 300ms linear infinite;
查看: Animation
div {
display: flex;
justify-content: center;
}
div {
display: flex;
justify-content: flex-start;
}
查看: Flexbox | Flex Tricks
#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
查看: Grid Layout
counter-set: subsection;
counter-increment: subsection;
counter-reset: subsection 0;
:root {
--bg-color: brown;
}
element {
background-color: var(--bg-color);
}
查看: 动态内容
| 选择器 | 说明 |
|---|---|
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 类型的元素 # |
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 | 第一行应用样式 # |
.column {
max-width: 200px; /* 最大宽度 200 像素 */
width: 500px; /* 宽度 500 像素 */
}
另见: max-width / min-width / max-height / min-height
.block-one {
margin: 20px; /* 边距 20 像素 */
padding: 10px; /* 补白 10 像素 */
}
另见: 边距(margin) / 补白(padding)
.container {
/* 设置的边框和补白的值是包含在 width 内的 */
box-sizing: border-box;
}
另见: box-sizing
.invisible-elements {
visibility: hidden; /* 隐藏元素 */
}
另见: Visibility
div {
/* 览器自己选择一个合适的外边距 */
margin: auto;
}
另见: 边距(margin)
.small-block {
/* 浏览器总是显示滚动条 */
overflow: scroll;
}
另见: 溢出(overflow)
/* @keyframes duration | timing-function | delay |
iteration-count | direction | fill-mode | play-state | name */
animation: 3s ease-in 1s 2 reverse both paused slidein;
/* @keyframes duration | timing-function | delay | name */
animation: 3s linear 1s slidein;
/* @keyframes duration | name */
animation: 3s slidein;
animation: 4s linear 0s infinite alternate move_eye;
animation: bounce 300ms linear 0s infinite normal;
animation: bounce 300ms linear infinite;
animation: bounce 300ms linear infinite alternate-reverse;
animation: bounce 300ms linear 2s infinite alternate-reverse forwards normal;
.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; /* [ ■ ■ ■ ] */
}
justify-content: flex-start | flex-end | center | space-between
flex-start:左对齐(默认值)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
flex-end:右对齐
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
center: 居中
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-between:两端对齐,项目之间的间隔都相等
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮ ╭┈╮ ╭┈┈┈╮┆
┆╰┈┈╯ ╰┈╯ ╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-around:每个项目两侧的间隔相等,项目之间的间隔比项目与边框的间隔大一倍
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈╮ ╭┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈╯ ╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
上面示例,假设主轴为从左到右
flex-wrap: nowrap | wrap | wrap-reverse;
nowrap:不换行(默认)
╭1╮╭2╮╭3╮╭4╮╭5╮╭6╮╭7╮╭8╮╭9╮╭10╮
╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈┈╯
wrap:换行,第一行在 上方
╭1┈╮ ╭2┈╮ ╭3┈╮ ╭4┈╮ ╭5┈╮ ╭6┈╮ ╭7┈╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
╭8┈╮ ╭9┈╮ ╭10╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯
wrap-reverse:换行,第一行在 下方
╭8┈╮ ╭9┈╮ ╭10╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯
╭1┈╮ ╭2┈╮ ╭3┈╮ ╭4┈╮ ╭5┈╮ ╭6┈╮ ╭7┈╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
项目都排在一条线(又称"轴线")上
flex-direction: row | row-reverse | column | column-reverse;
╭┈┈╮ ▲ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆
╭┈┈╮ ┆ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆ ┈┈┈┈┈┈┈┈┈┈┈▶ ◀┈┈┈┈┈┈┈┈┈┈┈
╭┈┈╮ ┆ ╭┈┈╮ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
╰┈┈╯ ┆ ╰┈┈╯ ▼ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈
column-reverse column row row-reverse
属性决定主轴的方向(即项目的排列方向)
align-items: flex-start | flex-end | center | baseline | stretch;
▶ flex-start(起点对齐) ▶ flex-end(终点对齐)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
▶ center(中点对齐) ▶ stretch(占满整个容器的高度)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
▶ baseline(第一行文字的基线对齐)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈╮ ┆
┆ ┆ ┆ ╭┈┈┈┈╮ ╭┈┈┈┈╮ ┆ ┆ ╭┈┈┈┈╮╭┈┈┈┈╮┆
┆ ┆ text ┆ ┆text┆ ┆text┆ ┆ text ┆ ┆text┆┆text┆┆
┆ ┆ ┆ ╰┈┈┈┈╯ ┆ ┆ ┆ ┆ ╰┈┈┈┈╯┆ ┆┆
┆ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯┆
┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
▶ flex-start(起点对齐) ▶ flex-end(终点对齐)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ╭┈┈┈╮╭╮ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
▶ center(中点对齐) ▶ stretch(满整个交叉轴)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆ ┆┆ ┆┆ ┆┆┆┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╭┈┈┈╮╭╮╭┈╮ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ┆ ┆┆┆┆ ┆ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯╰┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
▶ space-between(两端对齐) ▶ space-around(均匀分布项目)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆ ┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆
┆ ┆ ┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆
┆ ┆ ┆ ┆
┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╰┈┈╯ ┆
┆ ╰┈┈╯ ┆ ┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
.item {
order: <integer>;
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈╮
┆ ╭1┈╮ ╭1┈┈╮ ╭1┈╮ ╭2┈╮ ╭3┈┈┈┈┈┈╮ ┆ ┆ ╭2┈┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈┈┈┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ┆ ╭2┈┈┈┈╮ ┆
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ┆ ╰┈┈┈┈┈╯ ┆
┆ ╭-┈┈╮ ╭┈┈┈╮ ╭┈┈┈┈┈┈┈┈╮ ╭┈┈┈╮ ┆ ┆ ╭99┈┈┈╮ ┆
┆ ┆-1 ┆ ┆ 1 ┆ ┆ 2 ┆ ┆ 5 ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈┈╯ ╰┈┈┈╯ ╰┈┈┈┈┈┈┈┈╯ ╰┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈╯
属性 order 定义项目的排列顺序。数值越小,排列越靠前,默认为 0
.item {
flex-grow: <number>; /* default 0 */
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈1┈┈╮╭┈┈2┈┈╮╭┈┈1┈┈╮ ┆
┆ ╰┈┈┈┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈1┈╮╭┈┈┈┈2┈┈┈┈╮╭┈1┈╮ ┆
┆ ╰┈┈┈╯╰┈┈┈┈┈┈┈┈┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
属性 flex-grow 定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
定义 CSS 变量
:root {
--first-color: #16f;
--second-color: #ff7;
}
变量用法
#firstParagraph {
background-color: var(--first-color);
color: var(--second-color);
}
另见: CSS Variable
/* Set "my-counter" to 0 */
counter-set: my-counter;
/* Increment "my-counter" by 1 */
counter-increment: my-counter;
/* Decrement "my-counter" by 1 */
counter-increment: my-counter -1;
/* Reset "my-counter" to 0 */
counter-reset: my-counter;
另见: Counter set
div {
width: calc(100% - 30px);
height: calc(100% - 30px);
}
calc() CSS 函数允许您在指定 CSS 属性值时执行计算
html {
scroll-behavior: smooth;
}
点击我页面会平滑滚动到入门
input[type="text"]:autofill {
box-shadow: 0 0 0 1000px #000 inset;
-webkit-text-fill-color: white;
}
另见: :autofill
div {
aspect-ratio: 1/1
}
属性 aspect-ratio 可以非常容易的定义一个容器的长宽比
div {
cursor: url('path-to-image.png'), url('path-to-fallback-image.png'), auto;
/* 表情符号作为光标 */
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;
}
.sticky {
position: sticky;
top: 0;
}
属性 sticky 能在滚动到顶部的位置固定住元素
.container {
height: 250px;
overflow-x: scroll;
display: flex;
scroll-snap-type: x mandatory;
column-gap: 10px;
}
.child {
flex: 0 0 66%;
width: 250px;
background-color: #663399;
scroll-snap-align: center;
}
可用于 轮播图 效果,效果预览