CSS 专业技巧

这篇是什么

CSS 用过几年都会有一些"原来这样写可以省事"的发现 —— 多数不在官方文档显眼位置,但用过一次就再也不会忘。本文汇总 20 多个我用着舒服的小技巧,按主题分组。

CSS 专业技巧

1. CSS Reset(浏览器样式归一)

不同浏览器默认样式不同(margin、padding、字号都有差异),做精细布局前先 reset。最简洁的现代版本:

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

或者更完整的:

html {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

推荐用 Normalize.css 这种成熟方案,自己写难免漏。

2. 流式布局(Flexbox 一行垂直水平居中)

button {
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  outline: none;
  padding: 0;
}

这是 Flexbox 时代最简洁的居中。以前用 position: absolute + transform: translate(-50%, -50%) 或者 display: table-cell + vertical-align,都被这三行干掉。

3. 等高列(过去的痛,现在的甜)

button {
  all: unset;
}

Flexbox 默认 align-items: stretch,子元素自动等高。Grid 一样默认等高。这俩出现前,等高列要写一堆 hack 代码。

4. 响应式图片

/* 添加边框 */
.nav li {
  border-right: 1px solid #666;
}

max-width: 100% 让图片不超过父容器宽度;height: auto 保持宽高比。三行,响应式图片基础写法。

5. 自定义滚动条

/* 去掉边框 */
.nav li:last-child {
  border-right: none;
}

Webkit 系(Chrome / Edge / Safari)用这种 ::-webkit-scrollbar-* 伪元素。Firefox 用 scrollbar-width + scrollbar-color(更标准):

.nav li:not(:last-child) {
  border-right: 1px solid #666;
}

6. 多行文字省略(...)

@font-face {
  font-family: "Dank Mono";
  src:
    /* Full name */
    local("Dank Mono"),
    /* Postscript name */
    local("Dank-Mono"),
    /* Otherwise, download it! */
    url("//...a.server/fonts/DankMono.woff");
}

code {
  font-family: "Dank Mono", system-ui-monospace;
}

单行省略很简单(text-overflow: ellipsis + overflow: hidden + white-space: nowrap),多行靠 -webkit-line-clamp。Webkit 私有但所有主流浏览器都支持。

7. CSS 变量 + 主题切换

body {
  line-height: 1.5;
}

定义在 :root 的变量全局可用。改一处影响所有用到的地方,是 CSS 主题化的基础。

8. 暗色模式自动切换

a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus {
  box-shadow: none;
  outline: #000 dotted 2px;
  outline-offset: .05em;
}

prefers-color-scheme 媒体查询读取系统的颜色偏好。配合 CSS 变量,一个媒体查询切换整套配色。

9. 圆形头像

html,
body {
  height: 100%;
  margin: 0;
}

body {
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  display: -webkit-flex;
  display: flex;
}

border-radius: 50% 是圆形。object-fit: cover 保证图片裁切而不变形。

10. Sticky 粘性定位

body {
  display: grid;
  height: 100vh;
  margin: 0;
  place-items: center center;
}

position: sticky 在元素到达指定位置前是 static,到达后变成 fixed。适合做可滚动到顶时固定的导航 / 表头。

11. Aspect Ratio(宽高比)

ul > li:not(:last-child)::after {
  content: ",";
}

2021 年新增,直接声明宽高比,不需要 padding-top: 56.25% 这种 hack。视频容器、图片占位用得到。

12. Grid 自动列数

li {
  display: none;
}

/* 选择第 1 至第 3 个元素并显示出来 */
li:nth-child(-n+3) {
  display: block;
}

这条响应式 Grid 一行搞定 —— 屏幕够宽,每列至少 250px,有多余空间自动加列。不需要 media query。

13. 文字描边

/* 选择除前3个之外的所有项目,并显示它们 */
li:not(:nth-child(-n+3)) {
  display: none;
}

多个 text-shadow 叠加,模拟描边效果。或者用 -webkit-text-stroke(更直接,但 Firefox 支持稍晚)。

14. Gradient(渐变)

.logo {
  background: url("logo.svg");
}

线性渐变、径向渐变、圆锥渐变(conic-gradient)。圆锥渐变能做"色轮"、"饼图"这种特殊效果。

15. 玻璃磨砂效果

.no-svg .icon-only::after {
  content: attr(aria-label);
}

backdrop-filter: blur(10px) 配半透明背景,做 iOS 风磨砂玻璃。导航栏、卡片、模态背景视觉极佳。

16. 选中文字样式

* + * {
  margin-top: 1.5em;
}

::selection 伪元素覆盖默认的文字选中样式(浅蓝)。用品牌色做选中效果,品牌一致性细节满分。

17. 阻止用户选择

.slider {
  max-height: 200px;
  overflow-y: hidden;
  width: 300px;
}

.slider:hover {
  max-height: 600px;
  overflow-y: scroll;
}

按钮 / 标签 / 装饰元素,设 user-select: none 防止用户双击选中文字(让交互更像原生 App)。

18. 流畅滚动

.calendar {
  table-layout: fixed;
}

scroll-behavior: smooth 让点击锚点链接(<a href="#section">)时浏览器平滑滚动到目标。零 JS 实现。

19. CSS Counter(自动序号)

.list {
  display: flex;
  justify-content: space-between;
}

.list .person {
  flex-basis: 23%;
}

counter-resetcounter-increment 实现自动编号,适合给 h2/h3 自动加"1. 2. 3."这种章节号。比 JS 维护 index 优雅。

20. CSS 性能优化技巧

a[href^="http"]:empty::before {
  content: attr(href);
}

will-change 提示浏览器某属性会变化,提前优化。但不要滥用 —— 用完后及时移除,常驻反而拖慢。

21. Reduced Motion(尊重用户偏好)

a[href]:not([class]) {
  color: #008000;
  text-decoration: underline;
}

有些用户对动画敏感(头晕),操作系统提供了"减少动画"开关。CSS 用 prefers-reduced-motion 媒体查询响应这个偏好,关闭非必要动画。

剩下的几个

.container {
  height: 0;
  padding-bottom: 20%;
  position: relative;
}

.container div {
  border: 2px dashed #ddd;
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
}
img {
  display: block;
  font-family: sans-serif;
  font-weight: 300;
  height: auto;
  line-height: 2;
  position: relative;
  text-align: center;
  width: 100%;
}
img::before {
  content: "We're sorry, the image below is broken :(";
  display: block;
  margin-bottom: 10px;
}

img::after {
  content: "(url: " attr(src) ")";
  display: block;
  font-size: 12px;
}
h2 {
  font-size: 2em;
}

p {
  font-size: 1em;
}
article {
  font-size: 1.25rem;
}

aside .module {
  font-size: .9rem;
}
video[autoplay]:not([muted]) {
  display: none;
}
:root {
  font-size: calc(1vw + 1vh + .5vmin);
}
body {
  font: 1rem/1.6 sans-serif;
}
input[type="text"],
input[type="number"],
select,
textarea {
  font-size: 16px;
}
button:disabled {
  opacity: .5;
  pointer-events: none;
}
br + br {
  display: none;
}
:empty {
  display: none;
}

一句话总结

这些技巧大部分都是 2018 年后陆续加入主流浏览器的现代 CSS。新项目可以放心用,老项目能切就切 —— 同样效果代码量降到 1/3,可维护性提升一大截。

—— 别看了 · 2026
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理 邮箱1846861578@qq.com。
技术教程

CSS新用法 - min()、max()、clamp()、:where()、:is()、:has()

2024-6-6 11:37:19

技术教程

某大佬收藏100+前端工具和网站推荐收藏夹公开

2024-6-14 14:54:32

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索