Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
  • css 小技巧2

css 小技巧2

1. currentColor:颜色继承

它会自动继承元素的 color 属性值,让边框、背景等样式与文字颜色同步,完美遵循 “DRY(Don't Repeat Yourself)” 原则,后续修改颜色时也只需改一处。

.container button {
  color: #0066cc; /* 主色调 */
  border: 2px solid currentColor; /* 边框继承文字颜色 */
  background: transparent;
}

/* hover时同步变色 */
.container button:hover {
  color: #f99; /* 加深主色调 */
  /* 边框会自动变成 #f99,无需重复定义 */
}

2. ::marker 伪元素让我们能直接控制列表符号的样式,颜色、尺寸、字体都能自定义。

 /* 定制有序列表符号 */
  ol li::marker {
    color: #e91e63; /* 粉色符号 */
    font-size: 1rem; /* 放大符号 */
    font-weight: bold;
  }

  /* 定制无序列表符号(部分浏览器支持) */
  ul li::marker {
    content: "✨"; /* 用 emoji 当符号 */
  }

is ():简化多选择器重复写法

/* 传统写法 */
header p,
main p,
footer p {
  margin: 8px 0;
  line-height: 1.6;
}

/* :is() 简化写法 */
:is(header, main, footer) p {
  margin: 8px 0;
  line-height: 1.6;
}

Edit this page
最近更新: 2025/12/2 01:46
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人