Is it possible to make an HTML anchor tag not clickable/linkable using CSS?

HtmlCssAnchor

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

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>

Solution 3 - Html

Yes.. It is possible using css

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

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

Solution 4 - Html

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>

Solution 5 - Html

CSS was designed to affect presentation, not behaviour.

You could use some JavaScript.

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

Solution 6 - Html

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.

Solution 7 - Html

The answer is:

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

Solution 8 - Html

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

Solution 9 - Html

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

Solution 10 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - HtmlDiego UnanueView Answer on Stackoverflow
Solution 2 - HtmlKaran KView Answer on Stackoverflow
Solution 3 - HtmlpownView Answer on Stackoverflow
Solution 4 - HtmlPaulView Answer on Stackoverflow
Solution 5 - HtmlalexView Answer on Stackoverflow
Solution 6 - HtmlpixelfreakView Answer on Stackoverflow
Solution 7 - HtmlDoaView Answer on Stackoverflow
Solution 8 - HtmlOSS DevelopersView Answer on Stackoverflow
Solution 9 - HtmlJohnView Answer on Stackoverflow
Solution 10 - HtmlHollnderView Answer on Stackoverflow