268

I'm creating a navigation menu with words with different colors (href links). I would like the color NOT to change on any state (hover, visited etc).

I know how to set the the colors for the different states, but I would like to know the code to just leave the text color (and any other styling/formatting) as it is.

Any Suggestions?

5 Answers 5

434

You can simply define a style for links, which would override a:hover, a:visited etc.:

a {
  color: blue;
  text-decoration: none; /* no underline */
}

You can also use the inherit value if you want to use attributes from parent styles instead:

body {
  color: blue;
}
a {
  color: inherit; /* blue colors for links too */
  text-decoration: inherit; /* no underline */
}
5
  • 7
    The important part is the inherit keyword. It doesn't have 100% support unfortunately.
    – david
    Commented Jan 19, 2012 at 0:59
  • 2
    initial is also helpful when you want to css reset to the default style. This is well explained here link Commented Aug 29, 2013 at 16:36
  • @david, Does it still stand? Does inherit not work only in anciet browsers and Internet Explorer or there are difficulties in popular browsers (Chrome, Firefox) too?
    – parsecer
    Commented Oct 16, 2016 at 1:10
  • 1
    Check out unset as well. >> The unset CSS keyword resets a property to its inherited value if it inherits from its parent, and to its initial value if not. In other words, it behaves like the inherit keyword in the first case, and like the initial keyword in the second case. Commented Feb 28, 2019 at 19:51
  • The problem with resetting pseudo classes is that the resets are difficult to override later because of the specificity of a pseudo class selector. Commented Mar 29, 2024 at 8:12
47

The property-value pair:

a {
    all: unset;
}

seems cleaner in my opinion and has the advantage to work with all the selectors, e.g.:

a, button /* &c... */ {
    all: unset;
}

Note that this will also remove the property {cursor: pointer;} from your link. It might or might not be what you are looking for. Simply add this property in the second case.

To learn more about the all shorthand, checkout this page: https://developer.mozilla.org/en-US/docs/Web/CSS/all
As Emi said, don't forget to check for compatibility: https://caniuse.com/css-all

3
  • Genius! This one works for underline and link color on visited...etc. The best answer IMO
    – TOPKAT
    Commented Mar 23, 2022 at 8:51
  • 1
    I'd recommend checking browser compatibility for that approach: caniuse.com/css-all
    – Emi
    Commented Aug 29, 2022 at 11:41
  • 1
    But this will remove the special mouse hover icon. And it will leave the hover style. Commented Apr 14, 2023 at 14:19
6

As Chris said before me, just an a should override. For example:

a { color:red; }
a:hover { color:blue; }
.nav a { color:green; }

In this instance the .nav a would always be green, the :hover wouldn't apply to it.

If there's some other rule affecting it, you COULD use !important, but you shouldn't. It's a bad habit to fall into.

.nav a { color:green !important; } /*I'm a bad person and shouldn't use !important */

Then it'll always be green, irrelevant of any other rule.

4
  • This isn't what he asked. Re-read the question. He already knows this.
    – david
    Commented Jan 19, 2012 at 1:01
  • 1
    @david I have re-read, pretty sure that's what he asked. This code will allow him to set a style for a which will prevent any changes on :hover (or :visited etc). How about instead of being a dick and rubbishing all three answers you suggest an actual solution?
    – SpoonNZ
    Commented Jan 19, 2012 at 19:44
  • Thanks for the help. But what if the link is like this. 2 words in 2 colors, but it's just 1 link: unique(in pink) sales(in black) And they should stay that color in any state. I coded it inline this way... <a href="#" style="text-decoration: none" target="_blank"><span style="font-family: Arial;font-size: 13px;color: #e91974;font-weight: lighter;">unique</span> <span style="font-family: Arial;font-size: 13px;color: #020202;font-weight: lighter;">sales</span></a> But there must be a cleaner. (there where many links like this in the code) Any suggestions? Commented Mar 7, 2012 at 19:31
  • I guess that's why classes were invented. @Rbijker.com Commented Sep 30, 2022 at 10:11
-1

if you state a.redLink{color:red;} then to keep this on hover and such add a.redLink:hover{color:red;} This will make sure no other hover states will change the color of your links

-1

You can just use an a selector in your stylesheet to define all states of an anchor/hyperlink. For example:

a {
    color: blue;
}

Would override all link styles and make all the states the colour blue.

1
  • 1
    This isn't what he asked. Re-read the question. He already knows this.
    – david
    Commented Jan 19, 2012 at 1:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.