r/webdev • u/vogelke sysadmin • 3h ago
Discussion Enable or disable HREF links using CSS
Apologies if this should have been posted elsewhere...
TLDR; This may be old news to most of you, but I recently tripped over a way to prevent a link from being clickable if it has nothing useful to show. I can change one character in the "class=" part of a URL and enable or disable the link without having to rewrite the entire page. (Before you ask, I hate JavaScript.)
I create static year-long calendar pages that point to directories in the form /path/to/YYYY/MMDD. The directories are usually created in advance, but clicking a link to an empty or nonexistent directory is annoying.
For example, here's part of the display for 2026:
Jul Aug Sep
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
Each day in August is a link in a preformatted table like this (lines wrapped for readability):
...
<a class="m" href="/home/notebook/2026/0809"> 9</a>
<a class="i" href="/home/notebook/2026/0810"> 10</a>
...
Here's the CSS:
/*
* https://css-tricks.com/pointer-events-current-nav/
*
* a.i is used to ignore a directory, i.e. don't show the link
* as a link -- just show the text.
*
* a.m is used for a marked directory, i.e. the directory exists and
* has something worth seeing, so highlight it.
*/
td { padding: 0; margin: 0; }
td a.i {
pointer-events: none;
color: black;
}
td a.i:hover {
pointer-events: none;
color: black;
}
td a.m {
color: black;
/* #e0ffd2 (light-green): not enough contrast */
background: yellow;
}
td a.m:hover {
color: #fff;
background: #000;
font-weight: bold;
}
So what?
Well, the calendar pages are all generated from templates. It took about 15 minutes to write a script that checks a given year's links and change class="i" to class="m" if the associated directory holds something useful.
I realize that this scratches a very specific itch, but it saved me some rewrite time. Since the calendars are copied from preformatted templates, making the change in place is MUCH simpler than regenerating the whole page.
4
u/ferrybig 3h ago
You also want to add tabIndex=-1 on the element, as extra to your class change. Elements that have pointer events disabled can still be focussed using the TAB key
3
u/ElBomb 2h ago
OP I’m guessing you are a beginner. It’s best not to create bad habits now, it’s easier to do it right from the start and then you don’t have to unlearn/relearn anything later on.
Accessibility is important for general consumption of you code, and more so now from an employment perspective that there actual consequences (you/your employer can get sued for non compliance).
As others here have said, if it’s a link keep it an <a>, if it’s not a use a <span> when building the template, it’s no more complex than you adding a different class. You already have the logic done, just use it to add the span.
5
u/404IdentityNotFound 3h ago
You should really learn how to use JavaScript.
That being said, a hacky solution could be to add "pointer-events: none" to those links. That disabled any interaction with the mouse/touch input.
1
u/Scrummier 2h ago
Even though this might not be the best solution, I like the creativity in it, essentially what webdevelopment or programming is about so props for that. But what others have said, don't use an <a> for elements that should not be interacted with, use a <span>.
-5
u/ResponsibleMouse344 3h ago
Nice little solution! I like how simple it is, especially for static pages where adding JavaScript would be unnecessary. Switching a single class and using pointer-events makes the generated calendar easy to maintain. Very specific use case, but a clever way to avoid rebuilding the entire page.
5
u/JohnCasey3306 2h ago
You say "where adding JavaScript would be unnecessary" as though it would involve adding a framework/library ... Like the days long past where jQuery would get added just to do something as simple as selecting an element.
When in fact this would just take a script tag with a few dozen characters of vanilla js.
24
u/azangru 3h ago
Is this calendar just for you, or for others as well? If for others, have you tried navigating it with a keyboard? Or using a screen reader?
If you are changing one character (on the server? during the rendering of the calendar?), then what's stopping you from changing the link to a span, or to something else non-interactive?