本文介绍了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 中奇数/偶数子元素的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
相关文档推荐
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
Scale background image to fit ie8 window(缩放背景图像以适合 ie8 窗口)
@fontface in IE7 (IETEster) not working properly(IE7 (IETEster) 中的@fontface 无法正常工作)
Safari 5.1 breaks CSS table cell spacing(Safari 5.1 打破 CSS 表格单元格间距)
Put in bold part of description in metatag Drupal module(将描述的粗体部分放在元标记 Drupal 模块中)
-
• 错误 [ERR_REQUIRE_ESM]:不支持 ES ...
-
• vue中yarn install报错:info There ...
-
• 为什么 Chrome(在 Electron 内部)会...
-
• “aria-hidden 元素不包含可聚焦元素...
-
• 使用选择器在 CSS 中选择元素的前一...
-
• js报错:Uncaught SyntaxError: Unex...
-
• layui怎么刷新当前页面?...
-
• 将模式设置为“no-cors"时使用 ...
-
• 类型错误:window.require 不是函数...
-
• 跨域读阻塞 (CORB)...
-
• node.js安装依赖包 yarn install inf...
-
• Google.com 和 clients1.google.com/...
-
• 错误 [ERR_REQUIRE_ESM]:不支持 ES ...
-
• vue中yarn install报错:info There ...
-
• 为什么 Chrome(在 Electron 内部)会...
-
• “aria-hidden 元素不包含可聚焦元素...
-
• 使用选择器在 CSS 中选择元素的前一...
-
• js报错:Uncaught SyntaxError: Unex...
-
• layui怎么刷新当前页面?...
-
• 将模式设置为“no-cors"时使用 ...
-
• 类型错误:window.require 不是函数...
-
• 跨域读阻塞 (CORB)...
-
• node.js安装依赖包 yarn install inf...
-
• Google.com 和 clients1.google.com/...