CSS - 将相邻选择器与 nth-of-type 结合使用

标签 css css-selectors

我想做的是选择标题标记后的每四个 DIV。这是我正在使用的代码的基本概念。

<div class="view-content"> 

   <h3>The Heading</h3> 
   <div></div>  
   <div></div>
   <div></div>
   <div>Trying to select this DIV</div>

   <h3>The Heading</h3> 
   <div></div>  
   <div></div>
   <div></div>

   <h3>The Heading</h3> 
   <div></div>  
   <div></div>
   <div></div>
   <div>Trying to select this DIV</div>  
   <div></div>
   <div></div>
   <div></div>
   <div>Trying to select this DIV</div>

</div><!-- .view-content -->

我使用的是 Drupal,所以我仅限于编写 CSS 来安排内容。我试图选择每四个 DIV 的原因是因为一行有四个 DIV,我想从一行的第四个(也是最后一个)DIV 中删除右边距。有些行只有一个或两个 DIV,而其他行有十个或更多。

我最初编写的 CSS(似乎不起作用)是:

div.view-content h3 + div:nth-of-type(4n) {
margin-right: 0px;
}

可能是我想多了,也可能是没有办法。不管怎样,请帮忙!

最佳答案

不幸的是,它出现了there's no nice, general way to do this with CSS3 .从此处发布的其他答案中汲取灵感,您可以将其用于有限数量的元素:

div.view-content h3 + div+div+div+div,
div.view-content h3 + div+div+div+div + div+div+div+div,
div.view-content h3 + div+div+div+div + div+div+div+div + div+div+div+div /*etc*/ {
    margin-right: 0px;
}

http://cssdesk.com/Ty7Gf

关于CSS - 将相邻选择器与 nth-of-type 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905813/

相关文章:

javascript - 使用php存储提交信息并在单独的html页面上显示

html - th 表标签的默认行为

html - 应用于 HTML 页面主体元素的 CSS 选择器

HTML/CSS 网页看起来不正常

javascript - 将鼠标悬停在文本上时,文本会下降然后返回

css - 嵌套主题组件 : apply styles depending on a theme class of the nearest themed parent, 忽略承载主题类的更深层次父级

CSS 选择器以 p 标签中成对出现的最后一个 br 标签为目标?

javascript - 从 jQuery 选择器中排除具有特定属性的元素

CSS3 变换、倾斜和旋转

css - 为什么叫伪类?