CSS技巧:nth-of-type()

Rockyboy_ruby

嗨,我正在寻找一些特别的东西,甚至不确定是否可以使用纯CSS完成:

<div-1> 
  <div-x></div-x>
  <div-x></div-x>
  <div-x></div-x>
  <div-x></div-x>
<div-1> 

我想在scss中做的是

if <div-x> render count is even
  then only apply &:last-child{ something; } to div number len(<div-x>)-1
end

EG:如果div-x渲染为4,则将伪元素应用于第3 div-x,否则不要

我在下面尝试过,但它适用于所有奇数元素

&:nth-of-type(odd) {
  border-bottom: none;
}

任何提示/帮助将不胜感激,谢谢!

幽灵般的

您可以使用:nth-last-of-type

.wrapper div:nth-of-type(even) + div:nth-last-of-type(2) {
    background-color: #ADD8E6;
}

/* for when there are only two occurrences of this element type */
.wrapper div:nth-last-of-type(2):first-of-type {
    background-color: #ADD8E6;
}

div仅当第一个选择器紧接着出现偶数个时,才会选择倒数第二个样式div第二个选择器div也是倒数第二个div(只有两个divs时)为第一个选择器设置样式我仅出于可读性而使用了两个声明。

查看演示。

但是,请确保对伪类的支持足以满足您的要求。页面(2013年)指出:

:nth-last-of-type是在CSS选择器模块3中引入的,这意味着旧版本的浏览器不支持它。但是,现代浏览器的支持是无可挑剔的,并且新的伪选择器已广泛用于生产环境中。如果您需要较早的浏览器支持,则可以使用IE的polyfill或以非关键方式使用这些选择器,也可以使用渐进式增强,建议您这样做。

MDN页指出,从给定版本开始,以下浏览器具有“基本支持”:

┌────────┬─────────────────┬───────────────────┬───────┬────────┐
│ Chrome │ Firefox (Gecko) │ Internet Explorer │ Opera │ Safari │
├────────┼─────────────────┼───────────────────┼───────┼────────┤
│   4.0  │   3.5 (1.9.1)   │        9.0        │  9.5  │   3.2  │
└────────┴─────────────────┴───────────────────┴───────┴────────┘

对于移动浏览器:

┌─────────┬────────────────────────┬───────────┬──────────────┬───────────────┐
│ Android │ Firefox Mobile (Gecko) │ IE Mobile │ Opera Mobile │ Safari Mobile │
├─────────┼────────────────────────┼───────────┼──────────────┼───────────────┤
│   2.1   │       1.0 (1.9.1)      │    9.0    │     10.0     │      3.2      │
└─────────┴────────────────────────┴───────────┴──────────────┴───────────────┘

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CSS Nth of Type 未定位

来自分类Dev

CSS nth-of-type不起作用

来自分类Dev

CSS nth-of-type未按顺序选择

来自分类Dev

CSS3如何计算:nth-of-type项目?

来自分类Dev

CSS3如何计算:nth-of-type项目?

来自分类Dev

CSS:nth-of-type(2) 未应用于类

来自分类Dev

CSS中的nth-type和nth-child无法正常工作

来自分类Dev

CSS 3 nth-child(n)不起作用#table_id td a:nth-of-type(n)

来自分类Dev

CSS:nth-of-type选择器在不匹配的子项上中断

来自分类Dev

插入无类div会弄乱css .class:nth-of-type(n):在建立索引之前

来自分类Dev

CSS nth-of-type选择器,带有嵌套元素

来自分类Dev

:nth-of-type选择器会覆盖所有其他CSS选择器

来自分类Dev

类型的第n个元素的css选择器-nth-of-type()的怪异实现

来自分类Dev

如何将多个 CSS 动画 nth-of-type 选择器合二为一?

来自分类Dev

nth-of-type 不起作用

来自分类Dev

既不是nth-child也不是nth-of-type

来自分类Dev

:nth-child() 和 :nth-of-type() 不起作用

来自分类Dev

显示多个div的CSS技巧

来自分类Dev

CSS input [type =“ type”]是否需要引号?

来自分类Dev

Bootstrap 3和表单CSS技巧问题

来自分类Dev

Soundcloud播放器背景CSS技巧

来自分类Dev

iPhone解锁屏幕文字CSS技巧

来自分类Dev

背景图像模糊的CSS技巧

来自分类Dev

在更多列中向左浮动CSS技巧

来自分类Dev

CSS技巧透明和减去背景

来自分类Dev

如何使用input:nth-of-type(n)选择输入标签?

来自分类Dev

在jQuery中匹配两个单独的nth-of-type?

来自分类Dev

如何在此处正确实现nth-of-type?

来自分类Dev

具有nth-of-type()的jQuery选择器