Css Selectors PDF
Css Selectors PDF
Css Selectors PDF
This appendix contains three reference tables that can help you learn how to apply
styles to the correct elements.
Table C-1 contains the CSS 2.1 selectors. Selectors help tell the browser where to apply
the CSS declarations.
In the Generic pattern column of Table C-1, the values C, R, and S take
the place of type selectors.
669
Selector Generic pattern Description Sample
Descendant C R S Matches any S div#content p em { background-
element that is a color: yellow; }
descendant of ele-
ment R, which is a
descendant of
element C
Child C > S Selects any S ele- li > ul { list-style-type:
ment that is a child circle;}
of a C element
Adjacent sibling C + S Selects any S ele- div#content+p { text-indent:
ment that immedi- 0;}
ately follows a C
element
Grouping C, R, S Several selectors h1, h2, h3, h4 { color: #0cf;}
utilize the same
declaration(s)
Class C.classR Selects any C ele- img.content { padding: 4px;
ment that contains border: 1px solid black; }
a class attribute
with a value of
classR
ID C#idR Selects any C ele- div#content { color: #333;}
ment that contains
an id attribute
with a value of idR
Attribute selector C[attribute] Selects any C ele- a[link] {text-decoration:
ment that contains none;}
the attribute
Attribute selector C[attribute="valueR"] Selects any C ele- input[type="text"] { width:
ment that contains 33%; }
the attribute with a
value of valueR
Attribute selector C[attribute~="valueR"] Selects any C ele- div.advertisement
ment that contains form[class~="login"] { float:
the attribute whose left; margin-left: 7px; }
value is a space-
separated list of
words and one of
the words in that list
matches valueR
Although CSS3 is still being worked on as this book is being written, some browser
vendors are starting to support properties from the unfinished specification. This ap-
pendix provides a listing of the new CSS3 selectors for your reference.
To get a solid idea of what tools you have at your disposal to apply styles to a document,
consult Appendix C (which covers CSS 2.1 selectors) in conjunction with this listing.
Table D-1 describes the new CSS3 selector, the general sibling selector.
In the Generic pattern column of Table D-1, the values C, R, and S take
the place of type selectors.
673
Table D-2 contains a list of new attribute selectors. These allow greater control when
selecting elements in a document based on an attributes value or even a small portion
of that value.
Table D-2. CSS3 attribute selectors
Selector Generic pattern Description Sample
Attribute C[attribute^="valueR"] Selects any C element that contains the img[alt^="mugshot"]
selector attribute that begins with the value of { width: 100px; }
valueR
Attribute C[attribute$="valueR"] Selects any C element that contains the img[alt$="photo"]
selector attribute whose value exactly matches { border: 10px
valueR solid red; }
Attribute C[attribute*="valueR"] Selects any C element that contains the img[alt*="execu
selector substring valueR tive"] { }
Attribute C[attribute|="valueR"] Selects any C element that contains an img[alt|="non"]
selector attribute value thats a list of hyphen- { opacity: .5;}
separated values
Table D-3 contains a list of structural pseudo-elements. These allow you to pick out
elements based on their order. For example, you can pinpoint the odd-numbered li
elements using the nth-child selector instead of using the class attribute selector.