--- id: css-selectors title: "CSS Selectors" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["CSS selector", "element selector", "id selector", "class selector", "universal selector", "simple selectors"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "selectors"] raw_sources: ["https://www.w3schools.com/css/css_selectors.asp"] applied_in: [] github_commit: "" --- # [[CSS Selectors]] ## π― ν μ€ ν΅μ°° (One-line insight) CSS selectors are used to "find" (select) the HTML elements you want to style, ranging from simple selectors that match by name, id, or class to combinator, pseudo-class, pseudo-element, and attribute selectors. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **Purpose** β a CSS selector selects the HTML element(s) you want to style. [S1] - **Five categories** β Simple selectors (by name, id, class), Combinator selectors, Pseudo-class selectors, Pseudo-elements selectors, and Attribute selectors. [S1] - **Element selector** β selects elements based on the element name (e.g. `p`). [S1] - **id selector** β uses the `#` character plus the id of a specific element; an id is unique within a page. [S1] - **class selector** β uses a `.` followed by a class name; multiple elements can share a class. [S1] - **Naming rule** β an id name cannot start with a number, and a class name cannot start with a number. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **By type** β `element { ... }` styles all elements of that tag. [S1] - **By id** β `#id { ... }` styles the one element with that id. [S1] - **By class** β `.class { ... }` styles every element carrying that class. [S1] - **Type + class scoping** β `element.class { ... }` styles only those elements of a type that also carry the class. [S1] - **Multiple classes** β an element can reference more than one class at once via a space-separated `class` attribute. [S1] ## π μΈλΆ λ΄μ© (Details) A CSS selector is used to find (select) the HTML elements you want to style. CSS selectors divide into five categories: Simple selectors (select elements based on name, id, class), Combinator selectors, Pseudo-class selectors, Pseudo-elements selectors, and Attribute selectors. [S1] **The CSS element selector** selects HTML elements based on the element name. [S1] ```css p { text-align: center; color: red; } ``` **The CSS id selector** uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so it selects one unique element. To select an element with a specific id, write a hash (`#`) character followed by the id of the element. [S1] ```css #para1 { text-align: center; color: red; } ``` Note: an id name cannot start with a number! [S1] **The CSS class selector** selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (`.`) character followed by the class name. [S1] ```css .center { text-align: center; color: red; } ``` You can also specify that only specific HTML elements should be affected by a class. In this example only `
` elements with `class="center"` will be center-aligned and red: [S1] ```css p.center { text-align: center; color: red; } ``` HTML elements can also refer to more than one class: [S1] ```html
This paragraph refers to two classes.
``` Note: a class name cannot start with a number! [S1] **The CSS universal selector** (`*`) selects all HTML elements on the page. [S1] ```css * { text-align: center; color: blue; } ``` **The CSS grouping selector** selects all the HTML elements with the same style definitions. To group selectors, separate each selector with a comma. [S1] ```css h1, h2, p { text-align: center; color: red; } ``` **All CSS Simple Selectors** [S1] | Selector | Example | Example description | | --- | --- | --- | | `#id` | `#firstname` | Selects the element with id="firstname" | | `.class` | `.intro` | Selects all elements with class="intro" | | `element.class` | `p.intro` | Selects only `` elements with class="intro" | | `*` | `*` | Selects all elements | | `element` | `p` | Selects all `
` elements | | `element,element,..` | `div, p` | Selects all `
` elements | ## π οΈ μ μ© μ¬λ‘ (Applied in summary) The page's own examples apply each selector type to style headings and paragraphs (element, id, class, type+class, universal, and grouping). No external project/commit applications found in the source. ## π» μ½λ ν¨ν΄ (Code patterns) Selecting by id and by class (language: CSS): ```css #para1 { text-align: center; color: red; } .center { text-align: center; color: red; } ``` Scoping a class to a specific element type: ```css p.center { text-align: center; color: red; } ``` ## βοΈ λΉκ΅ λ° μ ν κΈ°μ€ (Comparison & decision criteria) | Selector | When to use | Scope | | --- | --- | --- | | `element` (e.g. `p`) | Style every element of a tag uniformly | All elements of that type [S1] | | `#id` (e.g. `#para1`) | Style one unique element | A single element (id is unique per page) [S1] | | `.class` (e.g. `.center`) | Reuse a style across many elements | All elements carrying that class [S1] | | `element.class` (e.g. `p.center`) | Reuse a class but limit it to one tag | Only that element type with the class [S1] | | `*` | Apply a baseline to everything | All elements on the page [S1] | ## βοΈ λͺ¨μ λ° μ λ°μ΄νΈ (Contradictions & updates) No contradictions found in the source. ## β κ²μ¦ μν λ° μ λ’°λ - **μν:** draft - **κ²μ¦ λ¨κ³:** conceptual (μ€μ μ μ© μ¬λ‘ λ°κ²¬ μ applied/validatedλ‘ μΉκ²© κ°λ₯) - **μΆμ² μ λ’°λ:** B (W3Schools β widely used educational reference, not a primary standards body) - **μ λ’° μ μ:** 0.89 - **μ€λ³΅ κ²μ¬ κ²°κ³Ό:** μ κ· μμ± (New discovery) ## π μ§μ κ·Έλν (Knowledge Graph) - **μμ/루νΈ:** [[CSS Tutorial]] - **κ΄λ ¨ κ°λ :** [[CSS Syntax]], [[CSS Grouping Selectors]], [[CSS Introduction]] - **μ°Έμ‘° λ§₯λ½:** Referenced whenever deciding how to target HTML elements for styling. ## π μΆμ² (Sources) - [S1] W3Schools β CSS Selectors β https://www.w3schools.com/css/css_selectors.asp ## π λ³κ²½ μ΄λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "CSS Selectors" page (Astra wiki-curation, P-Reinforce v3.1 format).