繁体   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