120个实用CSS技巧汇总合集
在前端开发中,CSS 往往是最被低估的一环。但真正优秀的开发者,往往懂得如何用 CSS 写出高效、优雅又强大的界面。
无论你是刚入门的新手,还是正在精进的前端工程师,这 120 个精选分类的 CSS 小技巧,都会帮你解决常见痛点,提升页面性能与交互体验。
一、布局技巧
1. 居中一个元素(水平 + 垂直)
.center {
display: flex;
justify-content: center;
align-items: center;
}
2. 响应式宽度限制
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
3. CSS Grid 快速布局三列
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
4. Flex 快速平分布局
.flex-split {
display: flex;
}
.flex-split > div {
flex: 1;
}
5. sticky 固定导航栏
nav {
position: sticky;
top: 0;
background: white;
}
6. 双栏固定 + 自适应布局
.layout {
display: grid;
grid-template-columns: 200px auto;
}
7. 圣杯布局
body {
display: flex;
}
.left, .right {
width: 200px;
}
.center {
flex: 1;
}
8. 内容高度占满剩余空间
.wrapper {
display: flex;
flex-direction: column;
height: 100vh;
}
.content {
flex: 1;
}
9. 媒体查询适配移动端
@media (max-width: 768px) {
.grid {
grid-template-columns: 1fr;
}
}
10. 强制换行
.break-word {
word-break: break-word;
}
11. 固定宽高比例容器(如16:9)
.ratio {
position: relative;
padding-top: 56.25%;
}
.ratio > iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
12. 单行文本省略号
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
13. 多行文本省略号
.multi-ellipsis {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
14. 子元素自动换行
.flex-wrap {
display: flex;
flex-wrap: wrap;
}
15. 等高卡片布局
.card-group {
display: flex;
}
.card-group .card {
flex: 1;
display: flex;
flex-direction: column;
}
16. 垂直居中(非 Flex)
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
17. 布局自适应缩放字体大小
html {
font-size: clamp(14px, 2vw, 18px);
}
18. flex 实现等间距分布
.space-between {
display: flex;
justify-content: space-between;
}
19. 内容自适应图片
img {
max-width: 100%;
height: auto;
}
20. Grid 网格自动填充列
.grid-auto {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 1rem;
}
二、视觉样式技巧
21. 自定义滚动条
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-thumb {
background: #ccc;
border-radius: 4px;
}
22. 纯CSS渐变边框
.border-gradient {
border: 4px solid transparent;
background-clip: padding-box, border-box;
background-origin: padding-box, border-box;
background-image: linear-gradient(#fff, #fff), linear-gradient(to right, #f06, #4a90e2);
}
23. 毛玻璃效果
.blur-bg {
background: rgba(255,255,255,0.3);
backdrop-filter: blur(10px);
}
24. 渐变文字
.gradient-text {
background: linear-gradient(to right, #f06, #4a90e2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
25. 图片变灰滤镜
img.gray {
filter: grayscale(100%);
}
26. 自定义光标
.custom-cursor {
cursor: url(cursor.png), auto;
}
27. 图标 hover 动画
.icon:hover {
transform: scale(1.2);
transition: transform 0.2s ease-in;
}
28. 半透明遮罩层
.overlay {
background-color: rgba(0, 0, 0, 0.5);
}
29. 设置图片圆角头像
.avatar {
border-radius: 50%;
}
30. 投影增强立体感
.shadow-box {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
31. 自定义文本选择颜色
::selection {
background: #ffb7b7;
color: #000;
}
32. 跳动心形动画
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
.heart {
animation: pulse 1s infinite;
}
33. 带阴影边框文字
.text-shadow {
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
34. 虚线边框动画
.dashed-animate {
border: 2px dashed #000;
animation: dashmove 4s linear infinite;
}
@keyframes dashmove {
to {
background-position: 100% 0;
}
}
35. hover 变暗按钮
button:hover {
filter: brightness(90%);
}
36. 高亮当前导航链接
nav a.active {
color: #f00;
font-weight: bold;
}
37. 使用 CSS clip-path 制作形状
.clip-circle {
clip-path: circle(50%);
}
38. 霓虹灯文字效果
.neon {
color: #fff;
text-shadow: 0 0 5px #0ff, 0 0 10px #0ff;
}
39. 动态加载效果
.loading {
background: linear-gradient(90deg, #eee, #ccc, #eee);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
100% { background-position: -200% 0; }
}
40. 设置文字描边
.stroke-text {
color: white;
-webkit-text-stroke: 1px black;
}
三、表单与交互技巧
41. 自定义复选框样式
input[type="checkbox"] {
accent-color: #f06;
}
42. placeholder 字体颜色
input::placeholder {
color: #999;
}
43. 输入框获得焦点样式
input:focus {
outline: none;
border-color: #4a90e2;
box-shadow: 0 0 5px rgba(74,144,226,0.5);
}
44. 禁止输入框缩放(移动端)
input, textarea {
font-size: 16px;
}
45. 表单验证成功样式
input:valid {
border-color: green;
}
46. 表单验证失败样式
input:invalid {
border-color: red;
}
47. 仅样式第一个或最后一个输入框
input:first-of-type {
border-top-left-radius: 8px;
}
input:last-of-type {
border-bottom-left-radius: 8px;
}
48. 按钮禁用状态样式
button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
49. 设置全局按钮样式
button {
padding: 10px 20px;
border: none;
background: #4a90e2;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
50. 自定义 radio 样式(基础)
input[type="radio"] {
accent-color: #ff5722;
}
51. 禁止输入框拖动文本(移动端)
input {
-webkit-user-select: none;
user-select: none;
}
52. 提交按钮 hover 效果
button:hover {
background: #357ab7;
}
53. 透明背景输入框
input {
background: transparent;
border: 1px solid #ccc;
}
54. 用 label 美化上传按钮
<label for="file">上传文件</label>
<input type="file" id="file" hidden>
55. 鼠标悬停切换 input 边框色
input:hover {
border-color: #4caf50;
}
56. checkbox 开关按钮样式(切换器)
.toggle input:checked + span {
background: #4caf50;
}
57. input 自动填充样式(兼容 Chrome)
input:-webkit-autofill {
background-color: #e0f7fa !important;
}
58. 自定义 select 下拉样式(简洁版)
select {
appearance: none;
background: url('arrow-down.svg') no-repeat right;
padding-right: 20px;
}
59. 限制只能输入数字
input[type="number"] {
-moz-appearance: textfield;
}
60. 表单输入框光标颜色
input {
caret-color: #ff4081;
}
四、性能与响应式技巧
61. 设置图像懒加载
<img src="image.jpg" loading="lazy">
62. 使用 will-change 提前渲染动画元素
.box {
will-change: transform;
}
63. 减少重绘:统一触发动画属性
.fast {
transform: translateY(10px);
opacity: 0.8;
transition: all 0.3s ease;
}
64. 使用 contain 限制重排范围
.card {
contain: layout paint;
}
65. 优化字体渲染
body {
-webkit-font-smoothing: antialiased;
}
66. 禁止图片拖拽
img {
user-drag: none;
}
67. 限制图片最大宽度防止溢出
img {
max-width: 100%;
height: auto;
}
68. 提高点击响应速度
a, button {
touch-action: manipulation;
}
69. 媒体查询:切换浅色/深色主题
@media (prefers-color-scheme: dark) {
body {
background: #222;
color: #fff;
}
}
70. 字体大小根据屏幕缩放
body {
font-size: clamp(14px, 1.5vw, 18px);
}
71. 使用 aspect-ratio 设置图片比例
img {
aspect-ratio: 16 / 9;
width: 100%;
}
72. 自适应弹性网格
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
73. 只在需要时开启动画
@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}
74. 防止 iOS 字体放大
html {
-webkit-text-size-adjust: 100%;
}
75. 设置固定视口单位避免移动端跳动
:root {
--vh: 100vh;
}
76. 移动端一键适配缩放
<meta name="viewport" content="width=device-width, initial-scale=1">
77. 适配 Retina 屏图片
@media (-webkit-min-device-pixel-ratio: 2) {
.logo {
background-image: url("logo@2x.png");
}
}
78. 控制响应式卡片宽度
.card {
max-width: 400px;
margin: auto;
}
79. 媒体查询响应式字体
@media (max-width: 600px) {
h1 {
font-size: 1.5rem;
}
}
80. 使用 object-fit 填充图像
img.cover {
width: 100%;
height: 200px;
object-fit: cover;
}
五、动画与过渡技巧
81. 渐变背景无限动画
@keyframes gradientMove {
0% { background-position: 0 0; }
100% { background-position: 100% 0; }
}
.bg-animate {
background: linear-gradient(270deg, #f06, #4a90e2);
background-size: 200% 100%;
animation: gradientMove 5s infinite linear;
}
82. 按钮点击波纹动画
button {
position: relative;
overflow: hidden;
}
button::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.5);
animation: ripple 0.6s ease-out;
}
@keyframes ripple {
from { opacity: 1; transform: scale(0); }
to { opacity: 0; transform: scale(2); }
}
83. 页面加载过渡动画
.fade-in {
opacity: 0;
animation: fadeIn 1s ease forwards;
}
@keyframes fadeIn {
to { opacity: 1; }
}
84. 悬浮卡片弹起
.card:hover {
transform: translateY(-10px);
box-shadow: 0 10px 20px rgba(0,0,0,0.15);
}
85. 自定义 loading 圆圈
.loader {
border: 4px solid #eee;
border-top: 4px solid #4a90e2;
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
}
@keyframes spin {
100% { transform: rotate(360deg); }
}
86. 渐隐渐现切换内容
.fade {
transition: opacity 0.5s ease-in-out;
}
.fade.hidden {
opacity: 0;
}
87. 抖动动画(错误提示)
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
.shake {
animation: shake 0.3s;
}
88. 按钮点击缩放动画
button:active {
transform: scale(0.95);
}
89. 文字打字机效果
.typing {
overflow: hidden;
white-space: nowrap;
animation: typing 3s steps(30) forwards;
}
@keyframes typing {
from { width: 0; }
to { width: 100%; }
}
90. 滚动条平滑滚动
html {
scroll-behavior: smooth;
}
六、实用小组件技巧
91. 标签页切换(纯 CSS)
<input type="radio" name="tab" id="tab1" checked>
<input type="radio" name="tab" id="tab2">
<div class="tabs">
<label for="tab1">标签1</label>
<label for="tab2">标签2</label>
</div>
<div class="content">
<div class="tab1-content">内容1</div>
<div class="tab2-content">内容2</div>
</div>
.tab1-content, .tab2-content { display: none; }
#tab1:checked ~ .content .tab1-content { display: block; }
#tab2:checked ~ .content .tab2-content { display: block; }
92. 纯 CSS 手风琴菜单
<input type="checkbox" id="accordion1">
<label for="accordion1">展开内容</label>
<div class="panel">这里是内容</div>
#accordion1:not(:checked) ~ .panel {
display: none;
}
93. CSS 打勾动画
.checkmark {
width: 30px;
height: 30px;
border: 2px solid #4caf50;
transform: rotate(45deg);
border-left: none;
border-top: none;
animation: check 0.3s ease forwards;
}
@keyframes check {
0% { width: 0; height: 0; }
100% { width: 30px; height: 30px; }
}
94. 进度条动画
.progress {
width: 100%;
background: #eee;
}
.progress-bar {
height: 10px;
width: 0%;
background: #4a90e2;
animation: fill 2s forwards;
}
@keyframes fill {
to { width: 80%; }
}
95. 模态弹窗样式(配合 JS 控制)
.modal {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.5);
display: flex;
justify-content: center;
align-items: center;
}
.modal-content {
background: white;
padding: 2rem;
border-radius: 10px;
}
96. 纯 CSS 弹出提示 Tooltip
<button class="tooltip" data-title="提示内容">悬停我</button>
.tooltip {
position: relative;
}
.tooltip::after {
content: attr(data-title);
position: absolute;
bottom: 100%;
background: #333;
color: white;
font-size: 12px;
padding: 5px;
white-space: nowrap;
border-radius: 5px;
opacity: 0;
transform: translateY(10px);
transition: 0.3s;
}
.tooltip:hover::after {
opacity: 1;
transform: translateY(0);
}
97. 纯 CSS 标签/徽章(Badge)
.badge {
background: red;
color: white;
padding: 2px 6px;
border-radius: 999px;
font-size: 12px;
}
98. 折叠菜单(侧边栏)
.sidebar {
width: 0;
overflow: hidden;
transition: width 0.3s;
}
.sidebar.open {
width: 200px;
}
99. 响应式卡片 hover 信息浮现
.card {
position: relative;
overflow: hidden;
}
.card-info {
position: absolute;
bottom: -100%;
background: rgba(0,0,0,0.7);
color: white;
width: 100%;
padding: 1rem;
transition: bottom 0.3s;
}
.card:hover .card-info {
bottom: 0;
}
100. 星级评分组件(伪元素)
.rating {
display: flex;
}
.rating::before {
content: '★★★★★';
letter-spacing: 3px;
background: linear-gradient(90deg, gold 60%, #ccc 0%);
-webkit-background-clip: text;
color: transparent;
}
101. 图片放大镜效果
.zoom {
overflow: hidden;
}
.zoom img {
transition: 0.3s;
}
.zoom:hover img {
transform: scale(1.2);
}
102. 时间轴
.timeline {
position: relative;
border-left: 2px solid #4a90e2;
padding-left: 20px;
}
.timeline-item {
margin-bottom: 20px;
position: relative;
}
.timeline-item::before {
content: '';
position: absolute;
left: -9px;
top: 0;
width: 10px;
height: 10px;
background: #4a90e2;
border-radius: 50%;
}
103. 输入框带搜索图标
.search {
background: url(search-icon.svg) no-repeat 10px center;
padding-left: 30px;
}
104. 骨架屏(Skeleton)
.skeleton {
background: linear-gradient(90deg, #eee, #ddd, #eee);
background-size: 200% 100%;
animation: skeleton 1.2s infinite;
}
@keyframes skeleton {
100% { background-position: -200% 0; }
}
105. 数字翻牌效果(纯动画)
@keyframes flipIn {
0% { transform: rotateX(-90deg); opacity: 0; }
100% { transform: rotateX(0); opacity: 1; }
}
.flip-number {
animation: flipIn 0.5s ease;
}
106. 卡片阴影层级提升
.card {
transition: box-shadow 0.3s;
}
.card:hover {
box-shadow: 0 12px 24px rgba(0,0,0,0.2);
}
107. 图标徽章位置(右上角)
.icon-wrapper {
position: relative;
}
.badge {
position: absolute;
top: 0;
right: 0;
background: red;
color: white;
font-size: 10px;
border-radius: 50%;
padding: 2px 6px;
}
108. 拖拽指示样式
.draggable {
cursor: grab;
}
.draggable:active {
cursor: grabbing;
}
109. 时间倒计时样式(需 JS 逻辑)
.countdown {
font-family: monospace;
font-size: 2rem;
background: #000;
color: #0f0;
padding: 10px;
}
110. 滚动提示箭头动画
.scroll-down::after {
content: '↓';
display: block;
animation: bounce 1s infinite;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(10px); }
}
七、进阶与实战技巧
111. 悬浮浮动阴影渐进增强
.shadow-hover {
transition: box-shadow 0.3s ease;
}
.shadow-hover:hover {
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
112. 图片加载失败备用样式
img {
object-fit: cover;
background: url('fallback.jpg') center/cover no-repeat;
}
113. 防止表单在移动端被缩放
input, select, textarea {
font-size: 16px; /* 阻止 iOS 缩放 */
}
114. 水平滚动条隐藏但仍可滚动
.scroll-x {
overflow-x: auto;
scrollbar-width: none; /* Firefox */
}
.scroll-x::-webkit-scrollbar {
display: none; /* Chrome, Safari */
}
115. 文字超出自动渐隐遮罩
.fade-text {
position: relative;
max-width: 300px;
white-space: nowrap;
overflow: hidden;
}
.fade-text::after {
content: '';
position: absolute;
right: 0;
width: 50px;
height: 100%;
background: linear-gradient(to right, transparent, white);
}
116. 图片 hover 翻转效果
.img-flip {
transition: transform 0.6s;
transform-style: preserve-3d;
}
.img-flip:hover {
transform: rotateY(180deg);
}
117. 全屏响应式封面图布局
.fullscreen-banner {
height: 100vh;
background: url('banner.jpg') center/cover no-repeat;
display: flex;
justify-content: center;
align-items: center;
}
118. 卡片点击阴影渐变动效
.card-click {
transition: box-shadow 0.2s;
}
.card-click:active {
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
}
119. 纯 CSS 鼠标跟随光晕(需 JS 辅助)
.cursor-glow {
position: fixed;
top: 0; left: 0;
width: 50px;
height: 50px;
border-radius: 50%;
pointer-events: none;
background: rgba(255, 255, 255, 0.3);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.8);
transition: transform 0.1s linear;
}
用 JavaScript 监听鼠标位置同步
transform: translate(x, y)
120. 无需 JS 的纯 CSS 图片轮播
<input type="radio" name="slider" id="s1" checked>
<input type="radio" name="slider" id="s2">
<input type="radio" name="slider" id="s3">
<div class="slides">
<div class="slide">图1</div>
<div class="slide">图2</div>
<div class="slide">图3</div>
</div>
<style>
.slides {
display: flex;
width: 300%;
transition: transform 0.5s;
}
#s1:checked ~ .slides { transform: translateX(0%); }
#s2:checked ~ .slides { transform: translateX(-100%); }
#s3:checked ~ .slides { transform: translateX(-200%); }
</style>
完整结尾
至此,完整 120 个实用 CSS 技巧已全部整理完毕!覆盖了布局、视觉、交互、性能、动画、小组件、以及实战技巧。
CSS 从不只是样式,它能让你的前端界面更优雅、更灵活、更具表现力。建议你将本份整理收藏,长期复用,在实际项目中不断熟练掌握、优化提升。