logo

Mastering CSS Selectors

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


100%;">

1. Universal Selector

  • *: Selects all elements.

* { margin: 0; padding: 0; }

2. Type Selector

  • Selects all elements of a specific type (e.g., p, div).

p { color: blue; }

3. Class Selector

  • Selects elements with a specific class.

.my-class { font-size: 18px; }

4. ID Selector

  • Selects an element with a specific ID (must be unique).

#my-id { background-color: yellow; }

5. Attribute Selector

  • Selects elements based on the presence, value, or part of a value of an attribute.
SelectorDescription
[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.

a[target="_blank"] { color: red; }

6. Pseudo-Classes

  • Dynamic pseudo-classes: Target elements in specific states.
SelectorDescription
:hoverSelects elements when hovered over.
:focusSelects elements when focused.
:activeSelects elements when active (clicked).
:visitedSelects links that have been visited.

button:hover { background-color: green; }
  • Structural pseudo-classes: Target elements based on their structure in the DOM.
SelectorDescription
:first-childSelects the first child of a parent.
:last-childSelects 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.

li:nth-child(2) { color: purple; }

7. Pseudo-Elements

  • Single-pseudo-elements: Create and style parts of an element.
SelectorDescription
::beforeInserts content before an element’s content.
::afterInserts content after an element’s content.
::first-lineStyles the first line of text.
::first-letterStyles the first letter of text.
::selectionStyles the highlighted part of text when selected.

p::before { content: "Note: "; font-weight: bold; }

8. Descendant Selector

  • Selects elements that are nested within another element.

div p { font-size: 14px; }

9. Child Selector

  • Selects direct children of an element.

div > p { color: orange; }

10. Adjacent Sibling Selector

  • Selects an element that is immediately after another element.

h1 + p { margin-top: 0; }

11. General Sibling Selector

  • Selects all siblings after a specified element.

h1 ~ p { color: blue; }

12. Grouping Selector

  • Selects multiple selectors, applying the same styles to all.

h1, h2, h3 { font-weight: bold; }

13. Combinators

SelectorDescription
E FSelects all F elements inside E.
E > FSelects direct child F elements of E.
E + FSelects F immediately following E.
E ~ FSelects all F elements following E siblings.

Summary Table of Common Selectors

SelectorTypeExampleDescription
*Universal*Selects all elements.
ETypepSelects all <p> elements.
.classClass.my-classSelects all elements with the class my-class.
#idID#my-idSelects the element with the ID my-id.
[attr]Attribute[type="text"]Selects elements with a specific attribute.
E, FGroupingh1, h2, h3Selects multiple element types.
E FDescendantdiv pSelects all <p> elements within <div>.
E > FChildul > liSelects direct child <li> elements in <ul>.
E + FAdjacent Siblingh1 + pSelects the <p> element directly after <h1>.
E ~ FGeneral Siblingh1 ~ pSelects all <p> elements after <h1>.


Using these selectors helps create efficient and targeted styles, which can make your CSS code more organized and maintainable.

Discussion (20)