簡體   English   中英

我可以將樣式應用於 CSS 或 Sass 中的所有偽選擇器嗎?

[英]Can I apply a style to all pseudo selectors in CSS or Sass?

是否可以使用 CSS 或 Sass 將樣式應用於所有錨標記的偽選擇器

就像是

a:* {
    color: #900;  
}

代替

a {
    &:hover, &:link, &:active, &:visited {
        color: #900; 
    } 
}

我只想重置標准樣式。 在 CSS 中,通配符可用於將樣式應用於所有元素……但是如何應用於所有偽選擇器呢?

簡短回答:不,不直接


然而,mixin 可以用來產生類似的效果。

// Sets the style only for pseudo selectors
@mixin setLinkSelectorStyle {
  &:hover, &:link, &:active, &:visited {
        @content;
    }
}

// Sets the style to pseudo selectors AND base default anchor
@mixin setLinkStyleAll {
  &, &:hover, &:link, &:active, &:visited {
        @content;
    }
}

a {
  color:red;
  @include setLinkSelectorStyle {
    color:gold;
  }
}

a.specialLink {
  @include setLinkStyleAll {
    color:purple;
  }
}

[使用http://sassmeister.com/編譯SASS的例子]

 a { color: red; } a:hover, a:link, a:active, a:visited { color: gold; } a.specialLink, a.specialLink:hover, a.specialLink:link, a.specialLink:active, a.specialLink:visited { color: purple; }
 <a>Normal anchor, No href (:link won't work, but other selectors will)</a> <hr /> <a href="#">Normal anchor</a> <hr /> <a class="specialLink">Specific class (no href)</a> <hr /> <a class="specialLink" href="#">Specific class</a>

當 mixin 包含在錨點/類中時,mixin 將為所有偽選擇器創建規則。


刪除舊答案,查看歷史記錄即可。

你可以這樣做:

  &:before, &:after {
      content: 'a';
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM