How to Change Link Colors with CSS

You can style links differently with CSS properties. In general, the properties used to style links are color, font-family and background-color.

There are three ways of changing the link color: inline, internal and external.

Inline method

Add the style attribute directly to the hyperlink code and specify the color property through the style attribute, then give a color value to it.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <p>Visit our <a href="https://www.w3docs.com/" style="color: #8ebf42">website</a>.</p>
  </body>
</html>

Result

Visit our website.

Internal method

Internal method requires you to use the <style> tag within the <head> section of your HTML document. In the <style> tag, we set the color of our link.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      a {
        color: #8ebf42;
      }
    </style>
  </head>
  <body>
    <p>Visit our
      <a href="https://www.w3docs.com/">website</a>.
    </p>
  </body>
</html>

There are 4 link states that links can be styled depending on what state they are in:

  • a:link - a normal, unvisited link,
  • a:visited - a link the user has visited,
  • a:hover - a link when a user mouses over it,
  • a:active - the moment a link is clicked.

When setting the style for several link states, follow these rules:

  • a:hover MUST come after a:link and a:visited,
  • a:active MUST come after a:hover.
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      /* unvisited link */
      a:link {
        color: #ccc;
      }
      /* visited link */
      a:visited {
        color: #095484;
      }
      /* mouse over link */
      a:hover {
        color: #8ebf42;
      }
      /* selected link */
      a:active {
        color: #800000;
      }
    </style>
  </head>
  <body>
    <p>Visit our
      <a href="https://www.w3docs.com/">website</a>.
    </p>
  </body>
</html>

To change the underline color, first of all, you need to remove it with the "none" value of the text-decoration property and set the "none" value, then add the border-bottom property with the width (in this case, used as a hyperlink underline width) and border-style (solid, dotted, or dashed) properties. For the anchor text color, use the color property.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      a {
        text-decoration: none;
      }
      a:link {
        color: #000;
        border-bottom: 1px solid #ff0000;
      }
      a:visited {
        color: #e600e6;
        border-bottom: 1px solid #b3b3b3;
      }
      a:hover {
        color: #2d8653;
        border-bottom: 1px solid #000099;
      }
    </style>
  </head>
  <body>
    <p>Visit our
      <a href="https://www.w3docs.com/">website</a>.
    </p>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      a.one:link {
        color: #ccc;
      }
      a.one:visited {
        color: #095484;
      }
      a.one:hover {
        color: #8ebf42;
      }
      a.two:link {
        color: #ccc;
      }
      a.two:visited {
        color: #095484;
      }
      a.two:hover {
        font-size: 150%;
      }
      a.three:link {
        color: #ccc;
      }
      a.three:visited {
        color: #095484;
      }
      a.three:hover {
        background: #8ebf42;
      }
      a.four:link {
        color: #ccc;
      }
      a.four:visited {
        color: #095484;
      }
      a.four:hover {
        font-family: monospace;
      }
      a.five:link {
        color: #095484;
        text-decoration: none;
      }
      a.five:visited {
        color: #095484;
        text-decoration: none;
      }
      a.five:hover {
        text-decoration: overline underline;
      }
    </style>
  </head>
  <body>
    <p>Mouse over the links and watch how they will be changed:</p>
    <p>
      <a class="one" href="#">This link changes color</a>
    </p>
    <p>
      <a class="two" href="#">This link changes font-size</a>
    </p>
    <p>
      <a class="three" href="#">This link changes background-color</a>
    </p>
    <p>
      <a class="four" href="#">This link changes font-family</a>
    </p>
    <p>
      <a class="five" href="#">This link changes text-decoration</a>
    </p>
  </body>
</html>

Now, we'll demonstrate another example, where we use the color property with its "inherit" value. This will make the element take the color of its parent.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p {
        color: green;
      }
      p a {
        color: inherit;
      }
    </style>
  </head>
  <body>
    <h1>Example</h1>
    <a href="https://www.w3docs.com/">W3docs.com</a>
    <p>Visit our
      <a href="https://www.w3docs.com/">W3docs.com</a> website.
    </p>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      a {
        text-decoration-color: grey;
      }
      a:link {
        color: #777777;
      }
      a:hover {
        color: #2d8653;
      }
    </style>
  </head>
  <body>
    <p>Visit our <a href="https://www.w3docs.com/">website</a>.</p>
  </body>
</html>

External method

Using external stylesheets you can take control of all the hyperlinks of your site. With external stylesheets, many attractive hyperlink effects can be created to develop the look of your website.

With the external method, you’ll link your web pages to an external .css file that can be created by any text editor in your device. This is a more efficient method, especially when you need to style a large website. You can change your whole site at once by editing one .css file.

Learn more about CSS links.


Do you find this helpful?

Related articles