15

我正在为图片库制作一个简单的 HTML。图库的每一行可以有 2、3 或 4 张图像。(在 2 图像行中,每个图像元素都命名为type-2type-3和也是如此type-4。)

现在我想选择每行的最后一个元素来设置自定义边距。我的 HTML 是这样的:

<div id="content">
    <div class="type-2"></div>
    <div class="type-2"></div> <!-- I want this, 2n+0 of type-2 -->
    <div class="type-3"></div>
    <div class="type-3"></div>
    <div class="type-3"></div> <!-- this, 3n+0 of type-3 -->
    <div class="type-4"></div>
    <div class="type-4"></div>
    <div class="type-4"></div>
    <div class="type-4"></div> <!-- this, 4n+0 of type-4 -->
    <div class="type-2"></div>
    <div class="type-2"></div> <!-- this -->
    <div class="type-3"></div>
    <div class="type-3"></div>
    <div class="type-3"></div> <!-- this -->
    <div class="type-4"></div>
    <div class="type-4"></div>
    <div class="type-4"></div>
    <div class="type-4"></div> <!-- this -->
</div>

我认为下面的 CSS 会起作用,但它没有:

.type-2:nth-of-type(2n+0) {margin-right:0;}
.type-3:nth-of-type(3n+0) {margin-right:0;}
.type-4:nth-of-type(4n+0) {margin-right:0;}

这个 CSS 选择的是:

<div id="content">
    <div class="type-2"></div>
    <div class="type-2"></div> <!-- selected by .type-2:nth-of-type(2n+0) -->
    <div class="type-3"></div> <!-- selected by .type-3:nth-of-type(3n+0) -->
    <div class="type-3"></div>
    <div class="type-3"></div>
    <div class="type-4"></div>
    <div class="type-4"></div>
    <div class="type-4"></div> <!-- selected by .type-4:nth-of-type(4n+0) -->
    <div class="type-4"></div>
    <div class="type-2"></div> <!-- selected by .type-2:nth-of-type(2n+0) -->
    <div class="type-2"></div>
    <div class="type-3"></div> <!-- selected by .type-3:nth-of-type(3n+0) -->
    <div class="type-3"></div>
    <div class="type-3"></div>
    <div class="type-4"></div>
    <div class="type-4"></div> <!-- selected by .type-4:nth-of-type(4n+0) -->
    <div class="type-4"></div>
    <div class="type-4"></div>
</div>

我可以编辑我的 HTML 来实现我想要的,但出于好奇,是否有某种 CSS 用于此目的?

编辑:这个问题可能看起来像询问是否nth-child并且nth-of-type可以应用于类的问题的重复 - 而不是元素。我已经知道答案是否定的。我真正想要的是一个纯 CSS 解决方案/hack,而选择的答案就是这样做的。

4

4 回答 4

15

这对于 CSS Selectors Level 3 是不可能的(至少,没有 CSS hack、额外的标记或 JavaScript)。

CSS Selectors Level 4 草案提出了:nth-match()伪类,它可以做你想做的事:

:nth-match(2n+0 of .type-2) {margin-right:0;}
:nth-match(3n+0 of .type-3) {margin-right:0;}
:nth-match(4n+0 of .type-4) {margin-right:0;}

这还没有被我知道的任何浏览器实现,但是有一个bug可以在 Chrome 中实现。

于 2013-10-19T17:51:06.627 回答
10

仅使用 CSS hack,无需修改标记,您就可以执行以下操作:

[class*=' type-'], [type^='type-']{ /* Set all the divs to float: left initially */
    float: left;
    content: url('http://static.adzerk.net/Advertisers/db5df4870e4e4b6cbf42727fd434701a.jpg');
    height: 100px; width: 100px;
}

.type-2 + .type-2 + div{
    clear: both; /* Clear float for a div which follows two div with class type-2 */
}

.type-3 + .type-3 + .type-3 + div {
    clear: both; /* Clear float for a div which follows three div with class type-3 */
}

.type-4 + .type-4 + .type-4 + .type-4 + div {
    clear: both; /* /* Clear float for a div which follows four div with class type-4 */
}

演示小提琴

于 2013-10-19T17:31:59.430 回答
7

nth-child并且nth-of-type是伪类,只能应用于元素。

.layout:nth-of-type是类的伪,因此不起作用。

Jquery 的 EQ 可能会提供一个解决方案

http://api.jquery.com/eq/

于 2013-10-19T17:09:25.637 回答
3

简短的回答

没有办法做你在纯css中要求的事情。

为什么

:nth-of-type只能用于像这样的元素选择器:

div:nth-of-type(2n+0) { margin-right: 0; }

参考链接

可能的 JS 解决方案

不过,您可以使用一些 jQuery 试试运气:

var $this;

$('[class^="type-"]').each(function (){

  $this = $(this);

  if ( $this.attr('class') !== $this.next().attr('class') )
    $this.addClass('last');

});

然后使用.type-2.last访问您的“最后一行类型”。

演示

Heres a demo.

于 2013-10-19T17:22:38.177 回答