Ryan
Ryan

Reputation: 6045

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

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.

Upvotes: 176

Views: 313143

Answers (12)

John Mark
John Mark

Reputation: 65

Yes.. It is possible using css

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

.disable-link {
   pointer-events: none;
   cursor: default;
}

Upvotes: 0

esenkaya
esenkaya

Reputation: 398

Questioner specifically asks for CSS solution but according to answers more lines needs to be added just to keep href='page.html'. To me correct approach should be adding onclick HTML attribute. Here is the solution;

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

This way href link can be kept in coding for future usage.

Upvotes: 1

OSS Developers
OSS Developers

Reputation: 29

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

Upvotes: 1

Hollnder
Hollnder

Reputation: 1

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.

Upvotes: -1

Diego Unanue
Diego Unanue

Reputation: 6826

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.

Upvotes: 365

Karan K
Karan K

Reputation: 715

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>

Upvotes: 64

pown
pown

Reputation: 1138

Yes.. It is possible using css

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

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

Upvotes: 12

John
John

Reputation: 887

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

Upvotes: -1

Paul
Paul

Reputation: 141839

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>

Upvotes: 8

pixelfreak
pixelfreak

Reputation: 17834

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.

Upvotes: 6

alex
alex

Reputation: 490163

CSS was designed to affect presentation, not behaviour.

You could use some JavaScript.

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

Upvotes: 6

Doa
Doa

Reputation: 2005

The answer is:

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

Upvotes: 3

Related Questions