CSS provides a variety of selectors to target elements with precision. Here’s a comprehensive overview:

1. Universal Selector
ghp_hATCP1QcjRuN7Sh1Yuvnvouoewutck2eZ2ZC
2. Type Selector
- Selects all elements of a specific type (e.g.,
p, div).
3. Class Selector
- Selects elements with a specific class.
4. ID Selector
- Selects an element with a specific ID (must be unique).
5. Attribute Selector
- Selects elements based on the presence, value, or part of a value of an attribute.
| Selector |
Description |
[attr] |
Selects elements with the attr attribute. |
[attr="value"] |
Selects elements where attr is exactly value. |
[attr~="value"] |
Selects elements where attr contains value as a word. |
| `[attr |
="value"]` |
[attr^="value"] |
Selects elements where attr begins with value. |
[attr$="value"] |
Selects elements where attr ends with value. |
[attr*="value"] |
Selects elements where attr contains value. |
6. Pseudo-Classes
- Dynamic pseudo-classes: Target elements in specific states.
| Selector |
Description |
:hover |
Selects elements when hovered over. |
:focus |
Selects elements when focused. |
:active |
Selects elements when active (clicked). |
:visited |
Selects links that have been visited. |
- Structural pseudo-classes: Target elements based on their structure in the DOM.
| Selector |
Description |
:first-child |
Selects the first child of a parent. |
:last-child |
Selects the last child of a parent. |
:nth-child(n) |
Selects the nth child of a parent. |
:nth-last-child(n) |
Selects the nth child from the end. |
:nth-of-type(n) |
Selects the nth child of a specific type. |
:not(selector) |
Selects all elements that do not match the selector. |
7. Pseudo-Elements
- Single-pseudo-elements: Create and style parts of an element.
| Selector |
Description |
::before |
Inserts content before an element’s content. |
::after |
Inserts content after an element’s content. |
::first-line |
Styles the first line of text. |
::first-letter |
Styles the first letter of text. |
::selection |
Styles the highlighted part of text when selected. |
8. Descendant Selector
- Selects elements that are nested within another element.
9. Child Selector
- Selects direct children of an element.
10. Adjacent Sibling Selector
- Selects an element that is immediately after another element.
11. General Sibling Selector
- Selects all siblings after a specified element.
12. Grouping Selector
- Selects multiple selectors, applying the same styles to all.
13. Combinators
| Selector |
Description |
E F |
Selects all F elements inside E. |
E > F |
Selects direct child F elements of E. |
E + F |
Selects F immediately following E. |
E ~ F |
Selects all F elements following E siblings. |
Summary Table of Common Selectors
| Selector |
Type |
Example |
Description |
* |
Universal |
* |
Selects all elements. |
E |
Type |
p |
Selects all <p> elements. |
.class |
Class |
.my-class |
Selects all elements with the class my-class. |
#id |
ID |
#my-id |
Selects the element with the ID my-id. |
[attr] |
Attribute |
[type="text"] |
Selects elements with a specific attribute. |
E, F |
Grouping |
h1, h2, h3 |
Selects multiple element types. |
E F |
Descendant |
div p |
Selects all <p> elements within <div>. |
E > F |
Child |
ul > li |
Selects direct child <li> elements in <ul>. |
E + F |
Adjacent Sibling |
h1 + p |
Selects the <p> element directly after <h1>. |
E ~ F |
General Sibling |
h1 ~ p |
Selects all <p> elements after <h1>. |
Using these selectors helps create efficient and targeted styles, which can make your CSS code more organized and maintainable.
Ff
Tuu