An attribute selector CSS rule matches an element by an attribute written in its markup, never by its text. Use [attr] to require the attribute, [attr="value"] for an exact value, and *=, ^= or $= for a partial match anywhere, at the start or at the end of the value. The input is a CSS rule plus some HTML, the output is the set of elements that rule styles, and I check each rule by outlining matches in the CSS Editor live preview.
Two more answers up front. Case sensitivity depends on the attribute: type values match without regard to ASCII case in HTML, while href and class values are case-sensitive unless you add the i flag. And an attribute selector has the same specificity as a class selector, 0-1-0, so a tie is settled by source order.
Table of Contents

Attribute selector CSS basics: what the browser tests
An attribute selector looks at the attribute string on the element in the document. It does not read the element’s text content, resolve a relative URL into an absolute one, or consult an input’s default state. That one idea explains most of the surprises later in this article.
These are the forms defined in Selectors Level 4. I quote values in every example because it keeps rules readable.
| Selector | Matches when | Example |
|---|---|---|
[attr] | The attribute is present, even with an empty value | a[href] |
[attr="value"] | The whole value equals the string | input[type="email"] |
[attr*="part"] | The string appears anywhere in the value | [class*="card-"] |
[attr^="prefix"] | The value begins with the string | a[href^="/docs/"] |
[attr$="suffix"] | The value ends with the string | a[href$=".pdf"] |
[attr~="word"] | One whitespace-separated token equals the word | [class~="card"] |
[attr|="en"] | The value is exactly en or starts with en- | [lang|="en"] |
Two details are worth remembering. First, [attr] only asks whether the attribute exists, so a link with href="" still matches a[href]. Second, the three substring operators (*=, ^= and $=) never match when the search string is empty, according to the same specification.
If you want a gentler walkthrough of the same ideas, the MDN attribute selectors lesson covers them with small examples.

Match links by href and inputs by type
Links and form controls are where attribute selectors earn their keep, because the attribute already carries the distinction you want to style. A CSS href attribute selector is the most common case. Here are four rules with different strictness.
a[href] { outline: 3px solid teal; }
a[href^="/docs/"] { outline: 3px solid orange; }
a[href$=".pdf"] { outline: 3px solid green; }
input[type="email"] { outline: 3px solid purple; }a[href] matches links that have an href attribute at all. An anchor element without one, sometimes used as a placeholder, is skipped. The prefix rule a[href^="/docs/"] is a literal string test on the start of the attribute, so it matches href="/docs/intro" but not href="docs/intro" and not an absolute address that contains /docs/ later in the string.
The suffix rule a[href$=".pdf"] matches /files/guide.pdf but misses /files/guide.pdf?download=1, because that string ends with download=1. You can list two selectors, or accept the broader a[href*=".pdf"], knowing it would also match something like /files/guide.pdf.html. Pick the one that reflects what you actually mean by a PDF link.
For form controls, input[type="email"] matches an input whose type attribute is email. Now consider an input with no type attribute. HTML’s missing-value default for the input type is the Text state, as described in the input element section of the HTML Standard, yet input[type="text"] does not match it, because the selector inspects the attribute and not the resolved state. To cover both cases, write this:
input:not([type]),
input[type="text"] { border: 2px solid #444; }
Contains CSS selector: partial matches on class fragments
The contains css selector is [attr*="part"]. It tests whether the part appears anywhere inside the attribute string. That is useful for generated names that share a fragment, and risky for the class attribute, because a class attribute holds a space-separated list and the selector sees it as one long string.
Take a card that has class="card card-featured" and a different element with class="cardinal". Here is what each rule does:
[class*="card"] { outline: 3px solid crimson; } /* both elements */
[class~="card"] { outline: 3px solid navy; } /* only the first, same as .card */
[class="card"] { outline: 3px solid gold; } /* neither: needs the whole value to be card */
[class^="card-"] { outline: 3px solid pink; } /* neither: the value starts with "card " */The first rule catches cardinal because the substring card is in it. The second and the plain .card selector compare whole class tokens, so they do not. The exact-value rule fails on the first element because its attribute has two tokens. The prefix rule fails because it tests the beginning of the whole attribute string, so it depends on token order. If a prefix fragment can appear in any position, a common pattern is to pair the prefix with a space-led substring:
[class^="card-"],
[class*=" card-"] { border-left: 4px solid teal; }This matches any class token that starts with card-, whether it is first or later in the list. It would still miss a token like my-card-x, which is what you usually want.
A partial match is the right choice in a few situations:
- You target a scheme with
a[href^="mailto:"]ora[href^="tel:"]. - Generated or third-party markup shares a prefix you cannot change, such as
icon-names. - A data attribute holds a structured value, for example
[data-state*="error"].
When you control the markup and want a whole class, use .card or [class~="card"] instead. A substring match is a string test, not a class-token, URL-host or file-type check.
Case sensitivity and the i and s flags
In HTML documents, attribute names are matched without regard to ASCII case, so [HREF] and [href] behave the same. Attribute values are different. Value matching normally follows the document language, and the HTML Standard lists type among the values that selectors match in an ASCII case-insensitive way, as described in its selectors section. That means input[type="EMAIL"] matches type="email". Values of class, id, data-* and href are ordinarily case-sensitive.
This is the answer to the CSS attribute selector case insensitive question. Add a trailing i inside the brackets to request ASCII case-insensitive value matching, or s to request case-sensitive matching:
a[href$=".PDF" i] { outline: 3px solid teal; }
[data-kind="Note" s] { outline: 3px solid orange; }The first rule matches guide.pdf, guide.PDF and guide.Pdf. The flags do not provide general Unicode case folding, so do not rely on i for accented or non-Latin text. I also would not assume every modifier behaves identically in every browser. The MDN attribute selectors reference, last modified on November 12, 2026, labels attribute selectors as broadly available but cautions that some parts vary in support. If you rely on a flag, test it in the browsers you support.
Attribute selector CSS specificity compared with a class selector
Specificity decides which rule wins when several match the same element. An attribute selector and a class selector each add 0-1-0. An element name adds 0-0-1. The MDN specificity guide describes the full model. For attribute selectors it works out like this:
[href]is0-1-0..linkis0-1-0, a tie with[href].a[href]is0-1-1.input[type="email"]is0-1-1, and so isinput.error.
.link { color: green; }
[href] { color: red; } /* ties with .link, later rule wins */
a[href] { color: blue; } /* 0-1-1 wins over both, regardless of order */On a link that has both class="link" and an href, the first two rules tie, so source order decides and red beats green. The third rule is more specific and wins. This only applies when the declarations otherwise have equal cascade precedence, so things like importance and cascade layers still take priority over specificity.
This is why a quick attribute rule can override a class rule by accident when it names the element too. The reverse also happens, and the same tie logic shows up with zero-specificity wrappers, which I walk through in my guide to a rule inside :where() losing to an element selector. Keep one habit: when a rule does not apply, compare the specificity of both selectors before reaching for more attributes.
Test every rule in the CSS Editor live preview
The CSS Editor has syntax highlighting, property auto-completion and a live preview on a fixed sample page, plus beautify and minify. It does not lint, so a typo in a selector simply produces no visible effect. The fixed page contains a link whose href is #, a button with the class btn, and no input. I add one rule at a time and watch that sample:
- Open the CSS Editor and add
a[href] { outline: 3px solid teal; }. The link gets an outline. - Replace it with
a[href^="#"] { outline: 3px solid orange; }. The same link is still matched, because its value starts with a hash. - Replace that with
[class~="btn"] { outline: 3px solid purple; }. The button is outlined. - Try
a[href^="/docs/"],a[href$=".pdf"]orinput[type="email"]. Nothing changes, and that is correct, because the sample page has no such link or input.
For the full set of operators you need your own markup. The CSS Editor does not accept replacement sample HTML, so paste a small page into the HTML Code Editor, which has a sandboxed live preview, and put the CSS in a style element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Attribute selector test</title>
<style>
a[href^="/docs/"] { outline: 3px solid orange; }
a[href$=".pdf"] { outline: 3px solid teal; }
input[type="email"] { outline: 3px solid purple; }
[class*="card"] { outline: 3px dashed crimson; }
[class~="card"] { background: #fff3c4; }
</style>
</head>
<body>
<p><a href="/docs/intro">Intro</a></p>
<p><a href="/files/guide.pdf">Guide</a></p>
<p><a href="/files/guide.pdf?download=1">Guide download</a></p>
<p><label>Email <input type="email"></label></p>
<p><label>Name <input></label></p>
<div class="card card-featured">Featured card</div>
<div class="cardinal">Cardinal</div>
</body>
</html>Before you look at the preview, predict each match. The expected result is an orange outline on the docs link, a teal outline on guide.pdf only, a purple outline on the email input, and nothing on the input without a type. Both divs get the dashed crimson outline, because *="card" also catches cardinal, but only the featured card gets the yellow background. If your prediction and the preview disagree, the difference usually teaches you which part of the string the selector really tested.
For the wider picture of what the visual CSS editor live preview can and cannot show, I cover its limits in a separate guide.
Limits to remember and how to verify a match
Attribute selectors are a string test, and a clean preview proves only that the string matched. Keep these limits in mind:
- A substring match on
hrefis not a reliable check for an external domain or a file type. A rule likea[href*="example.org"]also matcheshttps://example.org.test/pageand any address with that text in a query string. Do not use it as a security decision. - Attribute selectors read the attribute as written. They do not follow redirects, resolve relative paths or inspect what a link points to.
- Formatting a stylesheet or previewing it says nothing about whether the markup you ship has the attribute values you expect.
To verify a rule, outline the matches with a loud outline, then test negative cases too: include an element that should not match, like the query-string link or cardinal, and confirm it stays plain. When a rule that uses classes stops working after you edit markup in the homepage editor, remember that it is a Quill rich-text editor, so custom class and ID attributes can disappear when you apply source changes. Test class-based selectors in the HTML Code Editor instead.
Minification is another separate step. After you finish a stylesheet, you can tidy it with the CSS Beautifier or shrink it with the CSS Minifier, then recheck the preview, since whitespace changes can affect selectors, as in my write-up on a minifier removing the space before :focus.
Frequently asked questions
What is the contains CSS selector?
It is the attribute selector written as [attr*=”part”]. It matches an element when the given string appears anywhere inside the attribute value, for example [class*=”card”] or a[href*=”.pdf”]. It is a plain substring test, so it can match more than you intend, such as the class cardinal.
How do I select an element by a partial href value in CSS?
Use a CSS href attribute selector with the operator that fits the position of the text. Use a[href^=”/docs/”] for the start, a[href$=”.pdf”] for the end, and a[href*=”docs”] for anywhere in the value. Each one compares the attribute string exactly as written in the markup.
Is a CSS attribute selector case-sensitive?
It depends on the attribute. In HTML, attribute names are matched without regard to ASCII case, and type values also match case-insensitively. Values such as class, id, data attributes and href are ordinarily case-sensitive. Add i before the closing bracket, as in a[href$=”.PDF” i], to request ASCII case-insensitive matching, and test it in the browsers you support.
How does attribute selector specificity compare to a class selector?
They are equal. Each attribute selector and each class selector adds 0-1-0, so [href] ties with .link. Adding an element name raises the value, so a[href] is 0-1-1 and beats both. When specificity and cascade precedence are equal, the later rule in source order wins.
Why does a[href$=".pdf"] miss my download link?
Because the selector tests the end of the attribute string. A link such as /files/guide.pdf?download=1 ends with download=1, not .pdf. Add a second selector for the query-string form, use a broader a[href*=”.pdf”] if that is acceptable, or add a class to the markup.
Why does input[type="text"] not match an input with no type attribute?
Attribute selectors inspect the attribute in the markup, not the element’s resolved state. HTML treats a missing type as the Text state, but there is no type attribute to compare. Use input:not([type]), input[type=”text”] to cover both.
Can I test my own HTML with attribute selectors in the CSS Editor?
The CSS Editor previews your rules on a fixed sample page with a link whose href is #, a button with the class btn and no input, so it cannot show rules aimed at other markup. For your own markup, paste the HTML with a style element into the HTML Code Editor and use its live preview.
Next steps
A dependable attribute selector CSS workflow is simple: decide whether you need presence, an exact value, a token or a substring, predict what should match, then prove it with an outline. Your next step is to open the editor, add a[href^="#"] { outline: 3px solid orange; } to the sample page, and then paste the longer test page into the HTML Code Editor to see every operator side by side.
Related tools and resources on HTML Editor Online:
- CSS Editor
- CSS Where Specificity Rule Not Applying
- HTML Code Editor
- Visual CSS Editor Browser Live Preview
