这是一个 CSS 精华的快速参考备忘单,列出了选择器语法、属性、单位和其他有用的信息片段。
<div class="classname"></div>
<div class="class1 ... classn"></div>
一个元素支持多个类。
.post-title {
color: blue !important;
}
覆盖所有先前的样式规则。
color: #2a2aff;
color: green;
color: rgb(34, 12, 64, 0.6);
color: hsla(30 100% 50% / 0.6);
参见:颜色
background-color: blue;
background-image: url("nyan-cat.gif");
background-image: url("../image.png");
参见:背景
/* 这是单行注释 */
/* 这是
多行注释 */
a[target] |
带有 |
a[target="_blank"] |
在新标签页中打开 |
a[href^="/index"] |
以 |
[class|="chair"] |
以 |
[class*="chair"] |
包含 |
[title~="chair"] |
包含单词 |
a[href$=".doc"] |
以 |
[type="button"] |
指定类型 |
另请参阅:属性选择器
a:link |
正常状态的链接 |
a:active |
点击状态的链接 |
a:hover |
鼠标悬停的链接 |
a:visited |
已访问的链接 |
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) |
不是 span 的元素 |
input:checked |
选中的输入 |
input:disabled |
禁用的输入 |
input:enabled |
启用的输入 |
input:focus |
输入框获得焦点 |
input:in-range |
值在范围内 |
input:out-of-range |
输入值超出范围 |
input:valid |
具有有效值的输入 |
input:invalid |
具有无效值的输入 |
input:optional |
没有 required 属性 |
input:required |
具有 required 属性的输入 |
input:read-only |
具有 readonly 属性 |
input:read-write |
没有 readonly 属性 |
input: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 |
其父元素的唯一子元素 |
属性 | 描述 | ||||||
---|---|---|---|---|---|---|---|
font-family: |
<字体> <字体N> | ||||||
font-size: |
<大小> | ||||||
letter-spacing: |
<大小> | ||||||
line-height: |
<数字> | ||||||
font-weight: |
<数字> / bold / normal | font-style: |
italic / normal | text-decoration: |
underline / none | ||
text-align: |
left / right center / justify |
text-transform: |
capitalize / uppercase / lowercase |
另请参阅:字体
样式 | 字重 | 大小 (必需) | 行高 | 字体族 (必需) | ||
---|---|---|---|---|---|---|
font: |
italic |
400 |
14px |
/ |
1.5 |
sans-serif |
样式 | 字重 | 大小 (必需) | 行高 | 字体族 (必需) |
font-family: Arial, sans-serif;
font-size: 12pt;
letter-spacing: 0.02em;
/* Hello */
text-transform: capitalize;
/* HELLO */
text-transform: uppercase;
/* hello */
text-transform: lowercase;
@font-face {
font-family: "Glegoo";
src: url("../Glegoo.woff");
}
color: red;
color: orange;
color: tan;
color: rebeccapurple;
color: #090;
color: #009900;
color: #090a;
color: #009900aa;
color: rgb(34, 12, 64, 0.6);
color: rgba(34, 12, 64, 0.6);
color: rgb(34 12 64 / 0.6);
color: rgba(34 12 64 / 0.3);
color: rgb(34 12 64 / 60%);
color: rgba(34.6 12 64 / 30%);
color: hsl(30, 100%, 50%, 0.6);
color: hsla(30, 100%, 50%, 0.6);
color: hsl(30 100% 50% / 0.6);
color: hsla(30 100% 50% / 0.6);
color: hsl(30 100% 50% / 60%);
color: hsla(30.2 100% 50% / 60%);
color: inherit;
color: initial;
color: unset;
color: transparent;
color: currentcolor; /* keyword */
属性 | 描述 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
background: |
(简写) | ||||||||||||||||||
background-color: |
参见:颜色 | background-image: |
url(...) | background-position: |
left/center/right top/center/bottom |
background-size: |
cover X Y | background-clip: |
border-box padding-box content-box |
background-repeat: |
no-repeat repeat-x repeat-y |
background-attachment: |
scroll/fixed/local |
颜色 | 图像 | 位置X | 位置Y | 大小 | 重复 | 附件 | ||
---|---|---|---|---|---|---|---|---|
background: |
#ff0 |
url(a.jpg) |
left |
top |
/ |
100px auto |
no-repeat |
fixed; |
background: |
#abc |
url(b.png) |
center |
center |
/ |
cover |
repeat-x |
local; |
颜色 | 图像 | 位置X | 位置Y | 大小 | 重复 | 附件.. |
background: url(img_man.jpg) no-repeat center;
background:
url(img_flwr.gif) right bottom no-repeat,
url(paper.gif) left top repeat;
background: rgb(2, 0, 36);
background: linear-gradient(90deg, rgba(2, 0, 36, 1) 0%, rgba(13, 232, 230, 1) 35%, rgba(0, 212, 255, 1) 100%);
.column {
max-width: 200px;
width: 500px;
}
另请参阅:max-width / min-width / max-height / min-height
.container {
box-sizing: border-box;
}
另请参阅:Box-sizing
名称 | 持续时间 | 时间函数 | 延迟 | 次数 | 方向 | 填充模式 | 播放状态 | |
---|---|---|---|---|---|---|---|---|
animation: |
bounce |
300ms |
linear |
100ms |
infinite |
alternate-reverse |
both |
reverse |
名称 | 持续时间 | 时间函数 | 延迟 | 次数 | 方向 | 填充模式 | 播放状态 |
属性 | 值 |
---|---|
animation: |
(简写) |
animation-name: |
<名称> |
animation-duration: |
<时间>ms |
animation-timing-function: |
ease / linear / ease-in / ease-out / ease-in-out |
animation-delay: |
<时间>ms |
animation-iteration-count: |
infinite / <数字> |
animation-direction: |
normal / reverse / alternate / alternate-reverse |
animation-fill-mode: |
none / forwards / backwards / both / initial / inherit |
animation-play-state: |
normal / reverse / alternate / alternate-reverse |
另请参阅:动画
/* @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;
.one('webkitAnimationEnd oanimationend msAnimationEnd animationend')
.container {
display: flex;
}
.container > div {
flex: 1 1 auto;
}
.container {
display: flex;
display: inline-flex;
flex-direction: row; /* 从左到右 - 默认 */
flex-direction: row-reverse; /* 从右到左 */
flex-direction: column; /* 从上到下 */
flex-direction: column-reverse; /* 从下到上 */
flex-wrap: nowrap; /* 单行 */
flex-wrap: wrap; /* 多行 */
align-items: flex-start; /* 垂直对齐到顶部 */
align-items: flex-end; /* 垂直对齐到底部 */
align-items: center; /* 垂直居中对齐 */
align-items: stretch; /* 所有项目等高 (默认) */
justify-content: flex-start; /* [xxx ] */
justify-content: center; /* [ xxx ] */
justify-content: flex-end; /* [ xxx] */
justify-content: space-between; /* [x x x] */
justify-content: space-around; /* [ x x x ] */
justify-content: space-evenly; /* [ x x x ] */
}
.container > div {
/* 如下所示:*/
flex: 1 0 auto;
/* 等同于:*/
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
order: 1;
align-self: flex-start; /* 左对齐 */
margin-left: auto; /* 右对齐 */
}
.container {
display: flex;
}
.container > div {
width: 100px;
height: 100px;
margin: auto;
}
.container {
display: flex;
/* 垂直 */
align-items: center;
/* 水平 */
justify-content: center;
}
.container > .top {
order: 1;
}
.container > .bottom {
order: 2;
}
.container {
display: flex;
flex-direction: column;
}
.container > .top {
flex: 0 0 100px;
}
.container > .content {
flex: 1 0 auto;
}
一个固定高度的顶部栏和一个动态高度的内容区域。
.container {
display: flex;
}
/* 这里的 'px' 值仅为建议的百分比 */
.container > .checkbox {
flex: 1 0 20px;
}
.container > .subject {
flex: 1 0 400px;
}
.container > .date {
flex: 1 0 120px;
}
这将创建具有不同宽度但会根据情况相应调整大小的列。
.container {
align-items: center;
}
垂直居中所有项目。
.menu > .left {
align-self: flex-start;
}
.menu > .right {
align-self: flex-end;
}
#grid-container {
display: grid;
width: 100px;
grid-template-columns: 20px 20% 60%;
}
.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}
/*行间距为 20px*/
/*列间距为 10px*/
#grid-container {
display: grid;
grid-gap: 20px 10px;
}
#grid-container {
display: block;
}
#grid-container {
display: inline-grid;
}
.grid {
display: grid;
grid-template-columns: 100px minmax(100px, 500px) 100px;
}
CSS 语法:
grid-row-start: 2;
grid-row-end: span 2;
grid-row-gap: length;
任何合法的长度值,如 px 或 %。0 是默认值
.item1 {
grid-area: 2 / 1 / span 2 / span 3;
}
#container {
display: grid;
justify-items: center;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
.item {
grid-area: nav;
}
.grid-container {
display: grid;
grid-template-areas:
"nav nav . ."
"nav nav . .";
}
#grid-container {
display: grid;
justify-items: start;
}
.grid-items {
justify-self: end;
}
网格项位于行的右侧(末端)。
#container {
display: grid;
align-items: start;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
定义 CSS 变量
:root {
--first-color: #16f;
--second-color: #ff7;
}
变量用法
#firstParagraph {
background-color: var(--first-color);
color: var(--second-color);
}
另请参阅:CSS 变量
/* 设置 "my-counter" 为 0 */
counter-set: my-counter;
/* "my-counter" 增加 1 */
counter-increment: my-counter;
/* "my-counter" 减少 1 */
counter-increment: my-counter -1;
/* 重置 "my-counter" 为 0 */
counter-reset: my-counter;
另请参阅:计数器设置
body {
counter-reset: section;
}
h3::before {
counter-increment: section;
content: "Section." counter(section);
}
ol {
counter-reset: section;
list-marker-type: none;
}
li::before {
counter-increment: section;
content: counters(section, ".") " ";
}
.element-wrap {
container: element / inline-size;
}
@container element (min-inline-size: 300px) {
.element {
display: flex;
gap: 1rem;
}
}
.container {
--variant: 1;
&.variant2 {
--variant: 2;
}
}
@container style(--variant: 1) {
button {
} /* 你不能直接为 .container 设置样式,但可以选择其内部元素 */
.other-things {
}
}
@container style(--variant: 2) {
button {
}
.whatever {
}
}
.card {
padding: 5cqi;
font-size: 4cqi;
border: 1cqi solid brown;
height: 100%;
}
h2 {
font-size: 10cqi;
margin-block: 0 3cqi;
}
figure:has(figcaption) {
border: 1px solid black;
padding: 0.5rem;
}
.cards {
.card {
& .card-description {
color: blue;
}
& .card-title {
color: red;
}
}
}
@scope {
:scope {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
padding: 1rem;
border: 1px solid black;
}
.card {
padding: 1rem;
border: 1px solid black;
background: lightgray;
h2 {
margin: 0 0 1rem 0;
}
}
}
/* 指定层叠中应用样式的顺序 */
@layer legacyCard, newCard;
/* 假设你有很多样式 */
@layer newCard {
.card {
background-color: red;
}
}
@layer legacyCard {
.card {
background-color: green;
}
}
button {
background-color: #4caf50;
border: none;
color: white;
padding: 0.5rem 1.5rem;
text-decoration: none;
font: inherit;
border-radius: 4px;
.icon {
position: relative;
top: 0.125em;
fill: white;
width: 1em;
aspect-ratio: 1;
margin-inline-end: 0.25rem;
}
}
<div class="container">
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-color: color(display-p3 1 0.5 0);
}
}
</style>
</div>
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-color: oklch(61.88% 0.286 342.4);
}
}
</style>
</div>
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-color: oklab(0.73 0.15 0.16);
}
}
</style>
</div>
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-image: linear-gradient(to right in oklch, red, blue);
}
}
</style>
</div>
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-image: linear-gradient(to right in oklab, red, blue);
}
}
</style>
</div>
<div class="swatch">
<style contenteditable>
@scope {
:scope {
background-image: linear-gradient(to right in srgb, red, blue);
}
}
</style>
</div>
</div>
.swatch {
color: white;
width: 100px;
aspect-ratio: 1;
display: grid;
place-items: center;
text-align: center;
&:nth-child(1) {
background-color: var(--bg);
}
&:nth-child(2) {
background-color: color-mix(in oklch, var(--bg), black 30%);
}
&:nth-child(3) {
background-color: color-mix(in oklch, var(--bg), white 30%);
}
}
.container {
/* 防止元素末尾出现“多余”外边距 */
margin-trim: block-end;
/* 类似这样的元素可能是罪魁祸首,但也可能是任何元素 */
> p {
margin-block-end: 1rem;
}
}
.balance {
text-wrap: balance;
}
.pretty {
text-wrap: pretty;
}
html {
font-family: system-ui, sans-serif;
}
main {
max-inline-size: 60ch;
margin-inline: auto;
}
.grid {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(4, minmax(100px, auto));
}
.item {
display: grid;
grid-column: 2 / 7;
grid-row: 2 / 4;
grid-template-columns: subgrid;
grid-template-rows: repeat(3, 80px);
}
.subitem {
grid-column: 3 / 6;
grid-row: 1 / 3;
}