Attribute selectors allow you to target HTML elements based on their attributes (like title, href, type, etc.) or specific values of those attributes. This provides a precise way to style elements without adding extra classes.
[attribute="value"] SelectorTargets elements with a specific attribute that has an exact value.
/* Only targets text inputs */
input[type="text"] {
border-left: 5px solid green;
}
[attribute~="value"] SelectorTargets elements with an attribute value containing a specific word (separated by spaces).
class="highlight".[class~="highlight"] {
background-color: yellow;
}
You can target elements based on whether their attribute value starts with, ends with, or contains a string.
[attribute^="value"]The ^= selector targets values beginning with a specified string (often used for targeting protocols like https or mailto).
[attribute$="value"]The $= selector targets values ending with a specified string (useful for styling file extensions).
[attribute*="value"]The *= selector targets values that contain the specified string anywhere within the value.
/* Starts with 'https' */
a[href^="https"] { color: green; }
/* Ends with '.pdf' */
a[href$=".pdf"] { text-decoration: underline wavy red; }
/* Contains 'example' anywhere */
[title*="example"] { background-color: #e8f5e9; }