Is there any way to disable a link using CSS?
I have a class called current-page
and want links with this class to be disabled so that no action occurs
If you want it to be CSS only, the disabling logic should be defined by CSS.
To move the logic in the CSS definitions, you'll have to use attribute selectors. Here are some examples :
=
You can choose to disable links that contain a specific href value like so :
Exact path
[href="//website.com/exact/path"]{
pointer-events: none;
}
*=
Here, any link containing /keyword/
in path will be disabled
Contains in path
[href*="/keyword/"]{
pointer-events: none;
}
^=
the [attribute^=value]
operator target an attribute that starts with a specific value. Allows you to discard websites & root paths.
Begins with path
[href^="//website.com/begins/with"]{
pointer-events: none;
}
You can even use it to disable non-https links. For example :
a:not([href^="https://"]){
pointer-events: none;
}
$=
The [attribute$=value]
operator target an attribute that ends with a specific value. It can be useful to discard file extensions.
Link to pdf
[href$=".pdf"]{
pointer-events: none;
}
Css can target any HTML attribute. Could be rel
, target
, data-custom
and so on...
Blank link
[target=_blank]{
pointer-events: none;
}
You can chain multiple rules. Let's say that you want to disable every external link, but not those pointing to your website :
a[href*="//"]:not([href*="my-website.com"]) {
pointer-events: none;
}
Or disable links to pdf files of a specific website :
Link to image
[href^="//website.com"][href$=".jpg"] {
color: red;
}
Attributes selectors are supported since IE7. :not()
selector since IE9.