Complete CSS Selector Cheat Sheet: A Hands-On Guide with images😍
Tags: css, webdev, beginners, tutorial
Published: 2023-02-11 12:27:30

CSS selectors can seem challenging because they provide a wide range of options for selecting HTML elements to style, and it's essential to understand the various options available and how they work. In addition, selectors allow you to target specific elements, classes, ids, attributes, and even pseudo-elements, based on their structure and relationships with other elements in your HTML. 

---

> 📒 Therefore, I've come up with the complete guide/crash course/cheat sheet, whatever you call it, of CSS selectors to make your life easier.

---

## Basic Selectors

| Name  | CSS  | Description  | Result
|---|---|---|---|
| `Universal Selector`  | `*` | <u>`Select all elements`</u>   | ![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676107583/Sk%C3%A4rmbild_20230211_102500_lvrg7c.png) |
|  `Type` | `div`  |  <u>`Select elements of that type`</u>` | ![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676108482/Sk%C3%A4rmbild_20230211_104056_fiyokh.png)  |
|  `Class` | .c  | <u>`Select elements with that class`</u> | ![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676108613/Sk%C3%A4rmbild_20230211_104319_mpqvtn.png)  |
|`Id`|`#i`|<u>`Select elements with that id`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676109046/Sk%C3%A4rmbild_20230211_104629_usv2qa.png)|

## Combination of selectors
| Name | CSS &nbsp; &nbsp; &nbsp; &nbsp; | Description  | Result
|---|----|---|---|
|`Descendant`|`div a`|<u>`Select elements that are descendants of the first element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676109195/Sk%C3%A4rmbild_20230211_105302_ij5wrd.png)|
|`Direct Child`|`div > a`|<u>`Select elements that are direct children of the first element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676110612/Sk%C3%A4rmbild_20230211_111634_fw2n0c.png)|
|`General Sibling`|`div ~ a`|<u>`Select elements that are siblings of the first element and come after the first element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676110649/Sk%C3%A4rmbild_20230211_111720_jynkia.png)|
|`Adjacent Sibling`|`div + a`|<u>`Select elements that are siblings of the first element and come directly after the first element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676110693/Sk%C3%A4rmbild_20230211_111800_nhz2d5.png)|
|`Or`|`div, a`|<u>`Select elements that match any selector in the list`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676110734/Sk%C3%A4rmbild_20230211_111842_wqxeki.png)|
|`And`|`div.c`|<u>`Select elements that match all the selector combinations`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676110765/Sk%C3%A4rmbild_20230211_111915_s4r9bq.png)|

## Attribute
| Name | CSS &nbsp; &nbsp; | Description  | Result
|---|----|---|---|
|`Has`|`[a]`|<u>`Select elements that have that attribute`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676111260/Sk%C3%A4rmbild_20230211_112715_rxddgp.png)|
|`Exact`|`[a=”1”]`|<u>`Select elements that have that attribute with exactly that value`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676111461/Sk%C3%A4rmbild_20230211_113048_a0qlus.png)|
|`Begins With`|`[a^=”1”]`|<u>`Select elements that have that attribute which start with that value`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676111488/Sk%C3%A4rmbild_20230211_113116_gghxq4.png)|
|`Ends With`|`[a$=”1”]`|<u>`Select elements that have that attribute which end with that value`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676111522/Sk%C3%A4rmbild_20230211_113149_tynfb4.png)|
|`Substring`|`[a*=”1”]`|<u>`Select elements that have that attribute which contain that value anywhere`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676111590/Sk%C3%A4rmbild_20230211_113300_pszoyg.png)|

## Pseudo Element Selctor
| Name | CSS &nbsp; &nbsp; &nbsp; | Description  | Result
|---|-----|---|---|
|`Before`|`div::before`|<u>`Creates an empty element directly before the children of selected element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676112905/Sk%C3%A4rmbild_20230211_115453_kacl8c.png)|
|`After`|`div::after`|<u>`Creates an empty element directly after the children of selected element`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676112934/Sk%C3%A4rmbild_20230211_115522_odplla.png)|

## Pseudo Class State
| Name | CSS &nbsp; &nbsp; &nbsp; | Description
|---|-----|---|
|`Hover`|`button:hover`|<u>`Select elements that are hovered by the mouse`</u>|
|`Focus`|`button:focus`|<u>`Select elements that are focused.`</u>|
|`Required`|`input:required `|<u>`Select inputs that are required`</u>|
|`Checked`|`input:checked`|<u>`Select checkboxes/radio buttons that are checked`</u>|
|`Disabled`|`input:disabled`|<u>`Select inputs that are disabled`</u>|

## Pseudo Class Position/Other
| Name | CSS | Description  | Result
|---|----|---|---|
|`First Child`|`a:first-child`|<u>`Select elements that are the first child inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114695/Sk%C3%A4rmbild_20230211_122444_wtqxm9.png)|
|`Last Child`|`a:last-child`|<u>`Select elements that are the last child inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114730/Sk%C3%A4rmbild_20230211_122519_jigxj7.png)|
|`Nth Child`|`a:nth-child(2n)`|<u>`Select elements that are the nth child inside a container based on the formula`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114754/Sk%C3%A4rmbild_20230211_122546_f83ida.png)|
|`Nth Last Child `|`a:nth-last-child(3)`|<u>`Select elements that are the nth child inside a container based on the formula counting from the end`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114825/Sk%C3%A4rmbild_20230211_122655_gs87s2.png)|
|`Only Child`|`a:only-child`|<u>`Select elements that are the only child inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114860/Sk%C3%A4rmbild_20230211_122729_pide27.png)|
|`First Of Type`|`a:first-of-type`|<u>`Select elements that are the first of a type inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114905/Sk%C3%A4rmbild_20230211_122806_hfdvhu.png)|
|`Last Of Type`|`a:last-of-type`|<u>`Select elements that are the last of a type inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114952/Sk%C3%A4rmbild_20230211_122900_yockwn.png)|
|`Nth Of Type `|`a:nth-of-type(2n)`|<u>`Select elements that are the nth of a type inside a container based on the formula`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114905/Sk%C3%A4rmbild_20230211_122806_hfdvhu.png)|
|`Nth Last Of Type`|`a:nth-last-of-type(2)`|<u>`Select elements that are the nth of a type inside a container based on the formula counting from the end`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676114952/Sk%C3%A4rmbild_20230211_122900_yockwn.png)|
|`Only Of Type`|`a:only-of-type`|<u>`Select elements that are the only of a type inside a container`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676115086/Sk%C3%A4rmbild_20230211_123112_axqpjy.png)|
|`Not`|`a:not(.c)`|<u>`Select all elements that do not match the selector inside the not selector`</u>|![result](https://res.cloudinary.com/dafxmvvzp/image/upload/v1676115112/Sk%C3%A4rmbild_20230211_123143_cnjlet.png)|

---

## Conclusion
I hope I explained all of these CSS selectors to you quickly. If there are any mistakes in the article or I've missed something, please mention them in the comment section.

Thanks for reading the article. I will see you all in my next article👨‍💻