In this tutorial, CSS developers will come to learn completely about CSS Selectors from basic level to advanced level. So, stay tuned to this page and collect every bit of knowledge on Selectors in CSS like CSS element selector, id selector, class selector, universal selector, grouping selector, etc. Just go with the links available below and grab the concept of CSS Selectors with ease.
- CSS Selectors
- All CSS Simple Selectors
- Basic Selectors
- Attribute Selectors
- Pseudo-classes Selectors
- Pseudo-elements Selectors
- Combinator Selectors
- The CSS Grouping Selector
Every CSS selector, taken from the latest CSS3 standard.
CSS Selectors
CSS selectors are utilized to select the content you need to style. In CSS Rule Set, Selectors are the part. CSS selectors select HTML elements as per their id, class, type, attribute, etc.
CSS selectors are divided into five categories:
- Simple/Basic selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-classes selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
On this page, you will learn about all these five categories of CSS Selectors in detail. So, let’s dive into this tutorial without any delay.
All CSS Simple Selectors
Selector | Example | Example description |
---|---|---|
#id | #firstname | Selects the element with id=”firstname” |
.class | .intro | Selects all elements with class=”intro” |
element.class | p.intro | Selects only <p> elements with class=”intro” |
* | * | Selects all elements |
element | p | Selects all <p> elements |
element,element,.. | div, p | Selects all <div> elements and all <p> elements |
Basic Selectors
Selector | Description | Example |
---|---|---|
element |
Type selector. Matches an element. | p { color: red } |
.class |
Class selector. Matches the value of anclass attribute. |
.warning { color: red } |
#id |
ID selector. Matches the value of an id attribute. |
#warning { color: red } |
* |
Universal selector. Matches everything. | * { color: red } |
Attribute selectors
Selector | Description | Example |
---|---|---|
[attribute] |
Matches elements containing a given attribute. | a[href] { color: red } |
[attribute="x"] |
Matches elements containing a given attribute with a given value. | a[href="/sitemap/"] { color: red } |
[attribute~="x"] |
Matches elements containing a given attribute with a value that contains a sub-value within a space-separated list. | abbr[title~="Style"] { color: red } |
[attribute|="x"] |
Matches elements containing a given attribute with a value that contains a sub-value within a hyphen-separated list. | html[lang|="en"] { color: red } |
[attribute^="x"] |
Matches elements containing a given attribute with a value that starts with something. | a[href^="http://"] { color: red } |
[attribute$="x"] |
Matches elements containing a given attribute with a value that ends with something. | a[href$=".com"] { color: red } |
[attribute*="x"] |
Matches elements containing a given attribute with a value that contains something. | a[href*="htmldog"] { color: red } |
Pseudo-classes Selectors
Selector | Description | Example |
---|---|---|
:link |
Matches a link that has not been visited. | a:link { color: blue } |
:visited |
Matches a link that has been visited. | a:visited { color: purple } |
:active |
Matches an element that is being activated, such as a link being clicked on. | a:active { color: red } |
:hover |
Matches an element whose box is being hovered over by a cursor. | a:hover { text-decoration: none } |
:focus |
Matches an element that has focus, such as one that has been tabbed to. | a:focus { border: 1px solid yellow } |
:target |
Matches an element that has been linked to (via<a href="#x"… ,for example). |
h2:target { color: red } |
:lang() |
Matches an element of a given language. | p:lang(fr) { color: red } |
:first-child |
Matches the first child of an element. | p:first-child { color: red } |
:last-child |
Matches the last child of an element. | div p:last-child { color: blue } |
:first-of-type |
Matches the first sibling of its type in an element. | li:first-of-type { color: red } |
:last-of-type |
Matches the last sibling of its type in an element. | li:last-of-type { color: blue } |
:nth-child() |
Matches an element that is the ordinal number child of its parent. | p:nth-child(3) { color: red } |
:nth-last-child() |
Matches an element that is the ordinal number child, in reverse order, of its parent. | p:nth-last-child(2) { color: blue } |
:nth-of-type() |
Matches an element that is the ordinal number sibling of its type. | li:nth-of-type(5) { color: red } |
:nth-last-of-type() |
Matches an element that is the ordinal number sibling, in reverse order, of its type. | li:nth-of-type(5) { color: red } |
:only-child |
Matches an element if it is the only child of its parent. | article p:only-child { color: red } |
:only-of-type |
Matches an element if it is the only sibling of its type. | article aside:only-of-type { color: blue } |
:empty |
Matches an element with no children, or content. | td:empty { border-color: red } |
:root |
Matches the root element of a document. This will be the html element in HTML. |
:root { background: yellow } |
:enabled |
Matches form control elements that are not disabled. | input:enabled { border-color: lime } |
:disabled |
Matches form control elements that are disabled. | input:enabled { border-color: red } |
:checked |
Matches a radio or checkbox type input element that is checked. | input:checked { outline: 3px solid yellow } |
:not() |
Negotiation pseudo-class. Matches an element that does not match a selector. | p:not(:first-child) { color: orange } |
Pseudo-elements Selectors
Selector | Description | Example |
---|---|---|
::first-line |
Matches the first textual line in an element. | p::first-line { font-weight: bold } |
::first-letter |
Matches the first letter in an element. | p::first-letter { font-size: 2em } |
::before |
Used with the content property to generate content before the initial content of an element. |
h1::before { content: "*" } |
::after |
Used with the content property to generate content after the initial content of an element. |
h1::after { content: "+" } |
Combinator Selectors
Selector | Description | Example |
---|---|---|
selector selector |
Descendant combinator. Matches elements that are descendants of another element. | aside p { color: red } |
selector > selector |
Child combinator. Matches elements that are children of another element. | .warning > p { color: red } |
selector + selector |
Adjacent sibling combinator. Matches elements that immediately follow another element. | h1 + * { color: red } |
selector ~ selector |
General sibling combinator. Matches elements that follow another element. | h2 ~ p { color: red } |
The CSS Grouping Selector
The grouping selector in CSS picks all the HTML elements with the same style definitions.
Let’s take a look at the below CSS code (with the same style definitions ie., h1, h2, and p elements):
h1 { text-align: center; color: red; } h2 { text-align: center; color: red; } p { text-align: center; color: red; }
To minimize the code, just apply the CSS grouping selectors. Simply group the selectors by separating each selector with a comma. Let’s see the following code after CSS Grouping Selectors:
h1, h2, p { text-align: center; color: red; }