From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/css/_colon_nth-child/index.html | 218 ++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 files/zh-cn/web/css/_colon_nth-child/index.html (limited to 'files/zh-cn/web/css/_colon_nth-child') diff --git a/files/zh-cn/web/css/_colon_nth-child/index.html b/files/zh-cn/web/css/_colon_nth-child/index.html new file mode 100644 index 0000000000..9cb7fddd6f --- /dev/null +++ b/files/zh-cn/web/css/_colon_nth-child/index.html @@ -0,0 +1,218 @@ +--- +title: ':nth-child' +slug: 'Web/CSS/:nth-child' +tags: + - ':nth-child' + - CSS + - CSS 伪类 +translation_of: 'Web/CSS/:nth-child' +--- +
{{CSSRef}}
+ +

概述

+ +

:nth-child(an+b) 这个 CSS 伪类首先找到所有当前元素的兄弟元素,然后按照位置先后顺序从1开始排序,选择的结果为CSS伪类:nth-child括号中表达式(an+b)匹配到的元素集合(n=0,1,2,3...)。示例:

+ + + +

ab 都必须为整数,并且元素的第一个子元素的下标为 1。换言之就是,该伪类匹配所有下标在集合 { an + b; n = 0, 1, 2, ...} 中的子元素。另外需要特别注意的是,an 必须写在 b 的前面,不能写成 b+an 的形式。

+ +

语法

+ +
{{csssyntax}}
+
+ +

示例

+ +

选择器示例

+ +
+
tr:nth-child(2n+1)
+
表示HTML表格中的奇数行。
+
tr:nth-child(odd)
+
表示HTML表格中的奇数行。
+
tr:nth-child(2n)
+
表示HTML表格中的偶数行。
+
tr:nth-child(even)
+
表示HTML表格中的偶数行。
+
span:nth-child(0n+1)
+
表示子元素中第一个且为span的元素,与 {{Cssxref(":first-child")}} 选择器作用相同。
+
span:nth-child(1)
+
表示父元素中子元素为第一的并且名字为span的标签被选中
+
span:nth-child(-n+3)
+
匹配前三个子元素中的span元素。
+
+ +

Detailed example

+ +

HTML

+ +
<h3><code>span:nth-child(2n+1)</code>, WITHOUT an
+   <code>&lt;em&gt;</code> among the child elements.</h3>
+<p>Children 1, 3, 5, and 7 are selected.</p>
+<div class="first">
+  <span>Span 1!</span>
+  <span>Span 2</span>
+  <span>Span 3!</span>
+  <span>Span 4</span>
+  <span>Span 5!</span>
+  <span>Span 6</span>
+  <span>Span 7!</span>
+</div>
+
+<br>
+
+<h3><code>span:nth-child(2n+1)</code>, WITH an
+   <code>&lt;em&gt;</code> among the child elements.</h3>
+<p>Children 1, 5, and 7 are selected.<br>
+   3 is used in the counting because it is a child, but it isn't
+   selected because it isn't a <code>&lt;span&gt;</code>.</p>
+<div class="second">
+  <span>Span!</span>
+  <span>Span</span>
+  <em>This is an `em`.</em>
+  <span>Span</span>
+  <span>Span!</span>
+  <span>Span</span>
+  <span>Span!</span>
+  <span>Span</span>
+</div>
+
+<br>
+
+<h3><code>span:nth-of-type(2n+1)</code>, WITH an
+   <code>&lt;em&gt;</code> among the child elements.</h3>
+<p>Children 1, 4, 6, and 8 are selected.<br>
+   3 isn't used in the counting or selected because it is an <code>&lt;em&gt;</code>,
+   not a <code>&lt;span&gt;</code>, and <code>nth-of-type</code> only selects
+   children of that type. The <code>&lt;em&gt;</code> is completely skipped
+   over and ignored.</p>
+<div class="third">
+  <span>Span!</span>
+  <span>Span</span>
+  <em>This is an `em`.</em>
+  <span>Span!</span>
+  <span>Span</span>
+  <span>Span!</span>
+  <span>Span</span>
+  <span>Span!</span>
+</div>
+
+ +

CSS

+ +
html {
+  font-family: sans-serif;
+}
+
+span,
+div em {
+  padding: 5px;
+  border: 1px solid green;
+  display: inline-block;
+  margin-bottom: 3px;
+}
+
+.first span:nth-child(2n+1),
+.second span:nth-child(2n+1),
+.third span:nth-of-type(2n+1) {
+  background-color: lime;
+}
+ +

最终效果:

+ +

{{EmbedLiveSample('Detailed_example', 550, 550)}}

+ + + +

规范

+ + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('CSS4 Selectors', '#nth-child-pseudo', ':nth-child')}}{{Spec2('CSS4 Selectors')}}未变化。
{{SpecName('CSS3 Selectors', '#nth-child-pseudo', ':nth-child')}}{{Spec2('CSS3 Selectors')}}初始化定义。
+ +

兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatGeckoDesktop("1.9.1")}}9.09.53.1 
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.1{{CompatGeckoMobile("1.9.1")}}9.09.53.1
+
+ +

注意

+ + + +

参见

+ + -- cgit v1.2.3-54-g00ecf