发布于 2026-01-05 9 阅读
0

你可能不知道的CSS技巧。DEV全球项目展示挑战赛,由Mux赞助:快来展示你的项目吧!

你可能不知道的CSS小技巧。

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

今天我将和大家分享一些很棒的CSS选择器。

我们先从计数器开始。

要设置编号列表中数字的样式,我们需要使用名为 CSS 计数器的属性。CSS 计数器允许您根据内容在文档中的位置调整其外观。

<ul class="list">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
</ul>
Enter fullscreen mode Exit fullscreen mode
li {
    font-size: 40px;
    margin-bottom: 20px;
    counter-increment: li;
}

.list li::before {
    content: counter(li);
    margin-right: 10px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: inline-block;
    background-color: #f3b70f;
    color: white;
    text-align: center;
    line-height: 50px;
}
Enter fullscreen mode Exit fullscreen mode

替代文字

::选择。

::selection 伪元素用于 DOM 中高亮显示的元素。这是我最喜欢的伪元素之一。它的语法如下:
替代文字

p::selection {
  color: white;
  background-color: green;
}
Enter fullscreen mode Exit fullscreen mode

接下来是attr()函数。

它用于检索所选元素的属性值,并在样式表中使用该值。

<h2 class="title" data-count="01">Section</h2>
<h2 class="title" data-count="02">Section</h2>
<h2 class="title" data-count="03">Section</h2>
Enter fullscreen mode Exit fullscreen mode
.title {
    font-size: 35px;
    letter-spacing: 3px;
}

.title::before {
    content: attr(data-count);
    font-size: 1.2em;
    color: steelblue;
    margin-right: 10px;
}
Enter fullscreen mode Exit fullscreen mode

替代文字

为网页文本添加描边

<h2>Css is Awesome</h2>
Enter fullscreen mode Exit fullscreen mode
h2 {
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke: 3px tomato;
}
Enter fullscreen mode Exit fullscreen mode

替代文字

文章来源:https://dev.to/sasscrafter/css-tricks-you-probously-didn-t-know-about-3gad