CSS Trick: Highlight Links on Entry Hover

This :CSS: trick is pretty simple to implement, but it’s difficult to describe.  First, what I’m talking about: if you use a good browser (in other words, not Internet Explorer) when you move your mouse over this entry area/box, you should see any links in this text change appearance.  In particular, the links get slightly lighter and gain an underlining on my site.  It’s simply a subtle decorational type effect.

The reason you can do this is because the CSS 2 specification doesn’t limit the “hover” effect to only links; instead, pretty much any HTML element can have a hover effect applied to it.  (:IE:, naturally, doesn’t support this because it’s dumb.) Here is a very basic version to change how a link looks when you hover over a paragraph (<p>):

p:hover a {
   color: #77c;
   text-decoration: underline;
   }

If you just used that code by itself, though, it would affect any links you have in any paragraph on your site.  Most people wouldn’t want that, so you need to restrict it some.  Continue on to find out more details.

In my case, the weblog entries are always enclosed in an element that uses the “content” CSS class.  So, I can use that specific class to help limit how the style is applied:

.content:hover p a {
   color: #77c;
   text-decoration: underline;
   }

You can see that I moved the “hover” trigger away from the paragraph tag and onto the enclosing CSS class.  This way, the effect will occur if you hover over anywhere within the entry, not just directly over a paragraph.  When I first implemented that, I noticed that it worked well, but that any links I had in my blockquotes didn’t get affected.  So simply add a similar definition for blockquotes in addition to paragraphs:

.content:hover p a,
.content:hover blockquote a {
   color: #77c;
   text-decoration: underline;
   }

Okay, we’re almost there.  If you’ve followed along so far and actually tried it yourself, then you’ll notice something that you probably didn’t expect: while the links now look different when hovering over the entry, if you then hover over the link itself it won’t change to any regular link hover style you have defined.  So what we need to do is add in a definition that will switch back to your “regular” styling if you’re hovering over the entry but also directly hovering over a regular link.

.content:hover p a,
.content:hover blockquote a {
   color: #77c;
   text-decoration: underline;
   }

.content:hover p :link:hover,
.content:hover p :visited:hover,
.content:hover blockquote :link:hover,
.content:hover blockquote :visited:hover {
   color: #33c;
   text-decoration: none;
   }

Now, this second style definition is the tough one to explain.  The two styles in the second definition are my “regular/default” styles for links, so the definition applies those same styles if you are hovering over the enclosing CSS class ("content") and also hovering over either a regular (:link:hover) or visited (:visited:hover) link in a paragraph or a blockquote.

Whew, that’s kind of tough to explain; hopefully you understand at least the basic concept.

This is an older entry and as such, it may be by a guest author or contain formatting problems / extraneous code. If you notice something wrong with the entry, please use the Contact page to let me know the entry title and issue.

Comments

I really like your site and your tutorials. What i don’t understand is how to apply the .css class once its created to the page your using. For instance, if I am trying to get any link to display the hover effect what do I put in the actual weblog.php page and where?

There are several ways to apply :CSS: styles to your page; the most common is to link to an external stylesheet in your page HTML <head>.  It sounds like you need some information on the basics of CSS.  Try these resources:

‘No Crap’ CSS Primer
Intro to CSS

There are plenty of other introductory tutorials to CSS out there, too.  Just do some simple Google searches on [g]introductory CSS tutorial[/g] or something.

Thanks for the information. I read the first one and have it working just fine now.

Many Thanks…

Good site, what im wondering how to do is this: I am building a menu with layers that, when i roll over the block of css a layer will appear and sublinks will be in the layer. I want to apply a hover to a block link of css, but when i mouse off of the block link, i dont want it to go back to its original state. When i hover over the link, instead, I want to have it apply the css hover and only go back to normal only when i roll off the layer and off the block of css. So basically i would roll my mouse over the block of css allowing a submenu (in a layer) to appear and I want the block css to remain hilighted until i roll off the sub menu or the main block css. Can this be done with css? Have I thoroughly confused you. An example of what im talking about can be seen on the [url=http://www.michigan.org]http://www.michigan.org[/url] site under the “travel” section. I want my menu to look like that, but instead of using images as they are to rollover, i would be using css blocks.

Leave Your Comment

Comments may be edited for content or deleted at any time. Civilized discussion is welcome. Anyone spamming, going way off topic, or otherwise being a jerk will probably be deleted or banned.

User Information

pMcode is allowed for comment formatting. pop-up mini reference

Personalization Options

Comment Security