Placed in: Home arrow Programming arrow CSS arrow Showing links while hovering using CSS
Showing links while hovering using CSS

Links (or anchor tags) are really important in webdesign/development. With all default settings (Both in CSS and the webbrowser), a link does look pretty ugly: A blue, underlined text (and purple when you visited that website). I'm sure you've seen these colours before.

Luckily, CSS helps a lot. By changing the color, :hover and :visited you can easily make your links a little bit more fancy. Janko has written an excellent post how you can improve your links even more.

Here's a simple little CSS technique that could be really useful in your next webdesign/webdevelopment process when adding links to a HTML page. Too many links on a page can be really confusing. Some links are just more important than others: They're the one that really need the desired attention. Other links (that are more common, like links to your Twitter page or RSS feed) should not be so "special".

That's exactly what we're going to do today: Hide those unimportant links and unhide (show) them while hovering its parent. We'll achieve this using CSS.

Showing links while hovering using CSS

Achieving this technique isn't really hard and is more like a "small trick". That doesn't mean that it isn't useful: With this small piece of CSS, you can add a lot more rest to your website, since not so many links get loads of attention: Only when hovering the parent tag.

Demo showing links while hovering using CSS   Download showing links while hovering using CSS

Internet Explorer 6 is a real game stopper when it comes to CSS. I'm also giving a neat little jQuery solution to make this nice technique cross-browser compatible.

HTML

The HTML is fairly easy (the trick is in the CSS). The example below shows two paragraphs: One normal, and one that'll hide the links. Both paragraphs have two links: One to my Twitter account and one to the RSS feed.

 
<h2>Normal paragraph</h2>
<p>Lorem ipsum dolor sit amet, consectetur <a href="http://feeds2.feedburner.com/marcofolio" title="Subscribe to the feed">adipiscing elit</a>. Aenean dapibus ante id sem. Aenean eros. In vulputate semper augue. Vivamus nec ante a est pharetra lacinia. Cras euismod urna at massa. Fusce ac ipsum in mi volutpat lobortis. Etiam faucibus est vel nibh. Nullam orci tortor, hendrerit et, hendrerit sed, convallis sit amet, velit. <a href="http://feeds2.feedburner.com/marcofolio" title="Subscribe to the feed">Integer augue</a> metus, [...]</p>
<h2>Colorized links on hover paragraph</h2>
<p class="colorhover">Aenean non sem vel velit posuere laoreet. <a href="http://twitter.com/marcofolio" title="Follow me on Twitter">In hac habitasse</a> platea dictumst. Suspendisse posuere volutpat leo. <a href="http://feeds2.feedburner.com/marcofolio" title="Subscribe to the feed">Donec dictum</a>, ligula sed ultricies ultrices, lectus augue tempor orci, quis rhoncus lorem turpis ut velit. Nulla facilisi. Sed orci ligula, tempor non, tristique at, tempus id, orci. [...] </p>

As you could have expect, the second paragraph where the colorhover-class is applied, hides the links untill the user is hovering the paragraph. How? With CSS!

CSS

First, some CSS to help you understand this trick.

 
p { color:#888888; }
a { color:#718374; }
a:hover { color:#EEEEEE; }
  • p - A normal paragraph has a #888888 color (grey-ish)
  • a - A normal link has a #718374 color (green-ish)
  • a:hover - When hovering a normal link, it has a #EEEEEE color (white-ish)

Now for the special CSS. Remember the colorhover-class from the HTML. I also added some comments to make the CSS a little bit more clear.

 
/* hiding the links by giving it the same colour as the text */
.colorhover a { text-decoration:none; color:#888888; }
.colorhover:hover a { color:#718374; text-decoration:underline; }
.colorhover:hover a:hover { color:#EEEEEE; }

As you can see here, the real "trick" to this, is by applying :hover to a paragraph element: It isn't limited to an <a>!

You can access all child elements (inner anchors) even with the :hover. This way, the anchors will get their color when the user is hovering it's parent. There's just one catch here.

Internet Explorer 6

As you could have expect: This CSS trick doesn't do the trick for Internet Explorer 6. Why? Because IE6 does limit the :hover functionality only to anchor tags. But with a little bit of jQuery help, we can even make this work for that old webbrowser.

Here's the JavaScript to achive the same kind of effect.

 
// CSS for anchors while hovering the parent
var csshover = { 'color' : '#718374', 'text-decoration' : 'underline' };
// CSS for anchors while not hovering the parent
var cssclear = { 'text-decoration' : 'none', 'color' : '#888888' }
$(".colorhover").hover(
   function() {
       // apply css to all anchors inside parent while hovering
      $(this).find("a").css(csshover);
   },
   function() {
       // apply css to all anchors inside parent while not hovering
      $(this).find("a").css(cssclear);
   }
);

As you can see, the jQuery/Events/hover is used. When hovering the paragraph, some CSS is applied to all anchors found inside the parent. This does work in IE6, so you'll get the same effect cross-browser!

Download and Conclusion

Yeah - I know. You could have just use jQuery to work for all browsers. I didn't want to do so, just to actually show the power of CSS. I'm also trying to open peoples eyes to just try stuff: Did you know the :hover works on a paragraph element?

Demo showing links while hovering using CSS   Download showing links while hovering using CSS

This technique can be useful when you want to add links to certain paragraphs, but don't need a lot of attention from the user. Where would you try implementing something like this?


Tags:  css technique links html jquery tutorial webdevelopment

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
Gaya   2009-04-20 20:02:35
Gravatar image Hey Marco. Good to see you've picked this trick up. There is a ugly work around to make it work in IE6 though:

http://www.xs4all.nl/~peterned/csshover.html

It works... but not perfect =(!

And about the links; I am not a fan of hiding content from your users. Not all users go over a paragraph while reading it. I for one like to put the cursor to the edge of the screen and scroll using the up and down keys on my keyboard.
My solution would be to make the links not so noticeable, just slightly different from the text. And then apply the hover effect.
That were my two cents.
Marco - Thanks Gaya!   2009-04-20 20:08:04
Gravatar image Thanks for your two cents Gaya - appreciate it ;) !

I wasn't aware of that HTC fix to apply :hover to IE6. Although I don't know the behavior tag in CSS is actually CSS valid? By putting it in the jQuery, it keeps your CSS clean.

You're totally right about the fact that "hiding links" is actually bad. But in some cases, it could be really useful. When? For example, on the bottom of many blog posts, you'll see links to the RSS/Twitter page. Those links are posted on every page and don't need the same kind of attention like the links inside the article. You can make them "pop out" using this technique when the user is hovering over them ;) .

Anyway, this was just another one of my "random thoughts" that I wanted to share: It could be really useful, but if you don't want to use it, than don't :) .

Thanks once again for your feedback mate!
Gaya   2009-04-20 20:14:09
Gravatar image Letting links at the bottom pop out is a good idea, Marco.

I've been using the :hover trick for quite a while. It's cool, and easier to implement than Javascript :)
Japes   2009-04-20 20:34:23
Gravatar image As you know, I like the idea. Regarding the jQuery solution, you could extract the css from your script by adding or removing a desired css class like this

$(this).find("a" ).addClass("hoverState" );

Keep it up yo
Marco - Darn! + Fixed   2009-04-20 20:40:27
Gravatar image Hiya Japes - Fixed your comments (crazy emoticons kicking in).

Anyway, thanks for that "light": Adding the class with JavaScript is indeed an easier way than making a variable inside the script. The "downside" of this, is that you'll be adding more lines of code to the CSS that is only for IE6:


You can't do a:
addClass(".colorhover:hover" );

So you would have a specified tag for IE6 ("hoverstate" ) that would be loaded in every browser (just 1 line of overhead, so it doesn't really matter). The jQuery only gets loaded in IE6 because of the conditional statement.

Anyway, thanks for sharing your thoughts!
Gaya - oh yeah   2009-04-20 21:22:12
Gravatar image Oh man. Grandeur people are so smart =)
Darren Taylor   2009-04-21 09:33:10
Gravatar image Why would you want to do this? Its really really bad from an accessibility point of view, I for one certainly don't hover over text in the middle of a paragraph hoping it'll be a link. Underline links in your main body always.
Marco   2009-04-21 10:25:31
Gravatar image Thanks for your comment Darren. As you can see, this is really a point that can be discussed.

Komodomedia is also using the same kind of technique, just above the comments. The text looks a lot clearer when in "normal" view, but when you hover the paragraph, it'll show the links. In my opinion, a perfect way where to use this technique.

It's just a matter of personal taste too. Feel free to use it where you want, don't overuse it and don't use it if you don't want to ;) !
Matti Leppanen   2009-04-22 22:08:04
Gravatar image This doesn't work in IE8.

However, the same functionality can be obtained quite easily with jQuery for all browsers, with less CSS and about the same amount of jQuery. :)
Here comes.

CSS:
Quote:
p { color:#888888; }
a { color:#888888; text-decoration:none;}
.hoverstate{color:#718374; text-decoration:underline;}
.linkhover {color: #EEEEEE;}

Javascript:
Quote:
$(".colorhover" ).bind("mouseenter mouseleave", function(){
$("p.colorhover a" ).toggleClass("hoverstate" );
});
$(".colorhover a" ).bind("mouseover mouseout", function(){
$(this).toggleClass("linkhover" );
});
Marco - Great!   2009-04-23 08:31:27
Gravatar image Hi Matti,

Thank you for your comment! I removed your other comment and fixed the layout of yours - Many others have problems when placing actual code in the comments (where the comments change in smileys).

I didn't test the page in IE8 since I'm a Mac user. I did try it in IE7 and "hoped" that it would work for IE8 too, since it worked on IE7. Sadly, it doesn't.

I wasn't aware of the "bind" functionality in jQuery; As you can see in the code above, I'm using the "hover" function. When using bind, you indeed make the code a little bit shorter.

Thanks for your addition; as you can see, there's always room for improvement ;) .

Greetings,,,
Gaya   2009-04-23 14:38:22
Gravatar image Why use bind() if you can use .hover() or .mouseenter() / .mouseleave()?

http://docs.jquery.com/Events
Matti Leppänen   2009-04-23 15:43:21
Gravatar image Using .bind() you save some lines of code, if using .mouseenter() / .mouseleave()/.hover() you'd have to write two calls for .toggleClass().

Atleast for me this looks cleaner and easier to read:

Quote:

$(".colorhover" ).bind("mouseenter mouseleave", function(){
$(".colorhover a" ).toggleClass("hoverstate" );
});


than this

Quote:

$(".colorhover" ).hover(function(){
$(".colorhover a" ).toggleClass("hoverstate" );
},function(){
$(".colorhover a" ).toggleClass("hoverstate" );
});
Gaya   2009-04-24 09:51:12
Gravatar image Point taken! It's just readability or just what the programmer prefers =)
Matti Leppänen   2009-04-24 15:06:35
Gravatar image And it is also about saving bandwidth. That .hover() example I wrote is 34 bytes larger than the .bind() example.

Now, that doesn't seem a lot. Let's assume you've got a site which gets ~100000 unique hits / day. Now it is already 3.4MBytes of wasted bandwidth, and 1.2GBytes per year. I know there are browser caches and other stuff which reduce the total amount, but I hope you get the picture. :)
Pradeep CD - Uniq Web Designs   2009-05-04 14:14:43
Gravatar image Great trick... thanks... :cheer:
Jon Schwab - Thank you!   2009-05-16 18:43:07
Gravatar image This is exactly what I've been looking for! Thanks.
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio