【问题标题】:how to use nth-of-type for classes -- not elements [duplicate]如何对类使用第n个类型-而不是元素[重复]
【发布时间】:2015-02-16 11:48:19
【问题描述】:

我正在为图片库制作一个简单的 HTML。图库的每一行可以有 2、3 或 4 张图像。 (在 2 张图像行中,每个图像元素都命名为 type-2type-3type-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-childnth-of-type 是否可以应用于类而不是元素的问题重复。我已经知道答案是否定的。我真正想要的是一个纯 CSS 解决方案/hack,而选择的答案就是这样做的。

【问题讨论】:

  • 你应该只将每一行包装在一个 div 中。
  • @JamesMontagne 和 Oriol 感谢您指出重复项,我在发布之前进行了搜索,但我的关键字不足以解决这些问题。

标签: html css css-selectors


【解决方案1】:

这在 CSS 选择器级别 3 中是不可能的(至少,没有 CSS hack、额外的标记或 JavaScript)。

CSS 选择器级别 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 filed 可以在 Chrome 中实现它。

【讨论】:

  • 感谢@Brian 提供的一些非常有用的信息!
【解决方案2】:

仅使用 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 */
}

Demo Fiddle

【讨论】:

  • 上面的例子本质上是设置no。根据类型,每行显示的 div 数。如果你想应用额外的 CSS,应该还是可以的。
  • 这么简单又不错的解决方法,谢谢!另外,感谢您的演示!
  • @Harry 为什么不用.type-n 而不是[class*="type-n"]
  • @hitautodestruct:我们可以使用 .type-n 也 mate (事实上对于这种情况,这是最好的)。我只是想表明我们也可以将属性选择器与 data* HTML5 属性一起使用。昨天深夜,当我回答这个问题时,一定是在这之间打瞌睡:D
【解决方案3】:

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

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

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

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

【讨论】:

  • 感谢您对nth-childnth-of-type 功能的确认。 JS 很好,但因为我想要一个纯 CSS 解决方案,所以我接受了@Harry 的回答。
【解决方案4】:

简答

在纯 css 中无法做到你所要求的。

为什么

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

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

Reference link.

可能的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.

【讨论】:

  • 感谢您提供的信息和精彩的演示!我希望我能接受你的回答,但我选择了@Harry,因为它提供了一个纯 CSS 解决方案。
  • @HoangHuynh 没问题,很高兴您不必解决使用 JS 的 css 解决方案 :)
猜你喜欢
  • 2019-09-16
  • 2017-09-22
  • 1970-01-01
  • 2017-06-10
  • 2015-06-05
  • 2012-06-10
  • 2021-05-18
相关资源
最近更新 更多