For example, the following code could be placed in the head section of a webpage:
<style type="text/css"> .example { font-size: 24px; background-color: gray; text-align: center; width: 25% } .example a:link {color: white; } .example a:visited {color: pink; } .example a:hover {color: black; } /* unvisited link */ a:link { color: purple; } /* visited link */ a:visited { color: navy; } /* mouse over link */ a:hover { color: orange; } /* selected link */ a:active { color: yellow; } </style>
The .example
creates a class that can be applied to a
div
or other element on the page. For an element that has
a class of example
, the color for links will be white until
a user clicks on the link. Then it will turn to pink. If someone hovers
a mouse pointer over the link, the link color turns to black.
The code below could then be added to the web page:
<div class="example"> <a href="test.html">Inside div example<div> </div>
Someone viewing the page containing the code would see the following for a link using that class. If the person clicked on the link and the page exists and can be visited, the color for the text for the link will change to pink.
For any elements that don't have a class of example
, the
default colors for links will be controlled by the following code between
the style
tags placed in the head section of the page:
/* unvisited link */ a:link { color: purple; } /* visited link */ a:visited { color: navy; } /* mouse over link */ a:hover { color: orange; } /* selected link */ a:active { color: yellow; }
Or you can control the color of a link by placing a style element witin the anchor tag for the link as below:
<a style="color: green;" href="test2.html">Styled link</a>
Such a link would appear as shown below:
If you want some links on a page to always remain the same color, whether they are visited or unvisited, or selected, or someone moves the mouse pointer over them, you could use the following in the style section within the head section of the page or in a CSS file loaded for the page:
.example2 a {color: chocolate; }
A link to which the class example2 was applied would be displayed as follows:
The class could be applied to the above link with the following:
<a href="/network/web/html/css/creating-button.php" class="example2">
References:
Created: Friday January 2, 2015