Selectors
Group of Selectors
elementname-1, elementname-2
h1, p
Basic selectors
Name | Pattern | Example | |
---|---|---|---|
Universal selector | * |
* |
|
Type selector | E |
p |
|
Class selector | E.classname .classname |
p.odd odd |
|
ID selector | E#idname #idname |
p#first-paragraph #first-paragraph |
|
Attribute selector | E[attr] |
p[lang] |
contain attribute |
Attribute selector | [attr] |
[lang] |
contain attribute |
Attribute selector | [attr=value] |
[lang="pt-BR"] |
value is exactly value |
Attribute selector | [attr~=value] |
[class~="odd"] |
value is a whitespace-separated list of words, one of which is exactly value |
Attribute selector | [attr|=value] |
[lang|="BR"] |
value can be exactly value or can begin with value immediately followed by a hyphen |
Attribute selector | [attr^=value] |
[lang^="pt"] |
value value is prefixed (preceded) by value |
Attribute selector | [attr$=value] |
[lang$="BR"] |
value is suffixed (followed) by value |
Attribute selector | [attr*=value] |
[class*="odd"] |
value contains at least one occurrence of value within the string |
Combinators
Name | Pattern | Example |
---|---|---|
Adjacent sibling combinator | A + B |
h1 + p |
General sibling combinator | A ~ B |
h1 ~ p |
Child combinator | A > B |
ul > li |
Descendant combinator | A B |
ul li |
Pseudo-classes
Name | Pattern | Example |
---|---|---|
active pseudo-class | :active |
a:active |
checked pseudo-class | :checked |
input:checked |
focus pseudo-class | :focus |
input:focus |
first-child pseudo-class | :first-child |
ul > li:first-child |
hover pseudo-class | :hover |
a:hover |
last-child pseudo-class | :last-child |
ul > li:last-child |
link pseudo-class | :link |
a:link |
nth-child pseudo-class | :nth-child |
ul > li:nth-child(2) |
nth-of-type pseudo-class | :nth-of-type |
body > p:nth-of-type(1) |
visited pseudo-class | :visited |
a:visited |
Pseudo-elements
Name | Pattern | Example |
---|---|---|
after pseudo-element | ::after |
p::after |
before pseudo-element | ::before |
p::before |
first-letter pseudo-element | ::first-letter |
p::first-letter |
first-line pseudo-element | ::first-line |
p::first-line |
selection pseudo-element | ::selection |
p::selection |
Fonte: Selectors Level 3 | W3C