本文介紹了nth-child 中奇數/偶數子元素的問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個這樣的網站:
<html xmlns="http://www.w3.org/1999/xhtml"><頭><標題></標題><link rel="stylesheet" type="text/css" href="article_style.css"/></頭><身體><div 類="節"><!--<h1>標題</h1>-->段落</div>段落</div>段落</div></div><div 類="節">段落</div>段落</div>段落</div></div></身體></html>這是 CSS:
div.section{邊框:1px純黑色;}div.section div:nth-child(偶數){顏色:綠色;}div.section div:nth-child(奇數){紅色;}
結果如下:
這沒關系,因為在 each 部分中,奇數 div 為紅色,偶數為綠色.但是當我在第一部分的開頭添加標題時(示例中的注釋代碼)我得到了這個:
我不想那樣.我希望像以前一樣,但只是在第一部分有一個標題.所以首先是標題,然后是紅色段落.
解決方案 使用 nth-of-type
改為:
現場演示
div.section{邊框:1px純黑色;}div.section div:nth-of-type(偶數){顏色:綠色;}div.section div:nth-of-type(奇數){紅色;}
I have a web site like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="article_style.css" />
</head>
<body>
<div class="section">
<!--<h1>header</h1>-->
<div>
paragraph
</div>
<div>
paragraph
</div>
<div>
paragraph
</div>
</div>
<div class="section">
<div>
paragraph
</div>
<div>
paragraph
</div>
<div>
paragraph
</div>
</div>
</body>
</html>
and this is CSS:
div.section
{
border: 1px solid black;
}
div.section div:nth-child(even)
{
color: Green;
}
div.section div:nth-child(odd)
{
color: Red;
}
And this is the result:
This is OK because I get red for odd div and green for even in each section.
But when I add header at the begginig of first section (commented code in sample) I get this:
I don't want that. I want the to have like before, but just with a header in first section. So at first header and then red paragraph.
解決方案 Use nth-of-type
instead:
Live Demo
div.section
{
border: 1px solid black;
}
div.section div:nth-of-type(even)
{
color: Green;
}
div.section div:nth-of-type(odd)
{
color: Red;
}
這篇關于nth-child 中奇數/偶數子元素的問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)
-
• 在javascript中將GBK轉UTF-8的實例...
-
• AJAX在GBK編碼頁面中傳中文參...
-
• 為什么 .class:last-of-type 不能按...
-
• CSS3 的 :root 偽類和 html 有什么...
-
• 是否有 CSS 不等于選擇器?...
-
• CSS :not(:last-child):after 選擇器...
-
• 使用選擇器在 CSS 中選擇元素...
-
• CSS中的逗號,使用相同CSS的多...
-
• 無子但非空的 CSS 選擇器...
-
• 為什么在css中使用空格分隔事...
-
• 如何使用 nth-child 對具有行跨...
-
• 是否可以在 CSS3 選擇器中使用...
-
• 在javascript中將GBK轉UTF-8的實例...
-
• AJAX在GBK編碼頁面中傳中文參...
-
• 為什么 .class:last-of-type 不能按...
-
• CSS3 的 :root 偽類和 html 有什么...
-
• 是否有 CSS 不等于選擇器?...
-
• CSS :not(:last-child):after 選擇器...
-
• 使用選擇器在 CSS 中選擇元素...
-
• CSS中的逗號,使用相同CSS的多...
-
• 無子但非空的 CSS 選擇器...
-
• 為什么在css中使用空格分隔事...
-
• 如何使用 nth-child 對具有行跨...
-
• 是否可以在 CSS3 選擇器中使用...
css3
html5
canvas
域名頁
域名停放
地板
扁平
在線客服
動畫特效
按鈕切換
動畫模板
360
angular
jquery
svg
游戲模板
物流網站
博客
攝影
導航
小說源碼
郵件群發
蘋果cms
微擎微贊
微商
訂單系統
小程序
電影源碼
微信程序
帝國cms
養生網
挖礦網
java視頻
視頻打賞
thinkphp
蜘蛛池
discuz模板
淘寶客
音樂
分發系統
o2o
微擎
視頻教程
商城
掃碼點餐
零售系統
進銷存系統
bootstrap
商城模板
商務合作
廣告設計
驗證碼
門戶
ar
OElove
漫畫網
全景
視頻
區塊鏈
虛擬幣
你畫我猜
卡券
微小區
交友
小游戲
3d
刷單
小米
資源
ai