CSS selectors

Back to the index.

CSS2 selectors | CSS3 selectors | CSS3 UI selectors | CSS4 selectors

Here are almost all CSS2 and 3 selectors, and the CSS3 UI selectors (mostly structural pseudo-classes).

This is the mobile table. See also the desktop table.

Last major update on 1 October 2013.

I'm writing a CSS book.

Basics

[an error occurred while processing this directive] [an error occurred while processing this directive]
p Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
p.test Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
p#test Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
p span Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
Selects all elements Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
Two or more class names Yes Yes Yes Yes Yes Yes Yes Yes Yes

Combinators

[an error occurred while processing this directive] [an error occurred while processing this directive]
Selects an element that is a child of another element Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element that is a next sibling of another element Yes Yes Yes Yes Yes Yes Yes Yes Yes
Static
The browser doesn’t correctly update the styles of the original element when another element is placed before it.
  • UC applies the style to all paragraphs.
Selects an element that is a general next sibling of another element Yes Yes Yes Yes Yes Yes Yes Yes Yes

Attribute selectors

[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
Selects an element with a certain attribute Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element with a certain attribute value Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element with an attribute that includes a certain value; space-separated Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element with a certain attribute that starts with a certain value Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element with a certain attribute that ends with a certain value Yes Yes Yes Yes Yes Yes Yes Yes Yes
Selects an element with a certain attribute that contains a certain value; not necessarily space-separated Yes Yes Yes Yes Yes Yes Yes Yes Yes
Attribute value starts with "value" or "value-" Yes Yes Yes Yes Yes Yes Yes Yes Yes

Pseudo-elements

[an error occurred while processing this directive] [an error occurred while processing this directive]
To generate content before and after an element Yes Yes Yes Yes Yes Yes Yes Yes Yes
 
The first letter of an element Almost Almost Almost Yes Almost Almost Yes Almost Almost Yes Yes
  • WebKit-based browsers leave the first 'T' at the start of the paragraph when you add text.
  • Opera Mobile leaves out the 'T' of the second and subsequent sentences when you do the dynamic test.
  • NetFront and Samsung Pocket do not apply the :first-line styles to the first letter.
The first line of an element Yes Yes Yes Yes Yes Yes Yes Yes Yes
Text selected by the user. No Samsung Yes No Yes Yes No Yes Untestable ut Yes ut Yes ut ut Yes ut -moz-
Untestable / UT
Browser does not support the selecting of text
  • Samsung Android WebKits support it; others don’t.

Pseudo-classes

[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
An element in an active state. No No No Yes No Yes Yes No No No Alternative No No Yes Yes

Styles should be applied ontouchstart and removed ontouchend.

To be honest, I don't see a lot of use for this selector on touchscreen devices.

  • Nintendo supports it, but only when you hold the element for a while. A simple tap is not enough.
An element in a hovered state. Special Yes Yes Special Yes Desktop Yes Yes Yes Yes Desktop Yes

Normally the :hover styles are applied when the user touches the element, and removed when the user touches another element.

Special
Browser implements styles (and their removal) only on touchable elements; i.e. links, form fields, or elements with an explicit onclick event handler.
Desktop
Browser removes styles when touch ends; this is more in line with what the desktop browsers do.
Unvisited link Yes Yes Yes Yes Yes Yes Yes Yes Yes
Negation of a selector Yes Yes Yes Yes Yes Yes Yes Yes Yes
Element that is a target of a hash (page.html#testHash) Yes Yes Yes Yes Yes Yes Yes Incomplete Yes
  • IE doesn’t react to the Back and Forward buttons: the element doesn’t apply or remove the pseudo-class at all. This used to be an Opera problem, while IE used to get it right.
  • Symbian Anna supports it, but the Back button doesn’t take you back to the un-hashed URL, but to the previous page.
IE10: see above
Visited link Yes se Yes Yes Incomplete se Yes Yes Yes se Yes Yes Yes No Yes

There’s a security issue with :visited. See the page for more details.

se
Security issue active
  • The Chrome 29 on Nexus 7 (Android 4.3) does not show the visited link in green, while it should. The Chrome 29 on HTC One (Android 4.2) does everything right.

Structural pseudo-classes

[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
Empty elements Yes Yes Yes Yes Yes Yes Yes Yes Yes
Static
Browser doesn’t update the styles when content is added to the element.
An element that is the first child of its parent Yes Yes Yes Yes Yes Yes Yes Yes Yes
Static
Browser doesn’t update the styles when elements are added dynamically.
The first element of its type. Yes Yes Yes Yes Yes Yes Yes Yes Yes
Static
Browser doesn’t update the styles when elements are added dynamically.
An element that is the last child of its parent Yes Yes Yes Yes Yes Yes Yes Yes Yes
  • Symbian 9.2 and S40 apply the :last-child styles to the :first-child.
The last element of its type. Yes Yes Yes Yes Yes Yes Yes Yes Yes
Static
Browser doesn’t update the styles when elements are added dynamically.
Select elements according to a formula Yes Yes Yes Yes Yes Yes Yes Yes Yes
Yes Yes Yes Yes Yes Yes Yes Yes Yes
Yes Yes Yes Yes Yes Yes Yes Yes Yes
Select elements according to a formula Yes Yes Yes Yes Yes Yes Yes Yes Yes
An element that is the only child of its parent Yes Yes Yes Yes Yes Yes Yes Yes Yes
  • Symbian 9.2 and S40 apply the :only-child styles to the :first-child and don’t remove the styles when the :only-child gets a sibling.
The only element of its type. Yes Yes Yes Yes Yes Yes Yes Yes Yes
  • S40 applies the :only-of-type styles to the :first-of-type.
The root element, or initial containing block. Yes Yes Yes Yes Yes Yes Yes Yes Yes

UI state pseudo-classes

See also the CSS3 User Interface spec.

[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
Checked elements (checkboxes or radios) Yes No Yes No Yes No No Yes No Yes No Yes No Yes No
Default choices in select boxes or groups of radio buttons No No No No Incomplete No No No No No No

Tested on radio buttons and select boxes.

  • Opera doesn’t support it on select boxes.
Disabled form fields Yes Yes Yes Yes Yes Yes Yes Yes Yes
Non-disabled form fields Yes Yes Yes Yes Yes Yes Yes Yes Yes
Form fields that have the keyboard focus Yes Yes Yes No Yes Yes No Yes No Yes Yes Yes

Sidenote: Sony Android 4 shows the label text in focused form fields. I think this is an excellent idea that deserves to be copied.

Form fields with values that are in range (numbers) Yes No Yes Yes No Yes No Yes No Yes Yes No Yes
  • The Sony Android 4, as well as UC, wait until the focus is removed from the form field to apply the styles. The others do it directly when a number is typed.
Form field with value that does not conform to pattern Yes No Yes Yes Buggy Yes Yes No Yes No Yes Yes No Yes Yes
  • The Sony Android 4 waits until the focus is removed from the form field to apply the styles. The others do it directly when a number is typed.
  • Opera Mini always follows the :valid styles even if the value is invalid.
Non-required form fields Yes Yes Yes Buggy Yes Yes Yes Yes No Yes Yes
  • Opera Mini gives all form fields the :optional styles.
Form fields with values that are not in range (numbers) Yes No Yes Yes No Yes No Yes No Yes Yes No Yes
  • The Sony Android 4, as well as UC, wait until the focus is removed from the form field to apply the styles. The others do it directly when a number is typed.
Readonly form fields Yes Yes Yes Yes Yes No Yes Yes No No
Non-readonly form fields Yes Yes Yes Yes Yes No Yes Yes No No
Required form fields Yes Yes Yes Buggy Yes Yes Yes Yes No Yes Yes
  • Opera Mini gives all form fields the :optional styles.
Form field with value that conforms to pattern Yes No Yes Yes Buggy Yes Yes No Yes No Yes Yes No Yes Yes
  • The Sony Android 4 waits until the focus is removed from the form field to apply the styles. The others do it directly when a number is typed.
  • Opera Mini gives all form fields the :valid styles.

Tested browsers

[an error occurred while processing this directive]