CSS
Web Development
HTML
Front-end Development
User Interface Design

How to disable a link using only CSS

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Disabling a link using CSS is a technique often used in web development to prevent users from clicking or interacting with the link without removing the hyperlink from the page's markup. This feature is typically used in user interfaces (UI) where certain conditions might not permit the interaction with the link. Despite the lack of direct support in CSS for disabling links, several methods can be used to emulate this behavior.

1. Pointer-Events Property

The pointer-events property defines under what circumstances a particular HTML element can become the target of mouse events. By setting it to none, you can effectively disable a link. This not only prevents the link from being clickable but also from being focusable.

css
1.disable-link {
2    pointer-events: none;
3    color: gray; /* Visually indicates the link is disabled */
4}

HTML example:

html
<a href="http://example.com" class="disable-link">Unavailable Link</a>

This method is supported by all modern browsers, but it should be noted that it does not prevent keyboard navigation accessibility unless complemented with additional CSS properties or JavaScript.

2. Modifying the href Attribute with CSS

Unfortunately, CSS itself does not allow changing an HTML element's attributes; hence, the href attribute of an anchor tag cannot be manipulated directly with CSS. To achieve a complete link disablement that reflects also on keyboard users, other solutions involving JavaScript or server-side rendering should be considered.

3. Visual Feedback

To make a disabled link obvious, styling can play an instrumental role. Here are a few visual cues:

css
1.disable-link {
2    color: gray;
3    cursor: not-allowed;
4    text-decoration: none;
5}

4. Using tabindex for Accessibility

While CSS cannot manipulate HTML attributes directly, you can add tabindex="-1" to the HTML element to skip it during tab navigation, enhancing the effect of the pointer-events style.

HTML example:

html
<a href="http://example.com" class="disable-link" tabindex="-1">No Focus Link</a>

Summary Table

PropertyUsageEffect
pointer-events: none;Prevents mouse interactionsStops all mouse events including click, hover; doesn't stop keyboard navigation
cursor: not-allowed;Changes cursor to indicate preventionSuggests that the element is not interactive
color: gray;Change text color to gray or similarVisually suggests that the element is disabled
tabindex="-1"Used in HTMLSkips element during keyboard tab navigation

Conclusion

While CSS does not provide a direct method for disabling links through the href attribute manipulation, the strategies outlined—which include using the pointer-events, styling, and tabindex attribute—serve as effective workarounds for achieving the functionality. These methods contribute to enhanced user experience and UI state management without additional scripts or altering the HTML structure significantly.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.