aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/id_selectors/index.html
blob: 41ae08f7b7c81ab0da5b5175c45d466ef4247666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: ID 选择器
slug: Web/CSS/ID_selectors
tags:
  - CSS
  - Selectors
  - 参考
  - 选择器
translation_of: Web/CSS/ID_selectors
---
<div>{{CSSRef}}</div>

<p>在一个HTML文档中,CSS ID 选择器会根据该元素的 ID 属性中的内容匹配元素,元素 ID 属性名必须与选择器中的 ID 属性名完全匹配,此条样式声明才会生效。</p>

<pre class="brush: css no-line-numbers">/* The element with id="demo" */
#demo {
  border: red 2px solid;
}</pre>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">#id属性值 {<var>样式声明</var> }</pre>

<p>它与下面的{{Cssxref("Attribute selectors", "属性选择器")}}语句等价:</p>

<pre class="syntaxbox">[id=id属性值] {<var>样式声明</var> }</pre>

<h2 id="示例">示例</h2>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">span#identified {
  background-color: DodgerBlue;
}
</pre>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">  &lt;span id="identified"&gt;Here's a span with some text.&lt;/span&gt;
  &lt;span&gt;Here's another.&lt;/span&gt;
</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample("Example", '100%', 50)}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS4 Selectors", "#id-selectors", "ID selectors")}}</td>
   <td>{{Spec2("CSS4 Selectors")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("CSS3 Selectors", "#id-selectors", "ID selectors")}}</td>
   <td>{{Spec2("CSS3 Selectors")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("CSS2.1", "selector.html#id-selectors", "ID selectors")}}</td>
   <td>{{Spec2("CSS2.1")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("CSS1", "#id-as-selector", "ID selectors")}}</td>
   <td>{{Spec2("CSS1")}}</td>
   <td>初始定义</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("css.selectors.id")}}</p>