【问题标题】:Use "nth-of-type" kind of information as variable in css在 css 中使用“nth-of-type”类型的信息作为变量
【发布时间】:2018-04-03 16:22:05
【问题描述】:

我目前正在学习 CSS,发现自己发现了所有这些可爱的“CSS 形状”(https://css-tricks.com/examples/ShapesOfCSS/),我只需要摆弄它们。尤其是“指针”之一(在底部)。

问题是,我希望有几个指针彼此相邻,没有任何边距。

当我这样做时:

<div class="pointer"></div>
<div class="pointer"></div>
<div class="pointer"></div>
<div class="pointer"></div>
//also changed css to give .pointer a `float: left;` property

由于pointer:after 的定义方式(左边框为白色),所有指针在交叉点处都会有这个丑陋的空白。所以我所做的是:

<div class="pointer"></div>
<div class="pointer" style="z-index: -1;"></div>
<div class="pointer" style="z-index: -2;"></div>
<div class="pointer" style="z-index: -3;"></div>

是的,现在可以了。但是内联样式是一个禁忌。所以它让我思考:有没有办法使用给定 div 是 nth-of-type() 的信息作为变量来定义它的 z-index?比如:

div:nth-of-type(n) {
    //use magic to specify that `z-index = -n;`
}

【问题讨论】:

  • 不使用 CSS ...考虑诸如 SASS/LESS 之类的东西 ...或共享完整代码,因为我们可能会找到一些解决方法

标签: html css sass


【解决方案1】:

您可以尝试使用@forSASS 来实现您想要的。

@for $i from 1 through 3{
  div:nth-of-type(#{$i}) {
    z-index: -$i
  }
}

CSS 输出

div:nth-of-type(1) {
  z-index: -1;
}

div:nth-of-type(2) {
  z-index: -2;
}

div:nth-of-type(3) {
  z-index: -3;
}

您也可以尝试使用LESS

希望这会有所帮助。

【讨论】:

  • 是的,你可以使用 SASS 做很多很酷的事情。很高兴为您提供帮助。
猜你喜欢
  • 2014-11-29
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2014-11-12
相关资源
最近更新 更多