📄 selector.html
字号:
<P>For information on selecting the first child of an element, pleasesee the section on the <a href="#first-child">:first-child</a>pseudo-class below.<h2>5.7 <a name="adjacent-selectors">Adjacent sibling selectors</a></h2><p>Adjacent sibling selectors have the following syntax: E1 + E2,where E2 is the subject of the selector. The selector matches if E1and E2 share the same parent in the document tree and E1 immediatelyprecedes E2.<p>In some contexts, adjacent elements generate formatting objectswhose presentation is handled automatically (e.g., collapsingvertical margins between adjacent boxes). The "+" selectorallows authors to specify additional style to adjacent elements.<div class="example"><P style="display:none">Example(s):</P><p>Thus, the following rule states that when a P element immediatelyfollows a MATH element, it should not be indented:</p><pre>MATH + P { text-indent: 0 } </pre><p>The next example reduces the vertical space separatingan H1 and an H2 that immediately follows it:</p><pre>H1 + H2 { margin-top: -5mm } </pre></div><div class="example"><P style="display:none">Example(s):</P><p> The following rule is similar to the one in theprevious example, except that it adds an attributeselector. Thus, special formatting only occurs whenH1 has <samp>class="opener"</samp>:</p><pre>H1.opener + H2 { margin-top: -5mm } </pre></div><h2>5.8 <a name="attribute-selectors">Attribute selectors</a></h2><p>CSS2 allows authors to specify rules that match attributes definedin the source document.<h3><a name="q10">5.8.1 Matching attributes and attribute values</a></h3><P>Attribute selectors may match in four ways:</p><dl><dt><code>[att]</code><dd>Match when the element sets the "att" attribute, whateverthe value of the attribute.<dt><a name="x14"><span class="index-def" title="exactmatching|="><code>[att=val]</code></span></a><dd>Match when the element's "att" attribute value is exactly "val".<dt><a name="x16"><span class="index-def" title="space-separatedmatching|~="><code>[att~=val]</code></span></a><dd>Match when the element's "att" attribute value is a space-separated list of "words", one of which is exactly "val". If this selector is used, the words in the value must not containspaces (since they are separated by spaces).<dt><a name="x18"><span class="index-def" title="hyphen-separatedmatching||="><code>[att|=val]</code></span></a><dd>Match when the element's "att" attribute value is a hyphen-separated list of "words", beginning with "val". The matchalways starts at the beginning of the attribute value.This is primarilyintended to allow <a name="x20"><span class="index-inst" title="languagecode">language subcode</span></a> matches (e.g., the "lang"attribute in HTML) as described in RFC 1766 (<a href="refs.html#ref-RFC1766" rel="biblioentry" class="noxref"><span class="informref">[RFC1766]</span></a>).</dl><p>Attribute values must be identifiers or strings. Thecase-sensitivity of attribute names and values in selectors depends onthe document language.<div class="example"><P style="display:none">Example(s):</P><p>For example, the following attribute selector matches all H1 elements that specify the "title" attribute, whatever its value:</p><pre>H1[title] { color: blue; }</pre></div><div class="example"><P style="display:none">Example(s):</P><p>In the following example, the selector matches all SPAN elements whose"class" attribute has exactly the value "example":</p><pre>SPAN[class=example] { color: blue; }</pre></div><P>Multiple attribute selectors can be used to refer to severalattributes of an element, or even several times the same attribute.<div class="example"><P style="display:none">Example(s):</P><p>Here, the selector matches all SPAN elements whose"hello" attribute has exactly the value "Cleveland" and whose"goodbye" attribute has exactly the value "Columbus":</P><pre>SPAN[hello="Cleveland"][goodbye="Columbus"] { color: blue; }</pre></div><div class="example"><P style="display:none">Example(s):</P><p>The following selectors illustrate the differences between "=" and "~=".The first selector will match, for example, the value "copyright copyleft copyeditor" for the "rel" attribute. The secondselector will only match when the "href" attribute has the value "http://www.w3.org/".</p><pre>A[rel~="copyright"]A[href="http://www.w3.org/"]</pre></div><div class="example"><P style="display:none">Example(s):</P><P>The following rule hides all elements for which the value of the"lang" attribute is "fr" (i.e., the language is French).<PRE>*[LANG=fr] { display : none }</PRE></div><div class="example"><P style="display:none">Example(s):</P><P>The following rule will match for values of the "lang" attributethat begin with "en", including "en", "en-US", and "en-cockney":</p><PRE>*[LANG|="en"] { color : red }</PRE></div><div class="example"><P style="display:none">Example(s):</P><P>Similarly, the following aural style sheet rules allow a scriptto be read aloud in different voices for each role:</p><pre class="example">DIALOGUE[character=romeo] { voice-family: "Lawrence Olivier", charles, male } DIALOGUE[character=juliet] { voice-family: "Vivien Leigh", victoria, female }</pre></div><H3><a name="q11">5.8.2 Default attribute values in DTDs</a></H3><P>Matching takes place on attribute values in the document tree. Fordocument languages other than HTML, default attribute values may bedefined in a <a name="x21"><span class="index-inst" title="DTD">DTD</span></a> or elsewhere. Stylesheets should be designed so that they work even if the default valuesare not included in the document tree.<div class="example"><P style="display:none">Example(s):</P><P>For example, consider an element EXAMPLE with an attribute "notation"that has a default value of "decimal". The DTD fragment might be<pre class="dtd-example"><!ATTLIST EXAMPLE notation (decimal,octal) "decimal"></pre><p>If the style sheet contains the rules<pre class="example">EXAMPLE[notation=decimal] { /*... default property settings ...*/ }EXAMPLE[notation=octal] { /*... other settings...*/ }</pre><p>then to catch the cases where this attribute is set by default,and not explicitly, the following rule might be added:<pre class="example">EXAMPLE { /*... default property settings ...*/ }</pre><p>Because this selector is less <a href="cascade.html#specificity">specific</a> than an attribute selector, it will only be used for thedefault case. Care has to be taken that all other attribute valuesthat don't get the same style as the default are explicitly covered.</div><h3>5.8.3 <a name="class-html">Class selectors</a></h3> <p>For style sheets used with HTML, authors may use the dot (.)notation as an alternative to the "~=" notation when matching on the"class" attribute. Thus, for HTML,"DIV.value" and "DIV[class~=value]" have the same meaning. Theattribute value must immediately follow the ".".<div class="example"><P style="display:none">Example(s):</P><p>For example, we can assign style information to all elements with<samp>class~="pastoral"</samp> as follows:</p><pre>*.pastoral { color: green } /* all elements with class~=pastoral */</pre>or just<pre>.pastoral { color: green } /* all elements with class~=pastoral */</pre><p>The following assigns style only to H1 elements with<samp>class~="pastoral"</samp>:</p><pre>H1.pastoral { color: green } /* H1 elements with class~=pastoral */</pre><p>Given these rules, the first H1 instance below would not have greentext, while the second would:</p><pre><H1>Not green</H1><H1 class="pastoral">Very green</H1></pre></div><p>To match a subset of "class" values, each value must be precededby a ".", in any order.</P><div class="example"><P style="display:none">Example(s):</P><P>For example, the following rule matches any P element whose "class" attributehas been assigned a list of space-separated values that includes "pastoral"and "marine":</p><pre> P.pastoral.marine { color: green }</pre><p>This rule matches when <samp>class="pastoral blue aquamarine"</samp> but does not match for <samp>class="pastoralblue"</samp>.</div><div class="note"><p> <em><strong>Note.</strong> CSS gives so muchpower to the "class" attribute, that authors could conceivably designtheir own "document language" based on elements with almost noassociated presentation (such as DIV and SPAN in HTML) and assigningstyle information through the "class" attribute. Authors should avoidthis practice since the structural elements of a document languageoften have recognized and accepted meanings and author-defined classes maynot.</em></div><h2>5.9 <a name="id-selectors">ID selectors</a></h2><P>Document languages may contain attributes that are declared to beof type ID. What makes attributes of type ID special is that no twosuch attributes can have the same value; whatever the documentlanguage, an ID attribute can be used to uniquely identify itselement. In HTML all ID attributes are named "id"; XMLapplications may name ID attributes differently, but thesame restriction applies.<p>The ID attribute of a document language allows authors to assign anidentifier to one element instance in the document tree. CSS IDselectors match an element instance based on its identifier. A CSSID selector contains a "#" immediately followed by the IDvalue.</p><div class="example"><P style="display:none">Example(s):</P><p> The following ID selector matches the H1 element whose IDattribute has the value "chapter1":</p><PRE>H1#chapter1 { text-align: center }</PRE></div><div class="html-example"><p> In the following example, the style rule matchesthe element that has the ID value "z98y".The rule will thus match for the P element:</p><pre><HEAD> <TITLE>Match P</TITLE> <STYLE type="text/css"> *#z98y { letter-spacing: 0.3em } </STYLE></HEAD><BODY> <P id=z98y>Wide text</P></BODY></pre><p>In the next example, however, the style rule will only match an H1element that has an ID value of "z98y". The rule will not match theP element in this example:</p><pre><HEAD> <TITLE>Match H1 only</TITLE> <STYLE type="text/css"> H1#z98y { letter-spacing: 0.5em } </STYLE></HEAD><BODY> <P id=z98y>Wide text</P></BODY></pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -