CSS3 Selectors
Written By: Avinash Malhotra
Updated on:
CSS3 Selectors
CSS3 Selectors includes all css selectors till CSS3. CSS3 Selectors list includes Rational Selectors, Combinator, Grouping and Pseudo Selectors.
Earlier CSS versions use Type, class, id and child selectors. CSS 2 adds more pseudo-elements, pseudo-classes, and combinator selectors. And now With CSS3, we can target almost any element with wide range of selectors options. Evan we have updated CSS Selectors level 4 in this article.
CSS3 Selectors are part of HTML5.
Here we will discuss all CSS, CSS2, CSS2.1, CSS3 and css selectors level 4 in detail.
CSS Selectors List
There are no selectors to target parent and previous element sibling in css.
Rational Selectors or Combinator
Rational Selectors or Combinator targets elements based on their relationship with another elements. These selectors were first introduced in CSS1 and then again in css2 and css3.
Selector | CSS Code | Use | CSS Level |
---|---|---|---|
Descendant Selector |
div p{ } | Target all p tags inside div. That includes all p elements that are descendant (child, grandchild, great-grandchild, and so on) of div. | 1 |
Child Selector |
div>p{ } | This selector matches only direct child of element | 2 |
Adjacent Sibling |
h4+p{ } | This selector matches p tag next to h4 and both sharing same parent. This means p should come directly after h4. | 2 |
General Sibling |
h3~p{ } | This selector matches all P elements exactly after h3 element. | 3 |
General Sibling i.e. ( A~B) selector will work only in HTML5 Supported Browsers. IE 8 and lesser doesn't support this selector.
Attribute Selectors
Attribute Selectors were introduced in css level 2 or css2. Attribute Selectors targets matching elements based on the attribute and attributes value. Attribute Selectors are used using Brackets []
. Attribute Selectors can target both attributes with value or boolean attributes.
CSS Selector | Use | CSS Level |
---|---|---|
E[attr]{} |
Target only E Element or Elements having exact case-insensitive attribute value. For exp input with disabled attribute, input[ disabled]{ cursor:not-allowed } |
2 |
E[attr="value"]{} |
This selector matches only Element with exact attribute and value. For exp, input type checkbox only, input[ type="checkbox" ] |
2 |
E[attr~="foo"]{} |
This selector matches only Elements with exact Attribute with multiple values separated by white-space, and one of the value is exactly equal to "foo". | 2 |
E[attr|="foo"]{} |
This selector matches only Elements with exact Attribute with value separated by hyphen (-) , and one of the value is exactly equal to "foo". | 2 |
E[attr^="foo"]{} |
This selector matches only element E with attribute value starting exactly with foo. For exp, div having class name starting with "col", div[ class^="col" ] |
3 |
E[attr$="value"]{} |
This selector matches only element E with attribute value ending exactly with foo. For exp, div having class name ending with "wrap", div[ class$="wrap" ] |
3 |
E[attr*="value"]{} |
This selector matches only element E with attribute value contains the substring "foo". For exp, div having class name with substring "md", div[ class*="md" ] |
3 |
E[attr="value" i]{} |
This selector matches only element E with attribute value is "value" or "VALUE". i means incase-sensitive. For exp, div having class name "col" or "COL", div[ class="col" i] |
4 |
E[attr="value" s]{} |
This selector matches only element E with attribute value equals "value" only. For exp, div having class name "col", div[ class="col" s] |
4 |
E[attr^=foo"]{}, E[attr$=foo"]{} and E[attr*=foo"]{} attribute selectors will only in HTML-5 Supported Browsers. IE 8 and lesser doesn't support these selectors.
Pseudo Selectors
CSS Pseudo Selectors are classified as Pseudo Class and Pseudo elements. These selectors were first introduced in css1, and then again in css2 and css3. CSS Pseudo Selectors are called using Colon :
. In CSS3, we use double colons ::
for pseudo elements, but single colon is also supported.
CSS Code | Use | CSS Level |
---|---|---|
E:link{} |
E (Anchor) element with a hyperlink, and which is not visited yet. | 1 |
E:visited{} |
E (Anchor) element with a hyperlink, and which is already visited as per browsers history. | 1 |
E:hover{} |
Change style on mouseover state of E element. Exp | 1 and 2 |
E:focus{} |
Change style on focus state of E element. Generally All Form Controls supports focus state. | 1 and 2 |
E:active{} |
Change style on mouse key down state of E element. <button>, <input type="submit">, <button type="reset"> supports this. Exp | 1 and 2 |
:enable{} |
A user interface element which is enable. exp | 3 |
:disable{} |
A user interface element that is disabled. exp
|
3 |
:read-only{} |
A user interface element that is disabled. exp
|
3 |
E:checked{} |
For checkbox and radio buttons that are checked or unchecked.
|
3 |
:valid{} |
Applicable to elements that are valid, based on their type and pattern. For exp
|
3 |
:invalid{} |
Applicable to elements that are invalid, based on their type and pattern. For exp
|
3 |
:required{} |
Applicable to input elements havingrequired attribute. For exp
|
3 |
:optional{} |
Applicable to input elements that do not have required attribute. For exp
|
3 |
:in-range{} |
Applicable to range and input type number element when the value is in-range . For exp
|
3 |
:out-of-range{} |
Applicable to range and input type number element when the value is out-of-range . For exp
|
3 |
:focus-within |
:focus-within pseudo selector select the element when the child element is being focused.
Focus on inputs below
|
4 |
:target |
:target pseudo selector
|
3 |
:marker |
change color of bullets or numbers of lists
|
4 |
:is() |
:is() function selector Heading 1 Heading 2 Heading 3
|
4 |
:where() |
:where() function selector Heading 1 Heading 2 Heading 3
|
4 |
:has() | :has() check descendant selector
Para with span Para without span Para with span Para without span Para with span
|
4 |
Browsers Compatibility
:hover is not supported in IE6 and below. IE7 and below doesn't supports :focus. However all Level 1 and Level 2 selectors are supported in IE8 except(:checked, :enabled, :disabled, & :target). Good News is that IE9 and above supports all CSS Level 1, 2, 3 and 4 selectors.
Structural Pseudo-classes
Structural Pseudo Selectors can target any element based on their position in nodes. First time they were introduced in css1, and then again in css2 and css3.
CSS Code | Use | CSS Level |
---|---|---|
E:root |
an E Element, root of the document. | 2 |
:first-child |
Targets only first child Element of parent. For Exp
|
2 |
:first-line |
Targets only first line of Element. For Exp,
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque ducimus delectus recusandae nisi dolore tenetur doloremque voluptas, ad inventore earum quos harum illo mollitia magnam velit cum asperiores. Earum, voluptatem.
|
1 |
:first-letter |
targets only first letter of Element, For exp,
Lorem ipsum dolor sit amet.
|
1 |
:last-child |
Targets only last child Element of parent. For Exp
|
3 |
:nth-child(n) |
Targets only nth child of parent element. n is a numeric value. For Exp
|
3 |
:nth-child(odd) |
Targets only odd children of parent element. n is a numeric value. For Exp
|
3 |
:nth-child(even) |
Targets only even children of parent element, ie 2,4,6, etc.
For Exp
|
3 |
:nth-of-type(n) |
Targets only nth sibling of type n, starting from first.
For Exp
|
3 |
:nth-last-of-type(n) |
Targets only nth sibling of element, starting from last.
For Exp
|
3 |
:not() |
targets all element except the one in not function.
|
3 |
:empty |
An element having no child element.
For Exp
This is a para with content. Another para with content.
|
3 |
:lang() |
An element having lang attribute with given value.
For Exp
Hello friend, how do you do? Hola amigo, ¿cómo se hace?
|
3 |
:dir(ltr) |
An element having dir attribute with given specific value given inside.
For Exp
Hello friend, how do you do? ہیلو دوست، آپ کیسے ہیں؟
|
4 |
:first-child, :first-letter, :first-line will be supported in IE 7 and 8. But Level 3 selectors will supported only in IE 9 and above browsers.
Pseudo Elements
Pseudo Elements are used using double colons, i.e. :: selector. Like :before or ::before, both are same in CSS3. For example p:first-line or p::first-line.
Selector | Use | CSS Level |
---|---|---|
::placeholder |
To change color of placeholder
|
3 |
::selection |
To style selected text.
Select this text to check default selection Select this text to check ::selection
|
3 |