Css Selectors
Css Selectors
In CSS, selectors are patterns used to select the element(s) you want to style. The "CSS" column indicates in which CSS version the
property is defined (CSS1, CSS2, or CSS3).
Selector
Example
Example description
.class
#id
*
element
element,element
element element
element>element
element+element
.intro
CSS
1
#firstname
div,p
div p
div>p
div+p
[attribute]
[attribute=value]
[attribute~=value]
[target]
Selects all <p> elements that are placed immediately after <div>
elements
Selects all elements with a target attribute
[target=_blank]
[title~=flower]
Selects all elements with a title attribute containing the word "flower"
[attribute|=value]
:link
:visited
:active
:hover
:focus
:first-letter
:first-line
:first-child
:before
:after
:lang(language)
[lang|=en]
Selects all elements with a lang attribute value starting with "en"
a:link
a:visited
a:active
a:hover
input:focus
p:first-letter
p:first-line
p:first-child
Selects every <p> element that is the first child of its parent
p:before
p:after
p:lang(it)
Selects every <p> element with a lang attribute equal to "it" (Italian)
element1~element2
[attribute^=value]
p~ul
a[src^="https"]
Selects every <a> element whose src attribute value begins with "https"
[attribute$=value]
a[src$=".pdf"]
Selects every <a> element whose src attribute value ends with ".pdf"
[attribute*=value]
a[src*="w3schools"]
:first-of-type
p:first-of-type
Selects every <a> element whose src attribute value contains the
substring "w3schools"
Selects every <p> element that is the first <p> element of its parent
:last-of-type
p:last-of-type
Selects every <p> element that is the last <p> element of its parent
:only-of-type
p:only-of-type
Selects every <p> element that is the only <p> element of its parent
:only-child
:nth-child(n)
:nth-last-child(n)
p:only-child
Selects every <p> element that is the only child of its parent
p:nth-child(2)
Selects every <p> element that is the second child of its parent
p:nth-last-child(2)
:nth-of-type(n)
p:nth-of-type(2)
Selects every <p> element that is the second child of its parent,
counting from the last child
Selects every <p> element that is the second <p> element of its parent
:nth-last-of-type(n)
p:nth-last-of-type(2)
:last-child
:root
:empty
p:last-child
Selects every <p> element that is the second <p> element of its parent,
counting from the last child
Selects every <p> element that is the last child of its parent
:root
p:empty
Selects every <p> element that has no children (including text nodes)
:target
#news:target
:enabled
:disabled
:checked
:not(selector)
::selection
input:enabled
input:disabled
input:checked
:not(p)
::selection