Professional Documents
Culture Documents
Advanced Selectors
Selectors are the way you reference the parts of your HTML documents with
the styles you want to have applied to them. CSS2 and CSS3 brought with
them a host of new selector specifications, designed to allow greater access
to the elements and parts of those elements that make up each and every
webpage. These new syntax rules allow greater flexibility and accuracy in
defining exactly which parts of your page get styled.
Type selectors
Example(s):
The following rule matches all H1 elements in the document tree:
H1 { font-family: sans-serif }
Grouping Selectors
When several selectors share the same declarations, they may be grouped
into a comma-separated list.
Example(s):
In this example, we condense three rules with identical declarations into
one. Thus,
H1 { font-family: sans-serif }
H2 { font-family: sans-serif }
H3 { font-family: sans-serif }
is equivalent to:
H1, H2, H3 { font-family: sans-serif }
Descendant selectors
The advanced combinators allow you to define styles that are based on a
more complex rule that will be applied to more specific elements. Originally,
it was possible to get to descendant-elements through contextual styles.
For instance:
p em {font-size: larger; }
This rule will affect all em elements inside paragraphs. This is very useful,
but also relatively simplistic. Combinators allow you to state the required
relationship between two elements.
h1 + h2 {color: red; }
Child Selectors
The child selector (denoted by the greater-than symbol, >) can be used to
combine two elements, and will only be applied if the second element
comes directly after the first. It looks like this:
This will affect all <em> elements which occur as children of paragraphs,
but not those that have another parent element in-between (which would
make the em a grand-child or more). In the example below, the first em
element would be styled, but the second would not, as it is inside an a
element — and therefore not directly below the p element:
<p>Do <em>not</em> visit <a href="index.html"> <em> this </em>
page</a>!</p>
Universal Selector
The universal selector (denoted by a star, *), affects every element on a
page.
* {color: #bb0; }
* p {color: red; }
p {color: red; }
However, it does become useful when used in concert with the child
selector, as it can take the place of any element which may occur in
between two others, as in
p > * > em {font-size: larger; }
A rule like this would allow the second em element in our example above to
be styled, but not an em which was inside a further element, as in a case
like
It should be apparent that you can chain any of these rules together into a
single complex rule.
Attribute Selectors
CSS2 also allows you to apply styling to an element based on the attributes
it has defined in it, or even based on the values of these attributes. Firstly,
to select the elements with an attribute defined, you infix the attribute
name surrounded by square brackets:
[att] Match when the element sets the "att" attribute, whatever the value
of the attribute.
[att=val] Match when the element's "att" attribute value is exactly "val".
Example(s):
For example, the following attribute selector matches all H1 elements that
specify the "title" attribute, whatever its value:
Example(s):
In the following example, the selector matches all SPAN elements whose
"class" attribute has exactly the value "example":
Example(s):
Here, the selector matches all SPAN elements whose "hello" attribute has
exactly the value "Cleveland" and whose "goodbye" attribute has exactly
the value "Columbus":
SPAN[hello="Cleveland"][goodbye="Columbus"] { color: blue; }
Example(s):
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 second selector will only match when
the "href" attribute has the value "http://www.w3.org/".
A[rel~="copyright"]
A[href="http://www.w3.org/"]
Example(s):
The following rule hides all elements for which the value of the "lang"
attribute is "fr" (i.e., the language is French).
Example(s):
The following rule will match for values of the "lang" attribute that begin
with "en", including "en", "en-US", and "en-cockney":
first-child pseudo-class
Example(s):
In the following example, the selector matches any P element that is the
first child of a DIV element. The rule suppresses indentation for the first
paragraph of a DIV:
The :link pseudo-class applies for links that have not yet been visited.
The :visited pseudo-class applies once the link has been visited by the user.
Note. After a certain amount of time, user agents may choose to return a
visited link to the (unvisited) ':link' state.
The :focus pseudo-class applies while an element has the focus (accepts
keyboard events or other forms of text input).
User agents are not required to reflow a currently displayed document due
to pseudo-class transitions. For instance, a style sheet may specify that the
'font-size' of an :active link should be larger than that of an inactive link,
but since this may cause letters to change position when the reader selects
the link, a UA may ignore the corresponding style rule.
Example(s):
Note that the A:hover must be placed after the A:link and A:visited rules,
since otherwise the cascading rules will hide the 'color' property of the
A:hover rule. Similarly, because A:active is placed after A:hover, the active
color (lime) will apply when the user both activates and hovers over the A
element.
Example(s):
An example of combining dynamic pseudo-classes:
The last selector matches A elements that are in pseudo-class :focus and in
pseudo-class :hover.
Pseudo-elements
Example(s):
Example(s):