---
id: css-attribute-selectors
title: "CSS Attribute Selectors"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["attribute selector", "[attribute] selector", "[attribute=value]", "[attribute~=value]", "[attribute|=value]"]
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", "attributes"]
raw_sources: ["https://www.w3schools.com/css/css_attribute_selectors.asp"]
applied_in: []
github_commit: ""
---
# [[CSS Attribute Selectors]]
## π― ν μ€ ν΅μ°° (One-line insight)
CSS attribute selectors use square brackets to target HTML elements by the presence or value of an attribute β from a plain `[attribute]` match through exact (`=`), word-list (`~=`), and hyphen-prefix (`|=`) matching. [S1]
## π§ ν΅μ¬ κ°λ
(Core concepts)
- **Attribute selector** β selects and styles HTML elements with a specific attribute, a specific attribute value, or both. [S1]
- **Square-bracket syntax** β attribute selectors are written inside square brackets `[]`. [S1]
- **`[attribute]`** β selects elements that have the specified attribute, regardless of value. [S1]
- **`[attribute="value"]`** β selects elements with a specific attribute and an exact value. [S1]
- **`[attribute~="value"]`** β selects elements whose attribute value is a space-separated list of words, one of which is the value. [S1]
- **`[attribute|="value"]`** β selects elements whose value is exactly the value, or starts with the value followed by a hyphen (`-`). [S1]
## π§© μΆμΆλ ν¨ν΄ (Extracted patterns)
- **Presence targeting** β style every element carrying a given attribute (e.g. every link with a `target`) without touching the HTML. [S1]
- **Word-membership match** β `~=` matches when the value appears as one whole word in a space-separated list, useful for multi-token attributes. [S1]
- **Language/prefix match** β `|=` matches a whole value or a value immediately followed by a hyphen, the classic pattern for language codes (`en`, `en-US`). [S1]
## π μΈλΆ λ΄μ© (Details)
**What are attribute selectors?**
CSS attribute selectors are used to select and style HTML elements with a specific attribute or attribute value, or both. Attribute selectors use square brackets `[]`. [S1]
**Tip:** The attribute selectors are case-sensitive by default. To perform a case-insensitive match, add an `i` before the closing bracket. [S1]
**Example 1 β `[attribute]` selector**
Selects all `` elements with a `target` attribute: [S1]
```css
a[target] {
background-color: yellow;
}
```
**Example 2 β `[attribute="value"]` selector**
Selects all `` elements with a `target="_blank"` attribute: [S1]
```css
a[target="_blank"] {
background-color: yellow;
}
```
**Example 3 β `[attribute~="value"]` selector**
Selects all elements with a `title` attribute that contains a space-separated list of words, one of which is "flower." Per the source note, this will match elements with `title="flower"`, `title="summer flower"`, and `title="flower new"`, but not `title="my-flower"` or `title="flowers"`: [S1]
```css
[title~="flower"] {
border: 5px solid yellow;
}
```
**Example 4 β `[attribute|="value"]` selector**
Selects elements with the specific attribute whose value can be exactly the value, or start with the value followed by a hyphen (`-`). **Note:** The value has to be a whole word, either alone, like `class="top"`, or followed by a hyphen (`-`), like `class="top-text"`: [S1]
```css
[class|="top"] {
background: yellow;
}
```
## π οΈ μ μ© μ¬λ‘ (Applied in summary)
The page's four examples demonstrate applying each selector form to links and titled/classed elements. No external project/commit applications found in the source.
## π» μ½λ ν¨ν΄ (Code patterns)
The four basic attribute-selector forms (language: CSS):
```css
a[target] { background-color: yellow; } /* has attribute */
a[target="_blank"] { background-color: yellow; } /* exact value */
[title~="flower"] { border: 5px solid yellow; } /* word in list */
[class|="top"] { background: yellow; } /* value or value- */
```
## βοΈ λΉκ΅ λ° μ ν κΈ°μ€ (Comparison & decision criteria)
- **`[attribute]`** β use when only the presence of the attribute matters, not its value. [S1]
- **`[attribute="value"]`** β use when you need an exact, complete value match. [S1]
- **`[attribute~="value"]`** β use when the attribute holds a space-separated word list and you want elements where one whole word equals the value (does not match substrings like `flowers` or hyphenated `my-flower`). [S1]
- **`[attribute|="value"]`** β use when you want the exact value or that value followed by a hyphen, e.g. matching `top` and `top-text`. [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 Advanced Attribute Selectors]], [[CSS Selectors]], [[CSS Combinators]]
- **μ°Έμ‘° λ§₯λ½:** Referenced whenever styling elements by their attributes β links by `target`, inputs by `type`, elements by `title`/`lang`/`class`.
## π μΆμ² (Sources)
- [S1] W3Schools β CSS Attribute Selectors β https://www.w3schools.com/css/css_attribute_selectors.asp
## π λ³κ²½ μ΄λ ₯ (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "CSS Attribute Selectors" page (Astra wiki-curation, P-Reinforce v3.1 format).