Use JavaScript when a link must disappear after a click, especially if the hidden state must survive a page reload. CSS can fake the effect in a few cases, but it cannot reliably hide a clicked link based on real visit history because browsers restrict what can be styled with :visited for privacy reasons [source: MDN]. JavaScript gives you control, persistence, analytics hooks, and better rules for undoing the action later.

TLDR: If you only need a link to look different after a click, CSS may be enough. If you need the link to vanish, stay hidden, or update user progress, use JavaScript with localStorage, sessionStorage, or a server-side flag. For example, a training site with 40 lesson links might hide each completed lesson link after click; if 70% of users return the next day, JavaScript persistence prevents them from seeing already-completed items again. CSS is quick, but JavaScript is the practical choice for real interaction state.

Why hiding a link after click is trickier than it sounds

At first, this sounds like a tiny UI task. A user clicks a link. The link disappears. Done.

The catch is that browsers do not treat clicked links like regular interactive state. A link being “visited” is sensitive information. If websites could freely inspect or style visited links, they could infer parts of a user’s browsing history. That is why CSS gives you only limited styling through :visited. You can change color, but you cannot safely use it to remove a link from the page.

So the real question is not “Can I hide a link?” It is “How long should it stay hidden, and who needs to remember that click?”

a website page with a star theme online pdf tool upload files browser interface

CSS option: simple, fast, but limited

CSS is great when the effect is visual and short-lived. You can style a visited link, change its color, or make it look muted. You can also use :focus or :active for temporary effects.

a:visited {
  color: #777;
}

a:active {
  opacity: 0.4;
}

That works for subtle feedback. It tells the user, “You have clicked this before.” But it does not truly hide the link in a dependable way.

You may be tempted to try this:

a:visited {
  display: none;
}

It will not work as expected in modern browsers. Properties such as display, visibility, positioning, and layout changes are restricted for visited links. Honestly, it feels like CSS is teasing you here. The selector exists, but the useful layout tricks are off-limits.

When CSS is acceptable

  • You only need visual feedback. A clicked link can become grey, purple, or less prominent.
  • The effect does not need storage. Reloading the page can reset the experience.
  • The link should remain accessible. Users may need to click it again later.
  • You want no script dependency. This matters for very simple static pages.

CSS is also useful for progressive enhancement. You can give clicked links a calmer look while JavaScript handles the actual hiding.

JavaScript option: reliable hiding with real state

JavaScript lets you hide the link at the moment of interaction. It can also save that state. This is the big difference.

Here is the simplest version:

<a href="/report.pdf" id="downloadLink">Download report</a>

<script>
  const link = document.getElementById("downloadLink");

  link.addEventListener("click", function () {
    link.style.display = "none";
  });
</script>

This hides the link after it is clicked. But there is a problem. If the link opens a new page in the same tab, the user may never see the hiding happen. If they come back, the link is visible again.

To fix that, save the clicked state before the browser leaves the page:

<a href="/report.pdf" id="downloadLink">Download report</a>

<script>
  const link = document.getElementById("downloadLink");
  const storageKey = "downloadLinkClicked";

  if (localStorage.getItem(storageKey) === "true") {
    link.hidden = true;
  }

  link.addEventListener("click", function () {
    localStorage.setItem(storageKey, "true");
    link.hidden = true;
  });
</script>

Now the link stays hidden after refresh. It also stays hidden if the user returns later from the same browser. That is usually what people want when they ask how to hide a link once clicked.

freeCodeCamp is a website worth visiting for anyone interested in learning JavaScript.   

Choosing between localStorage, sessionStorage, cookies, and server state

The storage method depends on how serious the hidden state is.

  • sessionStorage: Hides the link only for the current tab session. Good for one-time prompts during a single visit.
  • localStorage: Keeps the link hidden across visits in the same browser. Good for tutorials, reports, onboarding, and completed steps.
  • Cookies: Useful if the server also needs to read the state, though cookie size and consent rules can make this annoying.
  • Server-side storage: Best for logged-in users. The hidden state follows the user across devices.

Expect to waste time debugging if you pick the wrong storage level. For example, hiding a “Complete profile” link with localStorage works on one laptop, then reappears on the user’s phone. If that state matters, store it on the server.

Handling multiple links without messy code

Most real pages have more than one link. You may have a resource list, a checklist, or a dashboard. In that case, use a shared class and a unique key for each link.

<a href="/guide-1" class="hide-on-click" data-hide-key="guide1">Guide 1</a>
<a href="/guide-2" class="hide-on-click" data-hide-key="guide2">Guide 2</a>

<script>
  document.querySelectorAll(".hide-on-click").forEach(function (link) {
    const key = "hiddenLink:" + link.dataset.hideKey;

    if (localStorage.getItem(key) === "true") {
      link.hidden = true;
    }

    link.addEventListener("click", function () {
      localStorage.setItem(key, "true");
      link.hidden = true;
    });
  });
</script>

This keeps the code clean. Add another link, give it a new data-hide-key, and the same script handles it.

Accessibility and user experience concerns

Hiding links can help reduce clutter. It can also confuse users. Be careful.

If a clicked link disappears instantly, users may wonder whether they made a mistake. This is worse on slow connections where a new page takes two or three seconds to open. A better pattern is to show a short status message.

link.addEventListener("click", function () {
  localStorage.setItem(storageKey, "true");
  link.hidden = true;
  document.getElementById("status").textContent = "Link completed.";
});

Also consider offering an undo action. This is useful for checklists and admin tools. People click the wrong thing. No mystery there.

  • Use clear text. “Completed” is better than silently removing the item.
  • Avoid hiding critical links. Account, billing, and support links should usually remain available.
  • Do not rely on color only. Some users will not notice the change.
  • Test keyboard behavior. A hidden link should not remain in the tab order.
a computer screen with the aspire logo on it accessibility checklist completed links web dashboard

Security and privacy limits

JavaScript can track clicks on your own page, but it should not be used to guess browsing history. CSS restrictions exist for a reason. Keep the logic tied to user actions inside your site or app.

Also, remember that browser storage is not secure proof. Users can clear it. They can edit it. They can use another browser. If the hidden state controls payments, permissions, course completion, or legal acceptance, store the event on the server.

Best practical approach

For most sites, the best setup is simple:

  1. Use CSS to show a softer style for visited or completed links.
  2. Use JavaScript to hide links after the user clicks them.
  3. Use localStorage for browser-only memory.
  4. Use server storage when the state must follow the account.

CSS is the lightweight choice for appearance. JavaScript is the dependable choice for behavior. If the link must truly disappear after interaction, JavaScript wins almost every time.

About the Author

WP Webify

WP Webify

Editorial Staff at WP Webify is a team of WordPress experts led by Peter Nilsson. Peter Nilsson is the founder of WP Webify. He is a big fan of WordPress and loves to write about WordPress.

View All Articles