102

The first example didn't work. I need to have always a list to disable links? Or what is wrong with my first demo?

<a class="disabled" href="#">Disabled link</a>

<ul class="nav nav-pills">
  ...
  <li role="presentation" class="disabled"><a href="#">Disabled link</a></li>
  ...
</ul>

https://jsfiddle.net/7y0u2amy/

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
user2990084
  • 2,699
  • 9
  • 31
  • 46

10 Answers10

154

I think you need the btn class.
It would be like this:

<a class="btn disabled" href="#">Disabled link</a>
Caampa
  • 1,547
  • 1
  • 8
  • 3
82

It seems that Bootstrap doesn't support disabled links. Instead of trying to add a Bootstrap class, you could add a class by your own and add some styling to it, just like this:

a.disabled {
  /* Make the disabled links grayish*/
  color: gray;
  /* And disable the pointer events */
  pointer-events: none;
}
<!-- Make the disabled links unfocusable as well -->
<a href="#" class="disabled" tabindex="-1">Link to disable</a><br/>
<a href="#">Non-disabled Link</a>
Emanuela Colta
  • 2,129
  • 4
  • 18
  • 31
9

I just created my own version using CSS. As I need to disabled, then when document is ready use jQuery to make active. So that way a user cannot click on a button until after the document is ready. So i can substitute with AJAX instead. The way I came up with, was to add a class to the anchor tag itself and remove the class when document is ready. Could re-purpose this for your needs.

CSS:

a.disabled{
    pointer-events: none;
    cursor: default;
}

HTML:

<a class="btn btn-info disabled">Link Text</a>

JS:

$(function(){
    $('a.disabled').on('click',function(event){
        event.preventDefault();
    }).removeClass('disabled');
});
Pedro Sanção
  • 1,328
  • 1
  • 11
  • 16
Mike
  • 1,525
  • 1
  • 14
  • 11
6

If what you're trying to do is disable an a link, there is no option to do this. I think you can find an answer that will work for you in this question here.

One option here is to use

<a href="/" onclick="return false;">123n</a>

Disabled href tag

Community
  • 1
  • 1
Cassandra
  • 176
  • 2
  • 11
4

You cant set links to "disabled" just system elements like input, textfield etc.

But you can disable links with jQuery/JavaScript

$('.disabled').click(function(e){
    e.preventDefault();
});

Just wrap the above code in whatever event you want to disable the links.

HelloWorld
  • 2,480
  • 3
  • 28
  • 45
1

I just removed 'href' attribute from that anchor tag which I want to disable

$('#idOfAnchorTag').removeAttr('href');

$('#idOfAnchorTag').attr('class', $('#idOfAnchorTag').attr('class')+ ' disabled');
Jsowa
  • 9,104
  • 5
  • 56
  • 60
asifaftab87
  • 1,315
  • 24
  • 28
1

Thanks for @jacob-van-lingen's comment, You can extend .btn-link in your global style

a.disabled{
  @extend .btn-link
}
Fangxing
  • 5,716
  • 2
  • 49
  • 53
0

I developed the following solution because when I apply class styles such as btn disabled offered by Bootstrap 5 to an <a> element inside a card, margin and padding are applied to the element:

enter image description here

.disabled {
    color: currentColor;
    cursor: not-allowed;
    opacity: 0.5;
    text-decoration: none;
}
<a href="#" class="disabled">Disabled Link</a>
Sercan
  • 4,739
  • 3
  • 17
  • 36
0

I liked the answer by Sercan and I added a tiny jQuery to it, so that the links are also not followed on click:

    $(document)
        .on("click",
            "a.disabled",
            function () {
                return false;
            });

and for the look from the Answer above:

.disabled {
   color: currentColor;
   cursor: not-allowed;
   opacity: 0.5;
   text-decoration: none;
}
Tillito
  • 7,718
  • 7
  • 34
  • 31
0

Use class="pe-none".
Find the bootstrap link here.

<a class="btn btn-outline-primary pe-none" target="_blank" href="#" role="button">Open</a>
Bessa
  • 1
  • 3