PROJECTENGRAM

CSS SELECTORS

Tags: #◇Computer Science #◇CCAPDEV #◇Conceptual #◇CSS Selectors


◇Overview

CSS selectors are the fundamental mechanism through which developers target specific HTML elements for styling. Without selectors, CSS rules would have no way of knowing which parts of the document to apply visual changes to. Understanding the various types of selectors—ranging from basic element selectors to complex combinators and pseudo-classes—is crucial for writing efficient, reusable, and maintainable stylesheets.

◇Key Definitions

  • Element Selector: Targets standard HTML tags directly (e.g., p, h1).
  • ID Selector (#): Targets a unique element on the page based on its id attribute. It can only be used once per HTML document.
  • Class Selector (.): Targets elements based on their class attribute. Classes can be reused across multiple elements.
  • Combinators: Selectors that explain the relationship between multiple elements (e.g., descendent, child).
  • Attribute Selectors: Target elements based on the presence or value of a specific HTML attribute.
  • Pseudo-classes & Pseudo-elements: Target the specific states of an element (like hover) or specific parts of an element (like the first letter).

◇Theoretical Framework / Core Concepts

ID vs. Class Selectors

The most common way to target specific non-generic elements is through IDs and Classes.

  1. IDs (#): Because IDs must be totally unique within an HTML document, they are highly specific. They are best used for major structural elements or elements targeted by JavaScript.
    Syntax: #header { ... }
  2. Classes (.): Classes are designed for reusability. You can apply the same class to multiple paragraphs, buttons, or divs to maintain visual consistency.
    Syntax: .btn-primary { ... }

Advanced Selection

  • Combinators: Allow you to target elements based on their position in the DOM relative to other elements. For example, targeting only paragraphs that are directly inside a specific div.
  • Attribute Selectors: Highly useful for styling forms. For instance, targeting only <input type="text"> differently than <input type="checkbox">.
  • Pseudo-classes / Pseudo-elements: These allow styling based on user interaction (e.g., :hover, :active) without needing JavaScript to manually toggle classes.

◇Worked Examples / Protocol Walkthroughs / Calculations

Example 1: Element Selector

Targeting generic HTML elements across the entire page.

『』
CSS
/* Targets every <h1> tag */
h1 {
    color: red;
}
/* Targets the document body */
body {
    background-color: silver;
}

Example 2: Class Selector

Using a class to style specific elements without affecting all tags of that type.

『』
HTML
<!-- HTML -->
<p class="comment">Sample comment</p>
<p>This is a normal paragraph.</p>
『』
CSS
/* CSS */
/* Targets ONLY elements with the class 'comment' */
.comment {
    color: blue;
}

Result: The first paragraph will be blue, but the second paragraph will remain the default text color.

◇Summary & Key Takeaways

  • CSS selectors are the bridge between HTML elements and CSS styling rules.
  • Element Selectors cast a wide net, styling every instance of an HTML tag.
  • Classes (.) offer reusable, targeted styling across multiple different elements.
  • IDs (#) are strict and must be unique, used for one-off elements on a page.
  • Advanced selectors like combinators, attribute selectors, and pseudo-classes provide fine-grained control over complex DOM structures and user states.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Which of the following symbols denotes a CSS Class Selector?

Q2

What is the primary difference between an ID and a Class selector?

Q3

Which selector targets every instance of a specific HTML tag on a page?

Q4

If you want to style an element only when the user's mouse pointer is over it, which selector should you use?

Q5

How would you target an HTML element with the ID main-nav in CSS?

Q6

What do Combinator selectors do?

Q7

Which of the following targets an element based on its HTML attributes, such as type="text"?

Q8

Why is it advantageous to use Class selectors over Element selectors?

Q9

What does the selector h1 { } represent?

Q10

Which selector would you use to style the very first letter of a paragraph differently from the rest of the text?

CSS Selectors | PROJECT ENGRAM