XML technologies / CSS / CSS Cookbook / Links and navigation / Different link colors for specific parts of a page

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Navigation and body text should use different link styles.

Solution

Enclose the different page areas in div tags with different values for the attribute id according to their function:

<div id="nav">
 [...]
</div> <!-- end -->
<div id="content">
 [...]
</div> <!-- end -->

Use contextual selectors together with an ID selector and the LoVe/HAte method from the recipe Customizing link colors to define different link styles for the page areas:

/* Stildefinitionen für Navigationslinks */
#nav a:link {
 color: blue;
}
#nav a:visited {
 color: purple;
}
/* Stildefinitionen für den Haupttext */
#content a:link {
 color: white;
}
#content a:visited {
 color: yellow;
}

Discussion

ID selectors let you distinguish individual parts of a web page and give them their own styles, even when they refer to the same HTML elements. Using the LoVe/HAte method ensures that your links work as expected.

See also

The recipe ID selectors.

  

 

 

 

Discussion

ID selectors let you distinguish individual parts of a web page and give them their own styles, even when they refer to the same HTML elements. The LoVe/HAte method ensures that your links work as expected.

See also

The recipe “Different selectors for assigning styles” about ID selectors; the W3Schools tutorial on CSS pseudo-classes at http://www.w3schools.com/css/css_pseudo_classes.asp; the “Backstage” tutorial on using multiple link colors at www.metalusions.com/backstage/articles/2/.

back to top