惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
Y
Y Combinator Blog
P
Proofpoint News Feed
Forbes - Security
Forbes - Security
美团技术团队
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
T
Tor Project blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
U
Unit 42
人人都是产品经理
人人都是产品经理
V2EX - 技术
V2EX - 技术
L
Lohrmann on Cybersecurity
罗磊的独立博客
博客园 - 聂微东
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
大猫的无限游戏
大猫的无限游戏
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
P
Privacy International News Feed
爱范儿
爱范儿
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog
T
Threat Research - Cisco Blogs
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hugging Face - Blog
Hugging Face - Blog
T
Tenable Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
Google Online Security Blog
Google Online Security Blog
D
Docker
Martin Fowler
Martin Fowler
I
Intezer
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security Affairs
T
Tailwind CSS Blog
IT之家
IT之家

博客园 - lzhdim

六种AI生意,从月入一万到月入十万 5个国产大模型实测对比2026:DeepSeek、Kimi、豆包、通义、智谱哪个好用 豆包、元宝、千问、Kimi、DeepSeek,这么多AI软件,到底该用哪个? 微软免费开源了一个 Linux 操作系统,没错!是微软出品 一次看懂5种咖啡冲煮方式:意式、手冲、法压、冷萃、摩卡壶到底有什么区别 JavaScript运算符与表达式详解 C#开发的ScreenSaver屏保应用 - 开源研究系列文章 - 个人小作品 WinForm 中跨线程操作 UI 的解决方案 JavaScript变量与数据类型详解 苹果良心!iOS 27适配设备公布:30款机型 7年前的iPhone 11还能升级 C++图形用户界面开发入门 10个不该免费的GitHub神级项目 JavaScript语言全面概述:从历史到现代实践 一张图看懂常见咖啡 C盘空间多出来4GB:谷歌服软 Chrome本地AI大模型可禁用、删除了 96GB显存运行230B大模型!七彩虹灵创K16笔记本评测:160W性能释放 AMD锐龙AI Max+ 395加持全能移动AI工作站 AI PC最强存储搭档!雷克沙NM1090 PRO 8TB固态评测:14400MB/s满速不缩水 C#已经不是当年的C#了——我用它写了个微秒级数据库引擎 EasyTool:轻量全能的 .NET 工具类库 SQL 入门 17:MySQL 数据类型:从字符串到 JSON 的全面解析 SQL 入门 16:SQL 事务隔离级别与死锁解析(易懂) 原相3955XM+TL3228主控!小米电竞鼠标2全面评测:原生双8K究竟有多强 PCIe 4.0火力全开:闪迪奥丁马仕GX 7100 NVMe SSD上手 超乎预料的千元级电竞主板!七彩虹iGame B850M Ultra-OC V14主板评测 SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题 名人 - 我的闪存 C#性能优化技巧 SQL 入门 14:SQL 触发器与事件:自动化数据处理 38个实用的JavaScript 技巧 一款基于 .Net WinForm 开发的节点编辑器,纯 GDI 实现,体积仅 100+Kb Masuit.Tools:这个 C# 工具库 SQL 入门 13:SQL 存储过程与函数:封装逻辑与参数处理 49个 JavaScript 代码快捷技巧,让你在 2026 年成为代码高手 20260503 - 个人小作品更新 SQL 入门 12:SQL 视图:创建、修改与可更新视图 C#如何实现Windows系统桌面截图功能... 6999元AMD新旗舰首测!锐龙9 9950X3D2性能解禁:这颗U根本不是给游戏玩家造的 SQL 入门 11:日期时间格式化、IF、CASE的使用 基于C#的校时类库的实现及使用 - 开源项目研究文章
120个 实用CSS 技巧汇总合集
lzhdim · 2026-06-29 · via 博客园 - lzhdim

在前端开发中,CSS 往往是最被低估的一环。但真正优秀的开发者,往往懂得如何用 CSS 写出高效、优雅又强大的界面。

无论你是刚入门的新手,还是正在精进的前端工程师,这 100 个经过精选分类的 CSS 小技巧,都会帮你解决常见痛点,提升页面性能与交互体验。

从布局排版到动画过渡,从表单交互到组件样式,每一个技巧都附有简洁示例,贴近实战,拿来即用。让我们一起重拾 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%; /* 16:9 */}.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: 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: 5px #0ff, 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: 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; }  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: 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:checked ~ .content .tab1-content { display: block; }#tab2:checked ~ .content .tab2-content { display: block; }.tab1-content, .tab2-content { display: none; }

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.CSS 图片放大镜效果

.zoom {  overflow: hidden;}.zoom img {  transition: 0.3s;}.zoom:hover img {  transform: scale(1.2);}

102.CSS 时间轴

.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.CSS 骨架屏(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.CSS 数字翻牌效果(纯动画)

@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: 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.CSS 时间倒计时样式(需 JS 逻辑)

.countdown {  font-family: monospace;  font-size: 2rem;  background: #000;  color: #0f0;  padding: 10px;}

120.滚动提示箭头动画

.scroll-down::after {  content: '↓';  display: block;  animation: bounce 1s infinite;}@keyframes bounce {  0%, 100% { transform: translateY(0); }  50% { transform: translateY(10px); }}

写在最后

以上,就是我今天整理的自实际开发中的 100+ 个高频 CSS 技巧。如果你觉得实用,建议收藏并多加练习,逐步内化为自己的前端“肌肉记忆”。你也可以将这份技巧清单应用到项目优化、组件封装和样式规范中,减少冗余代码,提升 UI 品质。

如果你希望将这些技巧拓展为完整的 UI 组件库或样式手册,也欢迎继续探索,我们可以一起打磨一套专属于你的 CSS 工具箱。