Selectors

Universal Selector
Pattern:

*

Description:

This selector matches any element name in the document’s language. If a rule does not have an explicit selector, then the universal selector is inferred.

Examples:
* {color: red;}
div * p {color: blue;}
Supported by:

All CSS-aware browsers.

Type Selector
Pattern:

element1

Description:

This selector matches the name of an element in the document’s language. Every instance of the element name is matched. (CSS1 referred to these as “element selectors.”)

Examples:
body {background: #FFF;}
p {font-size: 1em;}
Supported by:

All CSS-aware browsers.

Descendant Selector
Pattern:

element1 element2

Description:

This allows the author to select an element based on its status as a descendant of another element. The matched element can be a child, grandchild, great-grandchild, etc., of the ancestor element. (CSS1 referred to these as “contextual selectors.”)

Examples:
body h1 {font-size: 200%;}
table tr td div ul li {color: purple;}
Supported by:

All CSS-aware browsers.

Child Selector
Pattern:

element1 > element2

Description:

This type of selector is used to match an element based on its status as a child of another element. It is more restrictive than a descendant selector, as only a child will be matched.

Examples:
div > p {color: cyan;}
ul > li {font-weight: bold;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Adjacent Sibling Selector
Pattern:

element1 + element2

Description:

This allows the author to select an element that is the following adjacent sibling of another element. Any text between the two elements is ignored; only elements and their positions in the document tree are considered.

Examples:
table + p {margin-top: 2.5em;}
h1 + * {margin-top: 0;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Class Selector
Pattern:
element1.classname
element1.classname1.classname2
Description:

In languages that permit it, such as HTML, XHTML, SVG, and MathML, a class selector using “dot notation” can be used to select elements that have a class attribute containing a specific value or values. The name of the class value must immediately follow the dot. Multiple class values can be chained together, although there are support problems in Explorer previous to IE7. If no element name precedes the dot, then the selector matches all elements bearing that class value or values.

Examples:
p.urgent {color: red;}
a.external {font-style: italic;}
.example {background: olive;}
.note.caution {background: yellow;}
Supported by:

All CSS-aware browsers.

Note:

IE previous to IE7 does not support the chained selector syntax, though it does permit multiple words in class values in the markup.

ID Selector
Pattern:

element1#idname

Description:

In languages that permit it, such as HTML or XHTML, an ID selector using “hash notation” can be used to select elements that have an ID containing a specific value or values. The name of the ID value must immediately follow the octothorpe (#). If no element name precedes the octothorpe, then the selector matches all elements containing that ID value.

Examples:
h1#page-title {font-size: 250%;}
body#home {background: silver;}
#example {background: lime;}
Supported by:

All CSS-aware browsers.

Simple Attribute Selector
Pattern:

element1[attr]

Description:

This allows authors to select any element based on the presence of an attribute, regardless of the attribute’s value.

Examples:
a[rel] {border-bottom: 3px double gray;}
p[class] {border: 1px dotted silver;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Exact Attribute Value Selector
Pattern:

element1[attr="value"]

Description:

This allows authors to select any element based on the precise and complete value of an attribute.

Examples:
a[rel="Start"] {font-weight: bold;}
p[class="urgent"] {color: red;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Partial Attribute Value Selector
Pattern:

element1[attr˜="value"]

Description:

This allows authors to select any element based on a portion of the space-separated value of an attribute. Note that [class˜="value"] is equivalent to .value (see above).

Examples:
a[rel|="friend"] {text-transform: uppercase;}
p[class|="warning"] {background: yellow;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Beginning Substring Attribute Value Selector
Pattern:

element1[attr^="substring"]

Description:

This allows authors to select any element based on a substring at the very beginning of an attribute’s value.

Examples:
a[href^="/blog"] {text-transform: uppercase;}
p[class^="test-"] {background: yellow;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Ending Substring Attribute Value Selector
Pattern:

element1[attr$="substring"]

Description:

This allows authors to select any element based on a substring at the very end of an attribute’s value.

Example:
a[href$=".pdf"] {font-style: italic;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Arbitrary Substring Attribute Value Selector
Pattern:

element1[attr*="substring"]

Description:

This allows authors to select any element based on a substring found anywhere within an attribute’s value.

Examples:
a[href*="oreilly.com"] {font-weight: bold;}
div [class*="port"] {border: 1px solid red;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

Language Attribute Selector
Pattern:

element1[lang|="lc"]

Description:

This allows authors to select any element with a lang attribute whose value is a hyphen-separated list of values, starting with the value provided in the selector.

Example:
html[lang|="tr"] {color: red;}
Supported by:

Firefox, Internet Explorer 7+ only, Opera, Safari.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.162.87