我有一些我認為可能很龐大并且可以簡化的代碼。我沒有找到任何對我的情況有幫助的東西。我正在努力做到這一點,以便當我將滑鼠懸停在 a div, an上h2并p加下劃線時。它像這樣作業正常:
.test:hover h2, .test:hover p {
text-decoration: underline;
}
但我想知道是否可以以某種方式簡化它,而不必重復.test:hover兩次。
uj5u.com熱心網友回復:
有一個新的偽類:is將允許這樣做..
:is()CSS 偽類函式將選擇器串列作為其引數,并選擇可以由該串列中的選擇器之一選擇的任何元素。
.test:hover :is(h2, p) {
color: red;
text-decoration: underline;
}
<div class="test">
<h2>Heading</h2>
</div>
<div class="test">
<p>Lorem ipsum dolor sit amet.</p>
</div>
uj5u.com熱心網友回復:
如果您不需要支持 Internet Explorer,這可以通過:is偽類來完成:
.test:hover :is(h2, p) {
text-decoration: underline;
}
<div class="test">
<h1>An H1</h1>
<h2>An H2</h2>
<p>A paragraph</p>
</div>
另一種選擇是利用像 Sass 或 Less 這樣的 CSS 前處理器,它們都支持嵌套,這可以生成 DRY-er、更具表現力的樣式源代碼。不過,在您的情況下,這可能有點矯枉過正。SCSS這是 Sass格式的示例:
.test:hover {
h2, p {
text-decoration: underline;
}
}
uj5u.com熱心網友回復:
你可以使用它:
:is(h1, p) .test:hover {
text-decoration: underline;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453127.html
上一篇:其他影像消失在另一個下
下一篇:將元素縮放到像素大小
