6

Is there a way to reset visited and unvisited link colours to the browser default after they have been changed?

In my specific situation, I have a main style file containing:

a:link    { color: black; }
a:visited { color: black; }

And I would like to have a few specific links rendered with the default colours.

EDIT: Here is a jsFiddle to play with. I would like a style for the default class that makes it match the browser default.

Mangara
  • 1,116
  • 1
  • 10
  • 23

11 Answers11

3

It is different for each browser.

What you would have to do is get a stylesheet from the browser you are trying to reset (Gecko, WebKit, or Trident) and make that the new default.

Source: Browsers' default CSS for HTML elements

Community
  • 1
  • 1
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
3

Edit:

Another way is avoiding the problem from the beginning. Give the special links you want to be with the default style a special class (let's call it .default), and instead of:

a:link    { color: black; }
a:visited { color: black; }

Use the not pseudo class and write:

a:not(.default):link    { color: black; }
a:not(.default):visited { color: black; }

Notice that this pseudo class doesn't work on IE 8 and lower. For them you can use a special CSS (I don't like it, but it'll work).

Itay
  • 16,601
  • 2
  • 51
  • 72
  • Not supported in Internet Explorer **and** Opera – doitlikejustin Aug 16 '13 at 20:06
  • That sounds like exactly what I'm looking for, but it doesn't seem to work in this case (tested latest desktop Chrome and FF). – Mangara Aug 16 '13 at 20:15
  • I like the `:not` solution. Do older versions of IE just skip the rule, or apply it regardless of the `default` class? – Mangara Aug 16 '13 at 20:22
  • @Mangara : They'll skip the rule. You can make a special rule for them using a CSS hack. For further reading: [How Do I Target IE7 or IE8 Using CSS Hacks?](http://www.impressivewebs.com/ie7-ie8-css-hacks/) – Itay Aug 16 '13 at 20:37
  • 1
    In addition to not being widely supported, the `initial` keyword does *not* mean browser default. – Jukka K. Korpela Aug 16 '13 at 21:15
3

What you're looking for is revert keyword, but it's not yet implemented in most browsers, currently only Safari supports it. The links to track the development per browser are listed in the Browser compatibility section on MDN.

Some day this should work everywhere:

a { color: red; }
a.reverted { color: revert; }
<a href="#">red</a> <a class="reverted" href="#">default</a> <a href="#">red</a>

But for now think about a workaround. The feature is just not there yet.

user
  • 23,260
  • 9
  • 113
  • 101
2

If that is the only css controlling your a tags then just remove those and that will take off any styling. You could also just change the color?? Like so...

a:link {color: blue;}  
a:visited {color: purple;}
jeremy
  • 9,965
  • 4
  • 39
  • 59
jampafoo
  • 176
  • 4
1

Nowadays we can do something like this:

    <head>
        <style>
             :link    { color: black; }
             :visited { color: black; }
             .default-color :link    { color: LinkText; }
             .default-color :visited { color: VisitedText; }
            </style></head>
    <body>
        <a href='#'>link</a>,
        <span class='default-color'>
            <a href='#'>link</a></span></body>

The second link renders with default colours.

See: CSS Color Module § System Colors

Michael Allan
  • 3,731
  • 23
  • 31
  • I can't find any information on browser support for these, but once they're implemented it's a great option! – Mangara Apr 24 '22 at 17:18
0

You can only fiddle with the URL. Browsers record the URLs they've visited. If they're rendering a page, and a particular URL appears in that list, then url is colored as "visited".

You can't force a browser to treat a URL as visited, unless they've actually been there. But you CAN make a visited URL appear as "new" by adding something different to the url, so that it APPEARS new to the browser. e.g.

example.com/foo.php
example.com/foo.php?random=value

both point at the same script, but the browser will treat both as "different". If that random value changes each time, the the browser will effectively think each time it's a brand new url and color it as "new".

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

I guess one question to ask here is: why? Why would you want to do that in the first place? To my knowledge, there's no W3C standard delineating what default link colors should be, anyways. A value (such as default) for color wouldn't make sense at all, seeing as that the isn't a default value.

With that being said, the most logical way to go about this would to just style things yourself. I'm not sure what situation your in, but whatever the case is, I'm pretty sure you're doing something wrong if you're asking how to restore colors to the browser default. So, before I give you a rather dry solution, I'll ask: can you give us some context? In the case that you're making something like menu bar links and you don't want the same styling for those menu bar links to leak into your normal links, you should really be using some kind of container to select those links in.

Anyways, here comes that dry solution. Most browsers use blue for links, purple for visited links, and red for active links. So, something like the following would work for browsers that go by these colors (assuming that the user hasn't modified the browsers' styling sheet, in which case you may want to learn about that or use something like initial, examined in Itay's answer).

a:link, a     { color: blue; }
a:visited     { color: purple; }
a:active      { color: red; }
jeremy
  • 9,965
  • 4
  • 39
  • 59
0

enter code herea.class{ color:inherit; }

Specifies that the color should be inherited from the parent element.

so if your body was color:blue; then followed by a.class{color:inherit} then those examples would be blue. at the same time, you could just use a.class:link{color:blue}. and another for when you visit the link.

Your best with just customizing classes of links of special interest and leaving the rest by default.

Matthew S
  • 21
  • 3
0

No, you cannot set any CSS property to the browser default if it has been changed (i.e., if there is any style sheet being applied that assigns a value to the property. This follows from basic principles of CSS.

So consider asking a different question. There are ways to limit the effect of CSS rules to specific elements, instead of e.g. preventing all links from looking like links.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • That's not entirely true. You can use the `initial` value to reset properties to the default for that browser. It is not supported in most browsers, however. https://developer.mozilla.org/en-US/docs/Web/CSS/initial – aaronburrows Aug 16 '13 at 20:24
  • 1
    @aaronburrows, the value `initial` does not mean browser default; check the specs. – Jukka K. Korpela Aug 16 '13 at 21:37
  • Ah yes. The initial value can mean the browser default, but it all has to do with the cascade, and inheritance. D'oh! – aaronburrows Aug 20 '13 at 19:10
-1

Just style the ones you want to style by setting a class on them.

.class:link{}
.class:visited{}

Then leave the others default.

Nick
  • 14,291
  • 3
  • 17
  • 19
-1

You can use this:
a { color: inherit; } That will inherit, and as there is no other link color so the browser will give the link its own style!

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • 1
    This doesn't work for me in the latest desktop Chrome. The colour stays black. – Mangara Aug 16 '13 at 19:54
  • 1
    Then this is because of the fact that you have already changed the `a {color: #xxx}` to black. However, what type of black? I mean the purple one that is the link:visited value? – Afzaal Ahmad Zeeshan Aug 16 '13 at 19:55