Css attribute selector does not work a href
If your a[href]
CSS attribute selector is mysteriously misbehaving, ensure your selector's match is identical to your href
value in your HTML link.
Partial matches have got you covered with more options: ^=
(starts with), $=
(ends with), and *=
(contains):
Just be certain that no other stronger rule is flexing its muscles to override your CSS
Styling links to PDF files
If you're doing some interior design for PDF links, the $=
selector looks for URLs ending in .pdf
:
Dealing with Query Strings and Anchors
Got query strings or anchors in the URL? Here are the selectors to the rescue:
These can form a super team for more complete styling:
Minding your syntax
Always stay hyper-alert for CSS syntax gremlins; a lone miscreant quote or bracket could spell modern catastrophe for your selector.
Validate, validate, validate. Make sure the link is an <a>
tag with href
attribute directing to a PDF.
Making use of other attributes
For more finesse in your design, you can double-date with href
and other attributes:
Getting the hierarchy right
Sometimes, it's not the attribute selector causing issues, but another rule flexing its specificity muscles:
Playing in the JSFiddle sandbox
Try out different link scenarios in the JSFiddle playground to see how your selectors perform. Test out the PDF attribute selectors in action here: JSFiddle Example.
Was this article helpful?