|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: Attribute Selectors |
| 3 | +description: "Learn how to use Attribute Selectors to target elements based on the presence, exact value, or partial match of their HTML attributes, such as type, href, or target." |
| 4 | +keywords: [CSS Attribute Selectors, Attribute presence, Attribute value, Substring matching, CSS Selectors, CodeHarborHub] |
| 5 | +sidebar_label: Attribute Selectors |
| 6 | +--- |
| 7 | + |
| 8 | +So far, we've targeted elements by their tag name, class, ID, or position. **Attribute Selectors** give us a powerful new way to target elements based on the attributes they possess in the HTML (like `type`, `href`, `title`, or `disabled`). |
| 9 | + |
| 10 | +This is incredibly useful for styling elements that share the same tag but have different functions (e.g., different input types). |
| 11 | + |
| 12 | +<AdsComponent /> |
| 13 | +<br /> |
| 14 | + |
| 15 | +## The Basic Syntax |
| 16 | + |
| 17 | +Attribute selectors are always enclosed in **square brackets (`[]`)**. |
| 18 | + |
| 19 | +### 1. Attribute Presence Selector `[attr]` |
| 20 | + |
| 21 | +This is the simplest form. It selects any element that simply **has the specified attribute**, regardless of its value. |
| 22 | + |
| 23 | +| Syntax | Example | Targets | |
| 24 | +| :--- | :--- | :--- | |
| 25 | +| **`[attr]`** | `[disabled]` | All elements that possess the **`disabled`** attribute, regardless of its value. | |
| 26 | +| **`a[title]`** | `a[title]` | All `<a>` tags that have a **`title`** attribute present. | |
| 27 | +| **`[attr="value"]`** | `[type="submit"]` | All elements where the `type` attribute value is **exactly `"submit"`**. | |
| 28 | +| **`a[target="_blank"]`** | `a[target="_blank"]` | All links that have the `target` attribute set **exactly to `"_blank"`**. | |
| 29 | + |
| 30 | +```css title="Styles any input that is disabled, regardless of its type" |
| 31 | +input[disabled] { |
| 32 | + opacity: 0.6; |
| 33 | + cursor: not-allowed; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Exact Attribute Value Selector `[attr="value"]` |
| 38 | + |
| 39 | +This selects any element that has the specified attribute **and** its value exactly matches the one provided. |
| 40 | + |
| 41 | +| Syntax | Example | Targets | |
| 42 | +| :--- | :--- | :--- | |
| 43 | +| `[attr="value"]` | `[type="submit"]` | All elements whose `type` attribute is **exactly** `"submit"`. | |
| 44 | +| `a[target="_blank"]` | `a[target="_blank"]` | All anchor tags (`<a>`) whose `target` attribute is **exactly** `"_blank"` (opens in a new tab). | |
| 45 | + |
| 46 | +```css title="Targets only submit buttons" |
| 47 | +input[type="submit"] { |
| 48 | + background-color: #28a745; /* Green */ |
| 49 | + color: white; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +<AdsComponent /> |
| 54 | +<br /> |
| 55 | + |
| 56 | +## Substring Matching (Advanced Selectors) |
| 57 | + |
| 58 | +CSS offers several specialized operators for matching only a *part* of the attribute's value. |
| 59 | + |
| 60 | +| Operator | Syntax | Description | Specificity Score | |
| 61 | +| :--- | :--- | :--- | :--- | |
| 62 | +| **`~=`** | `[attr~="value"]` | Contains the whole **word** (`value`), separated by spaces. (e.g., `class="nav link"`, targets `link`) | (0, 0, 1, 0) | |
| 63 | +| **<code>|=</code>** | <code>[attr|="value"]</code> | Starts with the whole **word** (`value`), followed by a **hyphen** (`-`). (e.g., `lang="en-US"`) | (0, 0, 1, 0) | |
| 64 | +| **`^=`** | `[attr^="value"]` | **Starts with** the exact string (`value`). | (0, 0, 1, 0) | |
| 65 | +| **`$=`** | `[attr$="value"]` | **Ends with** the exact string (`value`). (e.g., targeting `.pdf` links) | (0, 0, 1, 0) | |
| 66 | +| **`*=`** | `[attr*="value"]` | **Contains** the exact string (`value`) **anywhere** in the attribute. | (0, 0, 1, 0) | |
| 67 | + |
| 68 | +### Example: Targeting File Extensions |
| 69 | + |
| 70 | +You can use the `$=` selector to target links that end with a specific file extension, like `.pdf`. |
| 71 | + |
| 72 | +```css title="styles.css" |
| 73 | +/* Targets any link whose 'href' ends with '.pdf' */ |
| 74 | +a[href$=".pdf"] { |
| 75 | + padding-right: 20px; |
| 76 | + background: url('/icons/pdf.png') no-repeat right center; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Example: Targeting Partial Class Names |
| 81 | + |
| 82 | +You can use `*=` to style elements whose class names contain a specific component, which is useful in utility frameworks. |
| 83 | + |
| 84 | +```css |
| 85 | +/* Targets any element whose class contains the word 'btn-' */ |
| 86 | +[class*="btn-"] { |
| 87 | + cursor: pointer; |
| 88 | + border-radius: 4px; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Specificity Score |
| 93 | + |
| 94 | +Attribute Selectors have the same medium specificity as **Class Selectors**. |
| 95 | + |
| 96 | +| Selector Type | Specificity Score | |
| 97 | +| :--- | :--- | |
| 98 | +| **Attribute Selector** | **(0, 0, 1, 0)** | |
| 99 | + |
| 100 | +A score of **10** means they easily override Element Selectors but can be overridden by ID Selectors. |
| 101 | + |
| 102 | +<AdsComponent /> |
| 103 | +<br /> |
| 104 | + |
| 105 | +## Interactive Attribute Selector Demo |
| 106 | + |
| 107 | +In this demo, the CSS targets inputs based on their `type` attribute. Notice how different selectors are needed for the text field and the password field. |
| 108 | + |
| 109 | +<CodePenEmbed |
| 110 | + title="Interactive Attribute Selector Demo" |
| 111 | + penId="OPNbqRE" |
| 112 | +/> |
| 113 | + |
| 114 | +**Challenge:** Remove the `required` attribute from the text input in the HTML. The red border will instantly disappear, showing the power of the presence selector `[required]`. |
0 commit comments