CSS 看起来简单,但要精通它需要掌握许多技巧。本文收集了实用的 CSS 技巧,帮助你编写更优雅的代码。

居中技巧

水平居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* 行内元素 */
.parent {
text-align: center;
}

/* 块级元素 */
.child {
margin: 0 auto;
}

/* Flexbox */
.parent {
display: flex;
justify-content: center;
}

垂直居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Flexbox - 最推荐 */
.parent {
display: flex;
align-items: center;
}

/* Grid */
.parent {
display: grid;
place-items: center;
}

/* 绝对定位 + transform */
.parent {
position: relative;
}

.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

文本省略

单行省略

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

多行省略

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

清除浮动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 方式1: 伪元素清除(推荐) */
.clearfix::after {
content: "";
display: block;
clear: both;
}

/* 方式2: Overflow */
.clearfix {
overflow: hidden;
}

/* 方式3: Flexbox 替代浮动 */
.container {
display: flex;
}

CSS 变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
:root {
--primary-color: #5CB3CC;
--text-color: #333;
--spacing-unit: 8px;
}

.button {
background: var(--primary-color);
color: var(--text-color);
padding: calc(var(--spacing-unit) * 2);
}

/* 动态修改变量 */
@media (prefers-color-scheme: dark) {
:root {
--text-color: #fff;
}
}

响应式设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 移动优先 */
.container {
width: 100%;
padding: 1rem;
}

@media (min-width: 768px) {
.container {
max-width: 720px;
margin: 0 auto;
}
}

@media (min-width: 1024px) {
.container {
max-width: 960px;
}
}

/* 容器查询 */
.card {
container-type: inline-size;
}

@container (min-width: 400px) {
.card-title {
font-size: 1.5rem;
}
}

隐藏元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 完全隐藏(不占空间) */
.hidden {
display: none;
}

/* 隐藏但保留空间 */
.invisible {
visibility: hidden;
}

/* 屏幕阅读器可读,视觉隐藏 */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}

平滑滚动

1
2
3
4
5
6
7
8
html {
scroll-behavior: smooth;
}

/* 回到顶部 */
.back-to-top {
scroll-margin-top: 100px;
}

自定义滚动条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Webkit 浏览器 */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}

::-webkit-scrollbar-track {
background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
background: #555;
}

玻璃拟态效果

1
2
3
4
5
6
7
.glass {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 16px;
}

渐变边框

1
2
3
4
5
6
7
.gradient-border {
position: relative;
background: padding-box,
linear-gradient(90deg, #5CB3CC, #4A90E2) border-box;
border: 2px solid transparent;
border-radius: 8px;
}

霓虹文字效果

1
2
3
4
5
6
7
8
9
.neon-text {
color: #fff;
text-shadow:
0 0 5px #fff,
0 0 10px #fff,
0 0 20px #ff00de,
0 0 40px #ff00de,
0 0 80px #ff00de;
}

卡片悬浮效果

1
2
3
4
5
6
7
8
.card {
transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

加载动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 旋转加载 */
@keyframes spin {
to { transform: rotate(360deg); }
}

.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top-color: #5CB3CC;
border-radius: 50%;
animation: spin 1s linear infinite;
}

/* 脉冲加载 */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}

.pulse {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

宽高比保持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.aspect-ratio-box {
aspect-ratio: 16 / 9;
width: 100%;
object-fit: cover;
}

/* 兼容方案 */
.aspect-ratio-fallback {
width: 100%;
padding-bottom: 56.25%; /* 16:9 = 9/16 = 0.5625 */
position: relative;
}

.aspect-ratio-fallback > * {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

Grid 布局技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 自动填充列 */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}

/* 卡片堆叠 */
.card-stack {
display: grid;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}

选择器技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* 第一个子元素 */
.first-child:first-child {
/* 样式 */
}

/* 最后一个子元素 */
.last-child:last-child {
/* 样式 */
}

/* 偶数子元素 */
.even:nth-child(even) {
background: #f5f5f5;
}

/* 奇数子元素 */
.odd:nth-child(odd) {
background: #e5e5e5;
}

/* 每3个元素 */
.every-third:nth-child(3n) {
border-right: none;
}

/* 之后的兄弟元素 */
h2 ~ p {
margin-top: 1rem;
}

/* 紧随其后的兄弟元素 */
h2 + p {
font-weight: bold;
}

总结

CSS 有很多实用技巧,掌握这些技巧可以帮助你更高效地完成样式开发。建议收藏本文,随时查阅!