問題描述
我正在為圖片庫制作一個簡單的 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:
<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 會起作用,但它沒有:
I think the following CSS would work but it didn't:
.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 選擇的是:
What this CSS selects is:
<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 可以做到這一點?
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:
[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 */
}
演示小提琴
這篇關于如何對類使用 nth-of-type -- 而不是元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!