CSS SELECTORS /// TAGS /// CLASSES /// IDS /// SPECIFICITY /// CSS SELECTORS /// TAGS /// CLASSES /// IDS /// SPECIFICITY ///

CSS Tags & Selectors

The foundation of styling. Learn how to target HTML elements perfectly using tags, classes, IDs, and master the rules of Specificity.

styles.css
1 / 12
12345
🎣

Tutor:To style the web, you first need to catch the elements. CSS uses 'Selectors' (also known as tags or rules) to find HTML elements.


Target Matrix

UNLOCK NODES BY MASTERING SELECTORS.

Concept: HTML Tags

Directly style fundamental HTML elements (like `p`, `h1`, `div`). Warning: this affects ALL elements of that type globally.

System Check

What happens if you use `div { background: red; }` in your CSS?


Community Holo-Net

Debate Specificity

ACTIVE

Having trouble overriding a pesky style? Share your code and the community will help you find the right selector.

CSS Selectors: Precision targeting for DOM Elements

Frontend Instructor in Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

CSS is useless without elements to style. Selectors are the hooks you use to grab parts of your HTML structure to paint them with properties. Master them, and you control the layout.

1. The Basics: Tags, Classes, and IDs

The foundation of CSS targeting relies on three primary selectors:

  • Tag Selectors: Target the raw HTML element. Using p { color: red; } targets every single paragraph on the page.
  • Class Selectors (.): Reusable targets. You can add class="btn" to multiple different elements. In CSS, target it with a dot: .btn.
  • ID Selectors (#): Unique targets. An ID can only appear ONCE per page. In CSS, target it with a hash: #header.

2. Combinators: Hunting Relationships

Elements on a web page are structured like a family tree. You can target elements based on their ancestors or siblings using combinators:

Descendant (Space): div p selects any <p> that lives inside a <div>, regardless of how deep it is.

Child (>): ul > li strictly targets direct children. An li inside another nested list wouldn't be affected.

3. The Specificity Wars

When two CSS rules target the exact same element with conflicting properties (e.g., one says blue, another says red), who wins? The answer is Specificity.

[Inline Styles] > [ID Selectors] > [Classes, Attributes, Pseudo-classes] > [HTML Tags]

View Architecture Tip+

Avoid over-qualifying your selectors! Instead of writing div#main-content ul.list li.item a.link, just write .link. Keep your specificity low to make your CSS easier to override and maintain later (e.g., BEM methodology).

Frequently Asked Questions

When should I use an ID vs a Class?

Class (.): Use it for 99% of your styles. They're reusable and keep specificity manageable.

ID (#): Reserve them almost exclusively for JavaScript anchor links or unique absolute structural layouts. Their high specificity makes them a nightmare to override in modular CSS.

What does the universal selector (*) do?

The asterisk * selects literally everything on the page. It's commonly used in CSS resets to remove default margins and paddings.

* { box-sizing: border-box; margin: 0; padding: 0; }
What happens if I use !important?

It breaks the cascade rules and forces itself to apply, regardless of specificity. It's considered a "bad practice" unless you're overriding styles from an external library (like Bootstrap) that you can't modify otherwise.

Selectors Glossary

Type (Tag) Selector
Selects all elements of the given HTML node name.
snippet.css
Class Selector
Selects all elements with a given class attribute (starts with a dot).
snippet.css
ID Selector
Selects the single element with the specific id attribute (starts with a hash).
snippet.css
Universal Selector
Selects all elements on the document. Useful for resets.
snippet.css
Attribute Selector
Selects elements based on the presence or value of a given attribute.
snippet.css
Pseudo-class
Selects elements based on their state (e.g., hovering, focus) or position (e.g., first-child).
snippet.css