问题描述
我正在为图片库制作一个简单的 HTML.图库的每一行可以有 2、3 或 4 张图像.(在一个 2 图像行中,每个图像元素被命名为 type-2
,type-3
和 type-4
也是如此.)
I am working on a simple HTML for an image gallery. Each row of the gallery can have 2, 3 or 4 images. (In an 2-image row, each image element is named type-2
, the same goes to type-3
and type-4
.)
现在我想选择每行的最后一个元素来设置自定义边距.我的 HTML 是这样的:
Now I want to select the last element of each row to set a custom margin. My HTML is like this:
我认为下面的 CSS 会起作用,但它没有:
I think the following CSS would work but it didn't:
这个 CSS 选择的是:
What this CSS selects is:
我可以编辑我的 HTML 来实现我想要的,但出于好奇,是否有某种 CSS 可以做到这一点?
I can edit my HTML to achieve what I want, but just out of curiosity, is there some kind of CSS for this?
此问题可能与询问 nth-child
和 nth-of-type
是否可以应用于类——不是元素.我已经知道答案是否定的.我真正想要的是一个纯 CSS 解决方案/hack,而选择的答案就是这样做的.
this question may look like a duplicate of questions asking if nth-child
and nth-of-type
can be applied to classes -- not elements. I already knew the answer is no. What I'm really asking for is a pure CSS solution/hack for it, and the chosen answer did just that.
推荐答案
只需 CSS hack,无需修改您的标记,您就可以执行以下操作:
With only CSS hacks, without modifying your markup, you can do something like the below:
演示小提琴
这篇关于如何对类使用 nth-of-type -- 而不是元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!