css - 单个 div 上的多个伪类

标签 css css-selectors

我正在尝试在一组无限的 Div 上创建外边框效果。

Image of what I mean

所以我尝试混合使用伪类,例如 :first-of-type 和 :nth-of-type(#) 但 :nth-of-type(#) 覆盖了 :first-of -类型。

.quarter > div:first-of-type { border-right:dashed #cccccc 1px; border-bottom: dashed #cccccc 1px; padding: 10px; box-sizing: border-box; }
.quarter > div:nth-of-type(3n+1) { border-right: none; border-bottom: dashed #cccccc 1px; }

所以除了第三行的最后一个和第一个,我需要用 Jquery 进行编码,所以这应该缩短所有内容,但我只需要知道这是否可行。哦,如果有人有更好的方法,请务必帮助兄弟。 :)

最佳答案

:first-of-type:nth-of-type(3n+1) 的子集,对于 n = 0(因为 3(0) + 1 = 1).这就是您看到覆盖的原因,因为这两个规则都匹配第一个 div

由于您的两个选择器都是同样具体的,并且您的第二条规则中没有其他声明不存在于您的第一条规则中,您只需将您的两条规则的位置交换为您的 :first-of-类型规则优先:

.quarter > div:nth-of-type(3n+1) { border-right: none; border-bottom: dashed #cccccc 1px; }
.quarter > div:first-of-type { border-right:dashed #cccccc 1px; border-bottom: dashed #cccccc 1px; padding: 10px; box-sizing: border-box; }

关于css - 单个 div 上的多个伪类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817075/

相关文章:

javascript - 为什么 "Read More"按钮在 Div 中不起作用?

基于for属性的CSS目标标签?

python - 如何使用 Selenium 和 Python 3.7.4 从一系列复选框中找到特定复选框

html - 如何使用css将div id ="main"内的3个div移动到中心?

javascript - 在 jQuery UI 日期选择器中标记选定的日期

html - 创建在鼠标悬停时展开的号召性用语按钮

javascript - Bootstrap Affix Nav 导致下面的div跳起来

html - 如何将 nth-child 与 :hover to style multiple children? 一起使用

html - 带有标题标签的第一子伪选择器问题

html - CSS::after 选择器在我的菜单中换行并移动文本