🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
CSS MASTER CLASS /// VISUAL ENGINEERING /// LAYOUT DESIGN /// ANIMATION LAB /// CSS MASTER CLASS /// VISUAL ENGINEERING ///

CSS Basic Selectors: Master the DOM Targeting System

Comprehensive tutorial on CSS Basic Selectors. Learn the syntax, performance, and specificity hierarchy of Tag, Class, and ID selectors for modern web design.

Total XP: 0|💻 css XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Core Targeting Engines

Foundational targeting methods. Command the browser's CSSOM selection engine to isolate and style specific DOM nodes using native tags, classes, and IDs.


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

A selector is the logical bridge between your data structure (HTML) and your visual design (CSS). Choosing the right selector is an architectural balance between technical precision, performance, and code reusability.

1Tags vs. Classes: Global vs. Modular

The Tag Selector (e.g., p, h1) directly targets the native HTML element. It is ideal for global typography and resets (* { margin: 0; }). However, its excessive use in specific components creates fragile code.

The Class Selector (.classname) is the core of modern Frontend architecture. It allows isolating styles in reusable components (like .btn or .card), applying the DRY (Don't Repeat Yourself) principle and enabling extreme scalability.

2The Power of ID and Specificity

An ID Selector (#idname) identifies a single node in the DOM. Due to this strict uniqueness required by HTML, browsers grant it massive Specificity. In the cascade calculation, a single #id overrides the influence of dozens of combined .classes.

Best Practices (Input-Output)

If you write <button id="submit" class="btn-red"> and in CSS you define .btn-red { background: red; } but #submit { background: blue; }, the button will irrevocably be blue. For this reason, senior engineers reserve IDs for JavaScript hooks and accessibility anchor points, using classes exclusively for CSS.

3Step-by-Step Breakdown

Core Targeting Engines. Welcome to the Targeting System. CSS Selectors are the bridge between your structural HTML data and your visual design. Today, you will master the foundational selectors—Tags, Classes, and IDs—and understand the critical Specificity algorithm that dictates how browsers resolve conflicting styles.

Native Element Selection: Tags. The Tag Selector targets raw HTML elements. It is the broadest net you can cast. Writing p { color: red; } will style every single paragraph in your entire document. It is primarily used for establishing global typography baselines and resets.

Which CSS targeting paradigm operates at the lowest specificity tier to universally style every instance of a specific native HTML element?

  • Tag Selector
  • Class Selector

Reusable Component Classes. The Class Selector is the backbone of modern CSS architecture. It starts with a period (.). Classes are reusable—you can apply the same class to hundreds of different elements to create standardized components like buttons or cards.

Which specific syntactic prefix must precede an identifier in the CSS stylesheet to successfully target a class attribute declared in the HTML?

  • Period (.)
  • Hash (#)

Multiple Classes. Elements are not limited to just one class. You can apply multiple classes to a single HTML element by separating them with a space. This allows you to mix and match modular styles rapidly.

When assigning multiple classes to a single HTML element, what character must you use to separate the class names within the class="..." attribute?

  • Comma (,)
  • Space ( )

Unique Node Identification: IDs. The ID Selector is highly specific and starts with a hash (#). According to strict HTML DOM standards, an ID must be absolutely unique—it can only be used on ONE element per page. It is reserved for major layout landmarks.

According to strict HTML and CSS validation specifications, which selector type MUST be absolutely unique per DOM instance (meaning it can only be used once per page)?

  • Class Selector
  • ID Selector

The Specificity Hierarchy. When two rules clash, the browser calculates 'Specificity' to decide the winner. The algorithm is strict: IDs defeat Classes. Classes defeat Tags. If an element has both a Class and an ID that set the color, the ID wins instantly, regardless of order.

Your HTML is <button id='submit' class='btn'>. You have .btn { background: blue; } at the *bottom* of your CSS file, and #submit { background: red; } at the *top*. What color is the button?

  • Blue (Because it is at the bottom)
  • Red (Because IDs have higher Specificity)

**The Universal Selector (*).** The asterisk (*) is the Universal Selector. It targets absolutely every element on the page simultaneously. It is primarily used at the top of stylesheets to execute global resets, stripping out inconsistent browser defaults.

Targeting Locked. You have conquered the fundamental targeting matrix! You know how to establish broad typography with Tags, architect modular systems with Classes, pinpoint landmarks with IDs, and understand the absolute power of Specificity. Next up: Advanced Combinators.

Target A Descendant Element. A descendant combinator (a space) selects any matching element nested anywhere inside another.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1IDs Used for Styling Often Duplicate ARIA Anchor Points

Reserving `id` attributes strictly for JavaScript hooks and accessibility anchors (like `aria-labelledby` or skip-link targets: `<a href="#main-content">`) keeps their purpose unambiguous, rather than overloading the same ID for both visual styling and assistive-technology navigation.

2A Universal Reset Must Not Silently Strip Focus Indicators

A `* { outline: none; }` reset (sometimes added alongside `margin: 0; padding: 0;`) removes the default focus ring from every interactive element site-wide, leaving keyboard users with no way to see where they are. Any universal reset should explicitly restore a visible `:focus-visible` style afterward.

SEO Implications

  • 1

    Class-Based Styling Keeps HTML Semantic, Which Crawlers Reward

    Relying on classes and tag selectors instead of jamming presentational attributes into markup keeps heading tags, lists, and semantic elements clean and meaningful, which search engines use as structural signals when parsing page content.

  • 2

    Bloated ID-Based Specificity Chains Increase Stylesheet Parse Time

    Stylesheets that lean heavily on ID selectors tend to accumulate long override chains and duplicated rules over time as developers fight specificity, inflating CSS file size and marginally slowing the browser's style-computation phase before first paint.

Best Practices

Reserve ID Selectors for JavaScript Hooks, Not Visual Styling

Since an ID's specificity is very difficult to override later without `!important`, use classes for all visual styling and keep `id` attributes for unique JS hooks, anchor links, and `aria-*` references instead.

Put the Universal Reset (*) at the Very Top of the Stylesheet

A `* { box-sizing: border-box; margin: 0; padding: 0; }` reset needs to load before any component-specific rule so nothing has to fight it for specificity — placing it first also makes the cascade easier to reason about.

Frequent Bugs

THE BUG

A class-based style (`.header { background: blue; }`) fails to apply even though the class is present on the element.

THE FIX

An ID selector elsewhere in the stylesheet (e.g. `#main-nav { background: white; }`) is winning due to higher specificity, regardless of source order. Move the background rule off the ID, or increase the class rule's specificity by combining it with the tag or another class.

THE BUG

Grouped selectors like `h1, h2, .title { color: red; }` unexpectedly break the entire rule when one selector in the list is invalid.

THE FIX

In standard CSS (not `:is()`), an invalid selector inside a comma-separated group invalidates the whole rule block in some older parsing contexts. Validate each selector in the group individually, or use the forgiving `:is()` selector list where supported.

Real-World Examples

Debugging a Specificity Conflict Between an ID and a Class

A component library's `.card { background: white; }` class rule wasn't applying to a specific card because a legacy `#promo-card { background: yellow; }` ID rule elsewhere in the codebase was silently winning, despite being declared earlier in the file.

/* Loses despite being declared later */
.card { background: white; }

/* Wins due to ID specificity, regardless of order */
#promo-card { background: yellow; }

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Misunderstanding Box Sizing

/* Wrong (Element might overflow container) */ .box { width: 100%; padding: 20px; } /* Correct */ .box { box-sizing: border-box; width: 100%; padding: 20px; }

The Solution //

By default, width and height only apply to the content box. Add 'box-sizing: border-box;' so padding and borders are included in the element's total width and height.

The Error //

Specificity Wars

/* Wrong */ #container .list-item.active { color: red !important; } /* Correct */ .list-item-active { color: red; }

The Solution //

Avoid using !important or overly complex selectors (like div#main span.active). Keep your selectors as flat and simple as possible to make them easier to override.

Lesson Glossary

[01]Tag Selector

Selects elements based on their HTML tag name.

Code Preview
div { ... }

[02]Class Selector

Selects elements with a specific class attribute. Reusable across many elements.

Code Preview
.btn-large

[03]ID Selector

Selects a single unique element with a specific id attribute. High specificity.

Code Preview
#unique-header

[04]Universal Selector

Selects all elements in the document.

Code Preview
*

[05]Specificity

The weight browsers apply to a rule, determining which style wins in a conflict.

Code Preview
ID > Class > Tag

Continue Learning