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; }
.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
| .parent { display: flex; align-items: center; }
.parent { display: grid; place-items: center; }
.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
| .clearfix::after { content: ""; display: block; clear: both; }
.clearfix { overflow: hidden; }
.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-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%; 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; }
.every-third:nth-child(3n) { border-right: none; }
h2 ~ p { margin-top: 1rem; }
h2 + p { font-weight: bold; }
|
总结
CSS 有很多实用技巧,掌握这些技巧可以帮助你更高效地完成样式开发。建议收藏本文,随时查阅!