I want the hyperlinks in my page to have different colours. How do I achieve this with CSS?
Use a class selector to define two different colors, and apply the corresponding class to each hyperlink.
I have a php page with two hyperlinks. I want these two hyperlinks to have different colours. How do I this with CSS?
Easy- use a class selector to define two different classes that apply to the anchor tag, and give each a different value for the 'color' attribute. By using class selectors, you'll only apply the custom colors to anchor tags with that specific class applied to them.
An example:
CSS:
a.importantlink { color: red; }
a.notsoimportantlink { color blue; }
HTML:
<a class="importantlink" href="...">...</a>
<a class="notsoimportantlink" href="...">...</a>
When displayed in a browser, the first link will have a red color applied, and the second link will have a blue color applied.
Note that in the example, semantically-sound names are used for the CSS classes (.importantlink, .notsoimportantlink) as opposed to names that describe the actual styling (i.e. .red, .blue, etc.). This is important, as it affords flexibility to change the actual styling in the CSS file, and have the intent of the style stay accurate and helpful in the HTML source.
Editor Note: Thanks to both Brian Crescimanno and missweblash for address the orgiinal question in the comment thread, and prompting this update to the post.
Text ID: DIFFERENT_COLOURS_ON_HYPERLINKS
To add a comment, please Log in.
Visited links and more
by offwhite on March 9, 2007
a.masnav:link, a.masnav:visited, a.masnav:active {
color: #FF0000;
text-decoration: none;
}
a.masnav:hover {
color: #FF0000;
font-weight: bold;
text-decoration: underline;
}
Assign the 'masnav' class to the <a> tags you want to control with this setting. I.e. <a href="home.html" class="masnav">Test</a>
What about visited links?
by DeborahMEd on February 23, 2007
I found the information helpful to create different color hyperlinks, but how do you create different color visited links?
Thanks!
What's to get?
by sfegette on February 19, 2007
Hi, Paul-
This post was most definitely a very basic CSS question, and was actually marked to be deleted. However, the comments (as you'll read below) warranted an update, as there was some good information to be had within. In general however, no- random questions or solicitations to 'debug a page' are not the intent of CSS Advisor. The site is for posting solutions to problems, and if either the post or comments don't give insight into how to (or not to) resolve the reported problem, then no- they don't belong on the site and should be asked in a different forum. Hope this helps, you may also want to click the 'how to use this site' link on the CSS Advisor home page for more clarification as to how the site's to be used.
DONT UNDERSTAND????
by paulgagu on February 18, 2007
I just read a comment in a different post about the intention of
CSS Adviser:
-----------------------------------------------------------------------------------------
Not the right place
by LoriHC (Dreamweaver Team) on February 16, 2007
Hi KaseyJKelly -- this isn't a site for debugging individual pages. CSS adviser is for reporting browser rendering bugs once you've narrowed down their triggers (you might try the css-discuss list for help with that -- http://www.css-discuss.org/). If you discover that your page is a good example of a browser bug in action, please re-post with your findings. Thanks!
This post will be deleted shortly.
--------------------------------------------------------------------------------------
But this post seems to be like an individual problem rather than
a browser or Css issue. I am right or I dont get the intention of CSS
ADVISER ?
Thanks!
by sfegette on February 17, 2007
Great comments- I've updated the post to reflect them as of now. Thanks to both Brian and missweblash for the suggestions.
A better way...
by missweblash on February 2, 2007
The style declaration is not semantic. If you have a style called "a.bluetext" it does not relate it's purpose - and what do you think will happen if you change the colour hex of "a.bluetext" to #FF9900 at a later date?.
Instead, use something like "a.veryImportantText" and "a.notSoImportantText". That way, you can change the colour at a later date and the context is maintained without having to modify your markup.
a different angle... links in email.
by malatmals on January 28, 2007
What if we revise the question to include hyperlinks sent through email. Specifically hover and visited link colors. There should be a solution but in the CSS approaches I've taken - the CSS get stripped off by most email clients.
Elementary question
by Brian Crescimanno on January 23, 2007
I'm going to answer your question, but I will first suggest that you do some reading on CSS as even the most basic discussion of CSS will discuss how to use classes, IDs, and the cascade to create the effect you're looking for. There are many good primers out there, a simple google search should get you started.
The most basic method would be to use classes:
/* CSS */
a.redtext { color: red; }
a.bluetext{ color blue; }
HTML:
<a class="redtext" href="...">...</a>
<a class="bluetext" href="...">...</a>