久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何對類使用 nth-of-type -- 而不是元素

how to use nth-of-type for classes -- not elements(如何對類使用 nth-of-type -- 而不是元素)
本文介紹了如何對類使用 nth-of-type -- 而不是元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在為圖片庫制作一個簡單的 HTML.圖庫的每一行可以有 2、3 或 4 張圖像.(在一個 2 圖像行中,每個圖像元素被命名為 type-2type-3type-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-childnth-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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

CSS selector when :target empty(:target 為空時的 CSS 選擇器)
Does the CSS direct decendant (gt;) not have any value in selectivity?(CSS 直接后代 (gt;) 在選擇性方面沒有任何價值嗎?)
Using querySelectorAll(). Is the result returned by the method ordered?(使用 querySelectorAll().方法返回的結果是否有序?)
Safari bug :first-child doesn#39;t update display:block when items are removed with JS(Safari 錯誤:當使用 JS 刪除項目時,first-child 不更新 display:block)
nth-Child CSS selectors(nth-子 CSS 選擇器)
Using same ID for multiple HTML tags?(對多個 HTML 標簽使用相同的 ID?)
主站蜘蛛池模板: 男人的天堂久久 | 狠狠综合久久av一区二区老牛 | 羞羞视频在线观看网站 | 国产精品视频网 | 亚洲国产成人一区二区 | 亚洲精品久久久久中文字幕欢迎你 | 免费a在线 | 亚洲一区二区国产 | 亚洲激精日韩激精欧美精品 | 成人伊人| 91欧美精品成人综合在线观看 | 亚洲黄色av | 91视频久久 | 亚洲国产欧美精品 | 免费一区二区三区在线视频 | 日韩精品一区二区三区中文字幕 | 精品一区二区久久久久久久网站 | 西西裸体做爰视频 | 日韩色视频 | 国产a区| 久久88 | 欧美视频第三页 | 天天综合网天天综合色 | 成人高清视频在线观看 | 国产女人与拘做受免费视频 | 在线观看视频你懂得 | 亚洲第一网站 | 精品国产精品国产偷麻豆 | 黄色日本视频 | 国产高清精品在线 | 欧美激情在线播放 | 色橹橹欧美在线观看视频高清 | 久久不卡| 成人精品在线观看 | 久久久久9999亚洲精品 | 国产精品久久久久久久久久免费看 | 91精品国产乱码久久久久久久久 | www.亚洲一区二区三区 | 亚洲精品一二区 | 国产中文字幕在线 | 色综合欧美 |