html

How to Disable a Link Using Only CSS

Every website contains a lot of links on each interface that direct the user to other web pages. For example, links to visit some other website for reference while reading a blog post, visit social media accounts of a brand while visiting their websites, and download computer software, etc. But, if it is required to disable a link, the CSS pointer event property is used.

The following post will explain how the pointer event property is used in a code to disable a link in an HTML document.

Disabling a Link Using CSS

CSS library is used in combination with other languages like HTML. So, if an HTML document has a link to directly visit any other web page, the CSS pointer-events property is used to disable a link:

pointer-events: none;
cursor: default;

How to Use pointer-events Property in Code?

The CSS style statement in which we will add the pointer-events property to disable the link should refer to the class that contains the link. For example, if we have a class named “not-active” that contains the link:

<h1>Disable the Link using CSS</h1><br>
<b>Link:</b>
    <a href="https://www.google.com/" class="not-active">Click Here</a>

In the above code, the clickable link has a class “not-active” which will be used to access this HTML element.

The above code does generate the following output:

Clicking the link directs the user to the google search engine:



The pointer-event Property

  • Inside a CSS style element, write the pointer event property (pointer-event: none) while referring to the class (not-active) containing the link that should be disabled.
  • Set the cursor as any of the options like default, none, pointer, etc.
<style type="text/css">
        .not-active {
            pointer-events: none;
            cursor: default;
        }
</style>

After executing the code, there will be no change in the graphical display of the link from the outside, but when the user clicks it, it will not do anything:

This was the easiest way to disable the link in a code using CSS statements.

Conclusion

A link that directs the user to other web pages can easily be disabled through a simple CSS “pointer-events: none” property. This does not require any changes to the logic of the code or the class in which the link has been created. A simple pointer event property in the style element referring to the class containing the link is required.

About the author

Hadia Atiq

A Software Engineer and Technical Writer passionate about learning and spreading knowledge of the latest technology. I utilize my writing skills to help readers understand the importance and usage of modern technology.