css – 包含Less的变量的多个嵌套选择器

前端之家收集整理的这篇文章主要介绍了css – 包含Less的变量的多个嵌套选择器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在这些方面构建一些CSS:
  1. h1,h2,h3,h4,h5,h6 {some rule}
  2. h1 a,h2 a,h3 a,h4 a,h5 a,h6 a {color: inherit;}
  3. h1 span,h2 span,h3 span,h4 span,h5 span,h6 span {another rule;}

如果我可以创建一个这样的变量将是有用的:

  1. @headings: h1,h6;

然后可能做这样的事情:

  1. @{headings} {
  2. & a {color: inherit;}
  3. }

不幸的是,这给了我:

  1. h1,h6 a {
  2. color: inherit;
  3. }

我想要的是什么?这是我想要做的简单版本,但我也觉得有用的是处理HTML输入类型和经常出现在一起的多个选择器的其他实例.

解决方法

#1
除了@ helderdarocha的答案和 https://stackoverflow.com/a/23954580/2712740中给出的答案之外,还有一个解决方案.也许这个可能看起来更清楚一点:
  1. // define header list as usual just
  2. // put a mixin call with some predefined name there
  3. h1,h6 {.headings}
  4.  
  5. // now to add styles/childs to the header list just add the mixin definitions:
  6.  
  7. .headings() {
  8. some: rule;
  9. }
  10.  
  11. .headings() {
  12. a {color: inherit}
  13. }
  14.  
  15. .headings() {
  16. span {another: rule}
  17. }
  18.  
  19. // etc.

解决方案的局限性在于h1,h3 …… {}和.headings应该定义在同一级别.另外,重要的是要记住,所有这些样式都会在h1,h3 … {}定义的位置输出到CSS,而不是在.headings定义的位置,因此如果你有,它可能会破坏你的级联覆盖一些).

#2
alt.解决方案我是从https://stackoverflow.com/a/23954580/2712740#3复制粘贴,基本上它与#1相同但没有限制(只有更多特殊的可怕符号):

  1. // the "variable":
  2. .headings(@-) {
  3. h1,h6
  4. {@-();}}
  5.  
  6.  
  7. // usage:
  8.  
  9. .headings({
  10. some: rule;
  11. });
  12.  
  13. .headings({
  14. a {color: inherit}
  15. });
  16.  
  17. .headings({
  18. span {another: rule}
  19. });
  20.  
  21. //etc.

猜你在找的CSS相关文章