CSS 实用技巧
这页适合作为“日常样式问题的实用片段集合”。真正高频的 CSS 问题通常不是语法不会写,而是布局、层级、溢出、暗色模式和动画细节没形成稳定套路。
先建立样式习惯
- 优先写可复用布局模式
- 优先移动优先
- 优先使用相对单位和弹性布局
- 动画和视觉效果先保证可读性,再追求炫技
现代布局
Container Queries
.card-container {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Subgrid
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.grid-item {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
}
居中
/* 最简单的居中 */
.center {
display: grid;
place-items: center;
}
/* Flexbox 居中 */
.center {
display: flex;
align-items: center;
justify-content: center;
}
实用片段
文本截断
/* 单行 */
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 多行 */
.line-clamp-3 {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
自定义滚动条
::-webkit-scrollbar {
width: 6px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: rgba(0, 0, 0, 0.2);
border-radius: 3px;
}
/* Firefox */
* {
scrollbar-width: thin;
scrollbar-color: rgba(0, 0, 0, 0.2) transparent;
}
毛玻璃效果
.glass {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(16px) saturate(1.2);
border: 1px solid rgba(255, 255, 255, 0.15);
}
渐变文字
.gradient-text {
background: linear-gradient(135deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
响应式字体
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
}
暗色模式
:root {
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0a0a0a;
--text: #e8e8e8;
}
}
/* 或用 class 切换 */
html.dark {
--bg: #0a0a0a;
--text: #e8e8e8;
color-scheme: dark;
}
动画
过渡
.btn {
transition: all 0.2s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
关键帧
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate {
animation: fadeIn 0.3s ease both;
}
骨架屏
.skeleton {
background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
0% {
background-position: 200% 0;
}
100% {
background-position: -200% 0;
}
}
选择器
/* 首个/末个 */
li:first-child {
}
li:last-child {
}
/* 奇偶行 */
tr:nth-child(odd) {
}
tr:nth-child(even) {
}
/* 空元素 */
div:empty {
display: none;
}
/* 聚焦可见(键盘聚焦) */
button:focus-visible {
outline: 2px solid blue;
}
/* has 选择器 */
.card:has(img) {
grid-template-rows: auto 1fr;
}
.form:has(:invalid) .submit {
opacity: 0.5;
}
/* 逻辑属性 */
.box {
margin-inline: auto; /* 左右 margin */
padding-block: 1rem; /* 上下 padding */
}
性能
/* 减少重绘 */
.animated {
will-change: transform;
transform: translateZ(0); /* 开启 GPU 加速 */
}
/* 内容可见性(懒渲染) */
.offscreen {
content-visibility: auto;
contain-intrinsic-size: 0 500px;
}
常见问题
为什么布局总是越修越乱
通常是因为:
- 没有建立统一布局模型
- 过多依赖绝对定位
- 不清楚块级、行内、Flex、Grid 的边界
为什么暗色模式切过去很奇怪
高频原因包括:
- 只改了背景,没改文字和边框
- 组件局部变量没有统一
- 图片、阴影和半透明层没有重新检查
CSS 技巧要不要全记住
不需要。更重要的是先掌握高频模式,把常用片段沉淀成自己的模板。
延伸阅读
参考链接
- CSS Tricks — 技巧与教程
- Modern CSS — 现代 CSS 方案
- Can I Use — 浏览器兼容性
- Defensive CSS — 防御性 CSS