924

Is there a 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RSK
  • 17,210
  • 13
  • 54
  • 74
  • 48
    after a lot of googling i got the perfect answer for this question http://css-tricks.com/pointer-events-current-nav/ – RSK May 26 '10 at 14:13
  • 1
    Whether a link should be used or not carries more semantical than presentational value. It should not be disabled through CSS, but through utilizing the `hidden` attribute that is applicable to any HTML element. CSS then can be used to select e.g.`a[hidden]` anchor and style it accordingly. – Armen Michaeli Jul 11 '16 at 12:22
  • @amn but i don't think browsers will display an element with the hidden attribute so styling becomes moot. – user1794469 Jan 11 '17 at 15:59
  • 1
    @user1794469 They will if you instruct them to, with CSS, using `display: block`, for instance or some other value for `display`. But `hidden` is not always applicable -- it's for elements that are *irrelevant*, and from the question it is not unclear *why* the link should be disabled. This is probably a case of XY problem. – Armen Michaeli Jan 19 '17 at 13:05
  • Clicking is just one form somebody can interact with a link. Avoiding any event on click is therefore not really disabling the link entirely. Search engines as well will still consider the link. – Andy Jul 20 '22 at 12:57

26 Answers26

1441

From this solution:

[aria-current="page"] {
  pointer-events: none;
  cursor: default;
  text-decoration: none;
  color: black;
}
<a href="link.html" aria-current="page">Link</a>

For browser support, please see https://caniuse.com/#feat=pointer-events. If you need to support Internet Explorer, 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 CSS 3 UI draft specification but, due to many open issues, has been postponed to CSS 4.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RSK
  • 17,210
  • 13
  • 54
  • 74
  • You can always use a polyfill for IE support, I find myself needing pointer-events: none frequently. – Ryan Taylor Oct 21 '13 at 22:14
  • 51
    Also, this doesnt avoid tabbing to the link then enter. – Jono Dec 22 '13 at 09:26
  • 4
    If you would style it a bit, so the user can see it's disabled. Give it some opacity: .2 – DNRN Feb 04 '14 at 10:13
  • 4
    This now works in all modern browsers including IE 11. If you need support for IE 10 and below, you can use a JavaScript polyfill such as [this one](https://github.com/kmewhort/pointer_events_polyfill). – Keavon Jul 31 '14 at 05:13
  • 33
    Important note: This only disables clicking, not the actual link itself. You can still use tab + enter to "click" the link. – Pikamander2 Sep 23 '14 at 23:55
  • This does not work to disable hyperlinks in print preview in Chrome. – Andrew Koper Nov 20 '14 at 21:29
  • 11
    Use of `pointer-events: none;` is not perfect. It also disables other events such as hover, which is required for display of `title="…"` or tooltips. I found the JS solution is better (using `event.preventDefault();`) along with some CSS (`cursor: default; opacity: 0.4;`) and a tooltip explaining why the link is disabled. – Quinn Comendant Jul 24 '15 at 03:34
  • It's also worth resetting your text color and underlining so it doesn't look like a link anymore: `color:#000; text-decoration: none;` – Juniper Jones Mar 29 '16 at 18:21
  • At least on Chrome the "cursor" property is pointless when using "pointer-events: none". AFAIK, it's just ignored. Try to for example set "pointer-events: none; cursor: not-allowed;", and cursor stays at default all the time. – Risto Välimäki Aug 22 '16 at 10:55
  • Any way to change cursor if I use `pointer-events: none;`. I've try to use `cursor: not-allowed;` but it not work. – Mostafa Nawara Nov 16 '16 at 11:03
  • Also it doesn't prevent bubbling so if you have an element underneath, that one gets still triggered – Otto Abnormalverbraucher Aug 29 '17 at 23:27
  • This is precisely what I was looking for: pointer-events: none; It was the missing peace in my puzzle. Thanks a lot. – Samuel Ramzan Mar 15 '18 at 19:20
  • warning: `pointer-events: none` means that if something is *behind* the link, that thing will get clicked. so if you use this in fly-out menus, it could result in accidentally clicking on something that is behind (covered up by) the flyout menu. – Woodrow Barlow Mar 05 '20 at 20:15
  • Tip: adding `color: unset;` will set the color to whatever the parent style text color is. – Gangula Nov 06 '20 at 09:56
  • Checkout the following link to customize cursor pointers: https://codepen.io/anweshgangula/pen/YzWOyrq – Gangula Nov 06 '20 at 18:20
  • If it isn't working for you, add `display: inline-block;` – mschwartz Apr 08 '21 at 19:45
154

.disabled {
  pointer-events: none;
  cursor: default;
  opacity: 0.6;
}
<a href="#" class="disabled">link</a>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Amir Keshavarz
  • 2,403
  • 3
  • 19
  • 19
  • You might need to set the display to inline-block (or something other than inline). – dev_masta Dec 23 '16 at 13:39
  • 1
    nice, but beware pointer-events browser support (i.e. < IE11) : http://caniuse.com/#search=pointer-events – Darren Shewry Mar 27 '17 at 07:49
  • 2
    For style, try changing `pointer-events:none;` to `pointer-events:unset;`. Then, the cursor can be changed to `cursor:not-allowed;`. This gives a better clue as to what is going on to the user. Seems to work in FF, Edge, Chrome, Opera and Brave as of today. – Sablefoste Sep 05 '17 at 01:17
  • 1
    @Sablefoste That doesn't work for me in Chrome 60. The cursor is indeed `not-allowed`, but the link remains clickable. – soupdog Sep 11 '17 at 20:19
124

CSS can only be used to change the style of something. The best you could probably do with pure CSS is to hide the link altogether.

What you really need is some JavaScript code. Here's how you'd do what you want using the jQuery library.

$('a.current-page').click(function() { return false; });
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nickf
  • 537,072
  • 198
  • 649
  • 721
33

CSS can't disable a link. It can inhibit pointer events like clicks, but clicks are not the only way to activate a link. Your options are:

  • Don't include the href or onclick attributes in your <a> tag.
  • Use document.querySelector et al to find the anchor elements you want to disable. Remove their href or onclick attributes so they no longer have link behavior that can be activated by any method.
easeout
  • 8,665
  • 5
  • 43
  • 51
  • 38
    That's not correct answer - pointer-events: none; css can disable it. – Adam Pietrasiak Mar 06 '15 at 21:45
  • 5
    I didn't think of that! Or maybe the attribute didn't exist yet in 2010? In any case it's true that `pointer-events: none` can disable mouse events. However, it doesn't disable the underlying link. In a test I tried in Chrome 81, I can still activate such a link by tabbing to it and typing the return key. – easeout May 27 '20 at 02:18
32

Bootstrap Disabled Link

<a href="#" class="btn btn-primary btn-lg disabled" role="button">Primary link</a>

<a href="#" class="btn btn-default btn-lg disabled" role="button">Link</a>

Bootstrap Disabled Button but it looks like link

<button type="button" class="btn btn-link">Link</button>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Jigar Bhatt
  • 4,217
  • 2
  • 34
  • 42
22

You can set the href attribute to javascript:void(0):

.disabled {
  /* Disabled link style */
  color: black;
}
<a class="disabled" href="javascript:void(0)">LINK</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xinus
  • 29,617
  • 32
  • 119
  • 165
  • 2
    @nickf true, however, this is a neat solution and is better than relying on the poor default IE styling when set to disabled. – SausageFingers Feb 28 '12 at 12:09
  • I think it could be a bit more complicated than that. Here's a solution http://snook.ca/archives/javascript/clear_links_to_1 – Mike Gifford Jun 16 '14 at 21:50
14

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:

<a href="//website.com/exact/path">Exact path</a>
[href="//website.com/exact/path"]{
  pointer-events: none;
}

Disable a link that contains a piece of path: *=

Here, any link containing /keyword/in path will be disabled:

<a href="//website.com/keyword/in/path">Contains in path</a>
[href*="/keyword/"]{
  pointer-events: none;
}

Disable a link that begins with: ^=

The [attribute^=value] operator targets an attribute that starts with a specific value. It allows you to discard websites and root paths.

<a href="//website.com/begins/with/path">Begins with path</a>
[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;
}

Disable a link that ends with: $=

The [attribute$=value] operator targets an attribute that ends with a specific value. It can be useful to discard file extensions.

<a href="/path/to/file.pdf">Link to pdf</a>
[href$=".pdf"]{
  pointer-events: none;
}

Or any other attribute

CSS can target any HTML attribute. Could be rel, target, data-customand so on...

<a href="#" target="_blank">Blank link</a>
[target=_blank]{
  pointer-events: none;
}

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:

a[href*="//"]:not([href*="my-website.com"]) {
    pointer-events: none;
}

Or disable links to pdf files of a specific website :

<a href="//website.com/path/to/file.jpg">Link to image</a>
[href^="//website.com"][href$=".jpg"] {
  color: red;
}

Browser support

Attributes selectors have been supported since Internet Explorer 7. And the :not() selector since Internet Explorer 9.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GuCier
  • 6,919
  • 1
  • 29
  • 36
14

I used:

.current-page a:hover {
    pointer-events: none !important;
}

And that was not enough; in some browsers it still displayed the link, blinking.

I had to add:

.current-page a {
    cursor: text !important;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pablo Molina
  • 327
  • 2
  • 6
13

Apply the below class on HTML.

.avoid-clicks {
  pointer-events: none;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Viplav Soni
  • 1,489
  • 13
  • 18
10

If you want to stick to just HTML/CSS on a form, another option is to use a button. Style it and set the disabled attribute.

E.g. http://jsfiddle.net/cFTxH/1/

Sebastian Patten
  • 7,157
  • 4
  • 45
  • 51
10

One way you could do this with CSS, would be to set a CSS on a wrapping div that you set to disappear and something else takes its place.

For example:

<div class="disabled">
    <a class="toggleLink" href="wherever">blah</a>
    <span class="toggleLink">blah</span
</div>

With a CSS like

.disabled a.toggleLink { display: none; }
span.toggleLink { display: none; }
.disabled span.toggleLink { display: inline; }

To actually turn off the a, you'll have to replace its 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fyjham
  • 7,004
  • 2
  • 32
  • 40
8

Try this:

<style>
    .btn-disable {
        display: inline-block;
        pointer-events: none;
    }
</style>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Benk
  • 1,284
  • 6
  • 33
  • 64
6

The pointer-events property allows for control over how HTML elements respond to mouse/touch events – including CSS hover/active states, click/tap events in JavaScript, and whether or not the cursor is visible.

That's not the only way you disable a link, but it is a good CSS way which work in Internet Explorer 10 (and later) and all new browsers:

.current-page {
  pointer-events: none;
  color: grey;
}
<a href="#" class="current-page">This link is disabled</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alireza
  • 100,211
  • 27
  • 269
  • 172
5

I searched the Internet and found no better than this. Basically, to disable button click functionality, just add CSS style using jQuery like so:

$("#myLink").css({ 'pointer-events': 'none' });

Then to enable it again, do this

$("#myLink").css({ 'pointer-events': '' });

It was checked on Firefox and Internet Explorer 11, and it worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Faisal Mq
  • 5,036
  • 4
  • 35
  • 39
4

body{
  font-family: 'Roboto', sans-serif;
  font-weight: 500;
}
a.disable{
  pointer-events: none;
  color: black;
  text-decoration: none;
}
<a href="https://example.com">Normal</a>
<a href="https://example.com" class="disable">Disable</a>
CodAIK
  • 715
  • 7
  • 7
3

You can use this CSS content:

a.button,button {
    display: inline-block;
    padding: 6px 15px;
    margin: 5px;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid rgba(0, 0, 0, 0);
    border-radius: 4px;
    -moz-box-shadow: inset 0 3px 20px 0 #cdcdcd;
    -webkit-box-shadow: inset 0 3px 20px 0 #cdcdcd;
    box-shadow: inset 0 3px 20px 0 #cdcdcd;
}

a[disabled].button,button[disabled] {
    cursor: not-allowed;
    opacity: 0.4;
    pointer-events: none;
    -webkit-touch-callout: none;
}

a.button:active:not([disabled]),button:active:not([disabled]) {
    background-color: transparent !important;
    color: #2a2a2a !important;
    outline: 0;
    -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .5);
    box-shadow: inset 0 3px 5px rgba(0, 0, 0, .5);
}
<button disabled="disabled">disabled!</button>
<button>click me!</button>
<a href="http://royansoft.com" disabled="disabled" class="button">test</a>
<a href="http://royansoft.com" class="button">test2</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
javad shariaty
  • 939
  • 10
  • 14
  • An explanation would be in order. E.g., what is the idea/gist? Why does it need so much more CSS content than previous answers? Is all of it really necessary? If it is necessary, what is the reason? – Peter Mortensen May 30 '21 at 01:56
2

I combined multiple approaches to provide some more advanced disabled functionality. Here is a gist, and the code is below.

This provides for multiple levels of defense so that anchors marked as disable actually behave as such.

Using this approach, you get an anchor that you cannot:

  • click
  • tab to and hit return
  • tabbing to it will move focus to the next focusable element
  • it is aware if the anchor is subsequently enabled

 

  1. Include this CSS content, as it is the first line of defense. This assumes the selector you use is 'a.disabled'.

    a.disabled {
      pointer-events: none;
      cursor: default;
    }
    
  2. Next, instantiate this class such as (with optional selector):

     $ ->
       new AnchorDisabler()
    

    Here is the CoffeeScript class:

     class AnchorDisabler
       constructor: (selector = 'a.disabled') ->
         $(selector).click(@onClick).keyup(@onKeyup).focus(@onFocus)
    
       isStillDisabled: (ev) =>
         ### since disabled can be a class or an attribute, and it can be dynamically removed, always recheck on a watched event ###
         target = $(ev.target)
         return true if target.hasClass('disabled')
         return true if target.attr('disabled') is 'disabled'
         return false
    
       onFocus: (ev) =>
         ### if an attempt is made to focus on a disabled element, just move it along to the next focusable one. ###
         return unless @isStillDisabled(ev)
    
         focusables = $(':focusable')
         return unless focusables
    
         current = focusables.index(ev.target)
         next = (if focusables.eq(current + 1).length then focusables.eq(current + 1) else focusables.eq(0))
    
         next.focus() if next
    
    
       onClick: (ev) =>
         # disabled could be dynamically removed
         return unless @isStillDisabled(ev)
    
         ev.preventDefault()
         return false
    
       onKeyup: (ev) =>
    
         # 13 is the JavaScript key code for Enter. We are only interested in disabling that, so get out fast
         code = ev.keyCode or ev.which
         return unless code is 13
    
         # disabled could be dynamically removed
         return unless @isStillDisabled(ev)
    
         ev.preventDefault()
         return false
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kross
  • 3,627
  • 2
  • 32
  • 60
1

Demo here
Try this one

$('html').on('click', 'a.Link', function(event){
    event.preventDefault();
});
czjvic
  • 123
  • 10
Suresh Pattu
  • 6,083
  • 16
  • 59
  • 91
1

You can also size another element so that it covers the links (using the right z-index): That will "eat" the clicks.

(We discovered this by accident because we had an issue with suddenly inactive links due to "responsive" design causing a H2 to cover them when the browser window was mobile-sized.)

0

It's possible to do it in CSS:

.disabled{
  cursor: default;
  pointer-events: none;
  text-decoration: none;
  color: black;
}
<a href="https://www.google.com" target="_blank" class="disabled">Google</a>

See at:

Please note that the text-decoration: none; and color: black; is not needed, but it makes the link look more like plain text.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Another trick is to place a invisible element above it. This will disable any hover effects as well

.myButton{
    position: absolute;
    display: none;
}

.myButton::after{
    position: absolute;
    content: "";
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
air5
  • 140
  • 2
  • 10
0

You can try this also

<style>
.btn-disable {
  pointer-events: none !important;
    color: currentColor;
    cursor: not-allowed;
    opacity: 0.6;
    text-decoration: none;       
}
</style>
<html>
    <head>
        <title>NG</title>
    </head>
    <style>
        .btn-disable {
          pointer-events: none !important;
            color: currentColor;
            cursor: not-allowed;
            opacity: 0.6;
            text-decoration: none;       
        }
        </style>
    <body>
        <div class="btn-disable">
            <input type="button" value="Show">
        </div>
    </body>
</html>
Pramod Lawate
  • 615
  • 1
  • 7
  • 21
0

There are a bunch of answers here!

however, what I've found the most useful is just plain old css:

a[disabled] {
    pointer-events: none!important;
    cursor: not-allowed!important;
}
ddjerqq
  • 61
  • 1
  • 3
-1

pointer-events:none will disable the link:

.disabled {
    pointer-events: none;
}
<a href="#" class="disabled">link</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikki
  • 409
  • 1
  • 5
  • 15
-2

<a href="#!">1) Link With Non-directed url</a><br><br>

<a href="#!" disabled >2) Link With with disable url</a><br><br>
Rudra
  • 535
  • 4
  • 16
-2

simply use your tab without a link, don't include any link attribute to it.