What is preventing the border from being added to my <a> tag when using the :visited css rule?

I'm struggling to add a small vertical line next to a link once it has been visited.

.navigation a:visited {
  border-left: 3px solid black;
  color: purple;
}
<div class="navigation">
  <div class="content">
    <div id="w">W</div>
    <ul>
      <li><a href="index.html">hello</a></li>
      <li><a href="one.html">Two</a></li>
      <li><a href="two.html">Three</a></li>
      <li><a href="three.html">Four</a></li>
    </ul>
  </div>
</div>

I've been unsuccessful in changing the border on visited links. It seems to work fine on :hover, but not when visited. I've attempted using !important and defining default styles for a with no luck. Is this simply not achievable?

To clarify, other style changes like color are applied successfully on :hover, so the issue seems specific to the border color change.

Answer №1

MDN explains the limitations of the :visited pseudo-class:

Browsers restrict the styles that can be applied to an element selected by this pseudo-class for privacy reasons. Only certain properties like color, background-color, border-color, and others can be changed. The alpha component is also handled differently.

Even if you change the color, the getComputedStyle method will still return the value of the non-visited color.

For more details on these restrictions and why they exist, refer to Privacy and the :visited selector.

(source)

Despite these limitations, it is possible to work around them. You can experiment with techniques like the one shown in this example: https://jsfiddle.net/1mbv2peg/1/

Just keep in mind that setting the color to 'transparent' first may not produce the desired result due to how the alpha component is handled: https://jsfiddle.net/1mbv2peg/

Answer №2

As stated on http://www.w3schools.com/cssref/sel_unvisited.asp

Web browsers restrict the CSS properties that can be applied to unvisited links for security reasons.

Permitted styles include:

  • color
  • background-color
  • border-color (including individual sides)
  • outline color
  • column-rule-color
  • the color components of fill and stroke

Answer №3

Perhaps you could consider implementing jquery to apply a new class.

$("a:visited").addClass("is_visited");

It might be beneficial to enclose it within a document.ready function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to eliminate the surplus 2 download icons from this Angular code when there are a total of 3 users?

Here is some HTML code snippet: <tr *ngFor="let user of users"> <td> <h6 class="font-medium">{{user.id}}</h6> </td> <td> <h ...

Javascript onclick events failing to toggle video play/pause functionality

In my website, I have background music and a background video (webm format) embedded. I am facing an issue with making play/pause buttons in the form of png images work for both the video and the music. The background music is added using the embed tag wi ...

What is the best way to design a shape (a quadrilateral with rounded edges) using CSS?

Struggling with creating a widget in React Js that has an image as the background? The image is designed to be dynamic, changing color and size based on the device. I've attempted using inline SVG and SVG within an img tag but can't seem to contr ...

Is there a way I can align these items in the center of the span?

Issue at Hand: The SVG and text elements are not aligning to the center of my spans as desired. Attempts Made: I have experimented with different display modes, used margin properties for alignment, and tried adjusting text-align. I even searched for so ...

What is the most effective way to incorporate animated hyperlinks?

I've been grappling with a webpage I'm designing, featuring sentences acting as links arranged in inline-block format. My goal is to create an animated effect that resembles twinkling stars, pausing the animation on hover. It feels like I'm ...

Why does this image slider begin with blankness?

For my recent school project, I created an image slider. However, upon starting the page or reloading it, only the arrows and dots are displayed. View screenshot of the issue Below is the code snippet: // JavaScript function to control the slideshow sho ...

Adjusting background position using incremental changes through JavaScript

Although I have a strong background in PHP coding, using javascript is a new venture for me so I hope this question doesn't sound naive. My objective is to create a button that, when clicked, shifts the background-position of a specified DIV object b ...

Steps to remove a child node and retrieve the parent element

My jQuery skills are not the best and I could really use some help. Here's the situation: I have an HTML table structured like this : <table id="table"> <tr><td><img class="image" />test</td><td></td></tr& ...

Transforming DOM elements into Objects

Here are some values that I have: <input name="Document[0][category]" value="12" type="text"> <input name="Document[0][filename]" value="abca.png" type="text" > I am looking for a way to convert them into an object using JavaScript or jQuer ...

Is there a way to alter the color of a button once it has been clicked?

For a school project, I am working on coding a website to earn extra credit. My goal is to create a fancy design that stands out. One of the main things I am trying to achieve is having a button change color when it is clicked, similar to how it highlight ...

Retrieve a document from a specified URL using JQuery's Ajax functionality

I have an HTML page with an input type text field. I want to be able to paste a URL, for example , into this text field and then click a button labeled "download" in order to download the linked file onto my computer. I am looking to implement this funct ...

Unable to render HTML in Simple Product Name on Magento platform

Is there a way to include HTML in my basic product titles, such as: <strong>A.</strong> Product Title Name However, the frontend displays these HTML tags. Is there a method to enable parsing of all HTML tags? ...

I'm struggling to understand why the background-color is affecting the margins as well

Check out this code snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <style type="text/css"> * {border: 0px; padding: 0px;} ...

Implementing a Horizontal Navigation Menu for Categories in WordPress

Hello there, I am currently working on a WordPress website that has both parent categories and subcategories. My goal is to create a main horizontal menu with the parent category and have the subcategories appear as a submenu when hovering over the main c ...

The final item in the inline list vanishes when a new list is added

I wrote a PHP script that creates an array of images from a specific folder: <?php $imagesDir = 'thumbnails/'; $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); echo json_encode($images); ?> Th ...

How can I align two div buttons at the top and bottom to appear on the left and right sides?

How can I change the position of two buttons from top and bottom to left and right as shown in the second image? I am utilizing Floating Vue, so the buttons will be contained within a div. Is it feasible to adjust their position accordingly? Check out th ...

Can the font family of <body> be customized in Bootstrap 5?

I am facing an issue where the Font-Family changes for my heading tags, but the body tag keeps getting cancelled out by the error "_reboot.scss:50". When I inspect my webpage and toggle off the default "enter code herefont-family: var(--bs-body-font-family ...

What is the best way to upload a canvas image from a GUI to the back-end using an AJAX call and setting the content-type as "image/jpeg"?

What is the best method for saving a canvas image from GUI to back-end using an AJAX call that accepts content type "image/jpeg" and avoids the error "jquery.js: 8453 Uncaught TypeError: Illegal invocation?" HTML <canvas id="myImage"></canvas> ...

When using JQuery, data may not be displayed in another function even when using the same command

Hello, I'm new here and encountering a strange problem with two functions in my script. Let me show you the first function: $(document).on("click", "#result2 p", function(){ var inv_id = $(this).text(); $.get("autocomplete_inv.php", ...

Angular JS is encountering an issue where the promise object is failing to render correctly

I'm currently learning Angular and I have a question about fetching custom errors from a promise object in Angular JS. I can't seem to display the custom error message on my HTML page. What am I missing? Below is my HTML file - <!DOCTYPE htm ...