{"id":4127,"date":"2017-03-02T07:37:08","date_gmt":"2017-03-02T12:37:08","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=4127"},"modified":"2021-12-08T13:46:22","modified_gmt":"2021-12-08T18:46:22","slug":"disable-a-link-using-css-no-javascript","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/disable-a-link-using-css-no-javascript\/","title":{"rendered":"How to Disable a Link in HTML Using CSS and no JavaScript"},"content":{"rendered":"\n
Like all things web development projects there is an easy way to do things and there is a complicated way to achieve the same result. This is no exception when you want to disable a link in HTML<\/a>.<\/p>\n\n\n\n Sure you could definitely use some JavaScript<\/a> or jQuery and these form some elegant functionality. But what if you want a simple solution with little fuss and overhead? Enter CSS<\/a>. Of course, there is always a gotcha when it comes to programming<\/a> and again there is no exception here, that brings us to the old trusty Internet Explorer. We will come to that later though.<\/p>\n\n\n\n Sure you can disable a link in HTML only using the You can use JavaScript or even take advantage of jQuery but these solutions require a little bit more thought and code even though in many cases they could be the best solution to the problem at hand. Using JavaScript you could intercept the clicks, you could use fake click handlers or sorts.<\/p>\n\n\n\n A CSS-only solution can be ideal where you are targeting active links in certain media types while not in others. An example would be when you need a phone link active on mobile devices but not on say a Desktop computer.<\/p>\n\n\n\n With that said let us dive right in and see the CSS solution. You just need a single CSS selector set on an HTML element, class, or ID to achieve what you need. This takes care of the technical aspects of this solution. We then add two more selectors to help out the human factor in what you are trying to do. The solution involves the following:<\/p>\n\n\n\n The code below illustrates the above in use.<\/p>\n\n\n\n The above solution works in most modern web browsers. For those still lingering in the IE world, you will need to put in a few extra minutes to accommodate that web browser’s needs. In IE the above solution will not work. The workaround is to leverage the disabled attribute of the “a”<\/strong> element. All other browsers will ignore it since the disabled attribute is not standard in HTML but it is according to IE. The example below shows what you need to do for IE.<\/p>\n\n\n\n There you have it. You can now comfortably disable links and build on the method to suit your needs as appropriate.<\/p>\n","protected":false},"excerpt":{"rendered":" Like all things web development projects there is an easy way to do things and there is a complicated way to achieve the same result. This is no exception when…<\/p>\n","protected":false},"author":1,"featured_media":4128,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,17],"tags":[106,170,212,303,320,328,523,543,635,638],"yoast_head":"\nonclick<\/code> event with a value set to “return false”. That works but like everything HTML it is not very portable.<\/p>\n\n\n\n
How to Disable a Link in CSS<\/h2>\n\n\n\n
.disabled-link {\n pointer-events: none;\n cursor: default; \n opacity: 0.5;\n} <\/code><\/pre>\n\n\n\n
<a href="#" class="disabled-link">This link is disabled<\/a><\/code><\/pre>\n\n\n\n
What About IE?<\/h2>\n\n\n\n
a[disabled] {\n pointer-events: none;\n }<\/code><\/pre>\n\n\n\n