--- id: css-structural-pseudo-classes title: "CSS Structural Pseudo-classes" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["structural pseudo-classes", ":first-child", ":lang", "nth-child", "position-based selectors", "tree-position selectors"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "pseudo-classes", "selectors"] raw_sources: ["https://www.w3schools.com/css/css_pseudo_classes_structural.asp"] applied_in: [] github_commit: "" --- # [[CSS Structural Pseudo-classes]] ## π― ν μ€ ν΅μ°° (One-line insight) Structural pseudo-classes select elements based on their position in the document tree, letting you target things like the first child or language-tagged elements without adding extra classes. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **Definition** β Structural pseudo-classes select elements based on their position in the document tree. [S1] - **`:first-child`** β Matches an element that is the first child of another element. [S1] - **`:lang()`** β Used to select elements with a `lang` attribute set to the specified value. [S1] - **Position is relative to the parent** β selection depends on where the element sits among its siblings, not on a class or id. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **First-child targeting** β `selector:first-child` styles only the element that appears first inside its parent. [S1] - **Combining position with descent** β `p em:first-child` vs `p:first-child em` produce different results depending on which element the `:first-child` is attached to. [S1] - **Language-aware styling** β `:lang(code)` applies styles (e.g. quotation marks) only to elements carrying a matching `lang` value. [S1] ## π μΈλΆ λ΄μ© (Details) **Introduction** [S1] Structural pseudo-classes select elements based on their position in the document tree. **The `:first-child` pseudo-class** [S1] The `:first-child` pseudo-class matches a specified element that is the first child of another element. Style all first child `
` elements:
```css
p:first-child {
color: blue;
}
```
Style the first `` element in all ` ` elements:
```css
p em:first-child {
color: blue;
}
```
Style all `` elements in all first child ` ` elements:
```css
p:first-child em {
color: blue;
}
```
**The `:lang()` pseudo-class** [S1]
The `:lang()` pseudo-class is used to select elements with a `lang` attribute with the specified value.
Define quotation marks for ` Some text ` elements with `lang="no"` (Norwegian):
```html
A quote in a paragraph
Some text.