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 when they are clicked.
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 when they are clicked.
I used:
And was not enough; in some browsers it still displayed the link, blinking.
I had to add:
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 :
Disable link that has an exact href:
=
You can choose to disable links that contain a specific href value like so :
Disable a link that contains a piece of path:
*=
Here, any link containing
/keyword/
in path will be disabledDisable a link that begins with:
^=
the
[attribute^=value]
operator target an attribute that starts with a specific value. Allows you to discard websites & root paths.You can even use it to disable non-https links. For example :
Disable a link that ends with:
$=
The
[attribute$=value]
operator target an attribute that ends with a specific value. It can be useful to discard file extensions.Or any other attribute
Css can target any HTML attribute. Could be
rel
,target
,data-custom
and so on...Combining attribute selectors
You can chain multiple rules. Let's say that you want to disable every external link, but not those pointing to your website :
Or disable links to pdf files of a specific website :
Browser support
Attributes selectors are supported since IE7.
:not()
selector since IE9.Only way you could do this without CSS would be to set a CSS on a wrapping div that made your a disappear and something else take it's place.
EG:
With a CSS like
To actually turn off the A you'll have to replace it's click event or href, as described by others.
PS: Just to clarify I'd consider this a fairly untidy solution, and for SEO it's not the best either, but I believe it's the best with purely CSS.
The answer is already in the comments of the question. For more visibility, I am copying this solution here:
For browser support, please see https://caniuse.com/#feat=pointer-events. If you need to support IE there is a workaround; see this answer.
Warning: The use of
pointer-events
in CSS for non-SVG elements is experimental. The feature used to be part of the CSS3 UI draft specification but, due to many open issues, has been postponed to CSS4.