147
votes

For example if I have this:

<a style="" href="page.html">page link</a>

Is there anything I can use for the style attribute that will make it so the link isn't clickable and won't take me to page.html?

Or, is my only option to simply not wrap 'page link' in an anchor tag?

Edit: I want to state why I want to do this so that people may be able to provide better advice. I am trying to set up my application so that the developer can choose what type of navigation style they want.

So, I have a list of links and one is always currently selected and all the others aren't. For the links that are not selected, I obviously want those to be normal clickable anchor tags. But for the selected link, some people prefer that the link remains clickable while others like to make it not clickable.

Now I could easily just programmatically not wrap an anchor tags around the selected link. But I figure it will be more elegant if I can always wrap the selected link in something like:

<a id="current" href="link.html">link</a>

and then let the developer control the linking style through CSS.

10
If you don't want it to be clicked, maybe use a <span> instead? - David says reinstate Monica
I would have to agree with David. Don't break UI paradigms. The expectation of a user is that links can be clicked. If it can't be clicked then its not really a link is it? - mrtsherman
Check out stackoverflow.com/questions/2091168/disable-a-link-using-css for some more possible answers. - Oliver
Yeah, this is a terrible idea. Screen readers will still see the link as a link, because it's a link. Search engines too. In fact, anything that understands HTML. Don't do this. There's nothing "elegant" about it. - Paul D. Waite

10 Answers

311
votes

You can use this css:

.inactiveLink {
   pointer-events: none;
   cursor: default;
}

And then assign the class to your html code:

<a style="" href="page.html" class="inactiveLink">page link</a>

It makes the link not clickeable and the cursor style an arrow, not a hand as the links have.

or use this style in the html:

<a style="pointer-events: none; cursor: default;" href="page.html">page link</a>

but I suggest the first approach.

56
votes

That isn't too easy to do with CSS, as it's not a behavioral language (ie JavaScript), the only easy way would be to use a JavaScript OnClick Event on your anchor and to return it as false, this is probably the shortest code you could use for that:

<a href="page.html" onclick="return false">page link</a>
12
votes

Yes.. It is possible using css

<a class="disable-me" href="page.html">page link</a>

.disable-me {
    pointer-events: none;
}
8
votes

Or purely HTML and CSS with no events:

<div style="z-index: 1; position: absolute;">
    <a style="visibility: hidden;">Page link</a>
</div>
<a href="page.html">Page link</a>
6
votes

CSS was designed to affect presentation, not behaviour.

You could use some JavaScript.

document.links[0].onclick = function(event) {
   event.preventDefault();
};
6
votes

A more un-obtrusive way (assuming you use jQuery):

HTML:

<a id="my-link" href="page.html">page link</a>

Javascript:

$('#my-link').click(function(e)
{
    e.preventDefault();
});

The advantage of this is the clean separation between logic and presentation. If one day you decide that this link would do something else, you don't have to mess with the markup, just the JS.

2
votes

The answer is:

<a href="page.html" onclick="return false">page link</a>
0
votes

It can be done in CSS very simply. Change the "a" to a "p". Your "page link" does not lead to somewhere anyways if you want to make it unclickable.

When you tell your CSS to do a hover action on this specific "p" tell it this:

(for this example I have given the "p" the "example" ID)

#example
{
  cursor:default;
}

Now your cursor will stay the same as it does all over the page.

0
votes

As Simple you do. Put **javascript:void(0)** in href, And see the effect E.g.

<a href="javascript:void(0)">Demo</a>

No Css, No JS Required for this

-1
votes
<a href="page.html" onclick="return false" style="cursor:default;">page link</a>