nth-child中奇数/偶数子元素的问题 [英] Problem with odd/even child elements in nth-child
本文介绍了nth-child中奇数/偶数子元素的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的网站:
<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>
这是CSS:
div.section
{
border: 1px solid black;
}
div.section div:nth-child(even)
{
color: Green;
}
div.section div:nth-child(odd)
{
color: Red;
}
这是结果:
这没问题因为我在每个部分中获得了红色的奇数div和绿色。
但是当我在第一部分的begginig(样例中的注释代码)添加头部时,我得到:
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.
推荐答案
使用 nth-of-type
:
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中奇数/偶数子元素的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文