views:

328

answers:

3

How can I make a link visible only when one hovers over the link?

+3  A: 

Try this:

a:link, a:visited {
    visibility: hidden;
}
a:link:hover, a:visited:hover {
    visibility: visible;
}


Edit    After piquadrat pointed out that Firefox doesn’t hover invisible elements, try this instead:

a:link span, a:visited span {
    visibility: hidden;
}
a:link:hover span, a:visited:hover span {
    visibility: visible;
}

And the corresponding HTML:

Lorem <a href="foo"><span>foo</span></a> ipsum dolor sit amet …
Gumbo
Could I set the hover option dynamically via jquery?
mrblah
@homestead, The `:hover` pseudoselector can't be 'set' in Javascript as far as I know (in a standard way, anyway). You could make a class `hover` and use that.
strager
@homestead: Sure: `$("head")[0].append('<style type="text/css"> … </style>')`
Gumbo
could I possibly change the class of the element on the fly that would make it visible upon hover? (doing it on certain elements under certain conditions, that's why I need to do it dynamically)
mrblah
@homestead: Well than add a class selector to the selector of the CSS rules.
Gumbo
gumbo, sorry I don't follow you... add a class selector to the selector?
mrblah
@homestead: Use `a.class:link, a.class:visited` instead and add your elements to that class.
Gumbo
+2  A: 

Did you mean you'd like the link to appear like normal text?

If so:

a { text-decoration: none; color: inherit; }
a:hover, a:active { text-decoration: underline; color: #00F; }
ceejayoz
+1. I too think that's what he meant!
Chetan Sastry
+2  A: 

The answers that used visibility CSS rule don't work for me, at least not in FF. The link is not visible, so when you move the mouse over it FF thinks you are hovering over whatever is behind the link.

This works for me though (even in IE6!):

a {
  zoom: 1; filter: alpha(opacity = 0); /* For IE */
  opacity: 0.0;
}
a:hover {
  zoom: 1; filter: alpha(opacity = 100); /* For IE */
  opacity: 1.0;
}
Kip
After reading `visibility` didn't work I immediately thought of using `opacity` like in your answer. +1. Note: the `zoom` and `filter` are needed only for IE6 (and below?) and not for IE7 or IE8 (as far as I know).
strager
@strager: actually zoom/filter is still needed on IE8. (I just tested it to make sure)
Kip