About Hyperlinks

Webmasters have known from the start that hyperlinks can seem complicated because they can have various configurations depending on whether they have been clicked or not, if they are being clicked, or if the cursor is over the hyperlink.

Anatomy of a Hyperlink

To handle these numerous configurations (called declarations), the CSS "pseudo-class" was developed. The syntax for a pseudo-class is as follows:

selector: pseudo-class {property: value;}

A hyperlink uses the anchor <a> tag. The anchor can have four different states: link, visited, hover, and active. (There is actually a fifth state named focus, but it is not often used.) Each state can define how a hyperlink looks at a particular time.

If you have visited many web sites, you know that links are usually easy to spot. The clue is evident because they are visually different from the surrounding text. The default is to underline the link text and make it blue. When you click on the link, the color changes to red. When you see a link to a page you have already seen, it has changed to purple. Today, many sites will change the default link colors, but they are still obviously links because they are different than the other text on the page.

If you wanted every link state to be the same, you would not have to add the pseudo-class and would style the link as follows:

css
  1. a {color: #c00000;}

If you wanted each link state to be the same except for the hover link, you would add the pseudo-class to the hover link rule. An example is as follows:

css
  1. a {color: #c00000;}
  2. a:hover {color: #0000c0;}

To set each link state differently, you add the pseudo-class to each state as follows:

css
  1. a:link {
  2. color: #c00000;
  3. text-decoration: underline;
  4. font-weight: bold;}

  5. a:visited {
  6. color: #c0c0c0;
  7. text-decoration: none;
  8. font-weight: bold;}

  9. a:hover {
  10. color: #0000c0;
  11. text-decoration: underline;
  12. font-weight: bold;}

  13. a:active {
  14. color: #c000000;
  15. text-decoration: none;
  16. font-weight: bold;}

Multiple Link Styles

In the real world of web design, you will probably have a page with different color blocks. For example, your main text area may be white, but the sidebar may be blue while the footer area may be black. Rather than trying to come up with a color scheme for links that would work well against all three different color blocks (a difficult, if not impossible, task ), we can specify link colors for each separate area using "contextual selectors".

A contextual selector is a rule that has more than one selector separated by a space. Only when the pattern matches exactly will the style apply.

For example, you want links to look a certain way in the main content area but different in the sidebar and different again in the footer:

css
  1. a {color: #c00000;
  2.  text-decoration: underline;
  3. font-weight: bold;}
css
  1. a:visited {
  2. color: #c0c0c0;
  3. text-decoration: none;
  4. font-weight: bold;}
css
  1. #sidebar a {
  2. color: #4baee9;
  3. text-decoration: none;
  4. font-weight: bold;}
css
  1. #sidebar a:hover {
  2. color: #f8c032;
  3. text-decoration: underline;
  4. font-weight: bold;}
css
  1. #footer a {
  2. color: #ff4040;
  3. text-decoration: none;
  4. font-weight: normal;}
css
  1. #footer a:hover {
  2. color: #4040ff;
  3. text-decoration: none;
  4. font-weight: normal;}

Contextual selectors are very powerful and can be used for more than just links. You can style different heading colors, text colors, and more.

Note that if you are going to use all link states, the links must be defined in a specific order: link, visited, hover, and active. An easy way to remember the order is LoVe HAte.

Just For Fun: Make a link look like a button

Learn More

css - Set link color
  1. .button a {
  2. font-size: 0.90em;
  3. color: #ffffff;
  4. padding: 5px;
  5. border-radius: 5px;
  6. background-color: #222222;
  7. text-decoration: none;
  8. font-weight: normal;
  9. border: 1px #ffffff solid;
  10. box-shadow: 0 1px 5px #666;}
css - set hover color
  1. .button a:hover {
  2. color: #ffffff;
  3. padding: 5px;
  4. background-color: #9e3400;
  5. text-decoration: none;
  6. font-weight: normal;}
html - apply the class
  1. <a href="css3.html" class="button">Learn More</a>