tags:

views:

1273

answers:

6

I used the code below in order to fulfill this target:

When mouse hover on the anchor, the underline comes out,

but it failed to work,

    <a class="hover">click</a>

    a .hover :hover{
        text-decoration:underline;
    }

What's the right version to do this?

+4  A: 

a.hover:hover

Ballsacian1
+5  A: 

If you want the underline to be present while the mouse hovers over the link, then:

a:hover {text-decoration: underline; }

is sufficient, however you can also use a class-name of 'hover' if you wish, and the following would be equally applicable:

a.hover:hover {text-decoration: underline; }

Incidentally it may be worth pointing out that the class name of 'hover' doesn't really add anything to the element, as the psuedo-element of a:hover does the same thing as that of a.hover:hover. Unless it's just a demonstration of using a class-name.

David Thomas
Thanks ricebowl, but is there any way to make it work for anchors without href attribute?
Shore
In IE I mean,it works in firefox,but not in IE for anchors without href attribute.
Shore
I'm afraid not, IE (certainly as IE6, and maybe IE7 -I can't remember) is limited in its use of :hover, so that only <a> tags are able to use it. IE8 may have improved this, but I'm not entirely sure.
David Thomas
IE7+ supports the :hover pseudo-class on arbitrary Elements.
ajm
So does FF, Opera, Chrome!
bobobobo
+2  A: 
a.hover:hover {
    text-decoration:underline;
}
chaos
seems it's not working in IE?
Shore
+1  A: 

You need to concatenate the selector and pseudo selector. You'll also need a style element to contain your styles. Most people use an external stylesheet, for lots of benefits (caching for one).

<a class="hover">click</a>

<style type="text/css">

    a.hover:hover {
        text-decoration: underline;

    }

</style>

Just a note: the hover class is not necessary, unless you are defining only certain links to have this behavior (which may be the case)

alex
Will this work in IE?
Shore
@Shore, Yes it will work in IE
alex
but it's not working in IE8,maybe I'm using a class name with '-' in it?say a.hover-link:hover ?
Shore
hyphens (-) are fine in class names in CSS. For your selector above to work, your element must look like <a href="" class="hover-link">click</a> and then mouseover that link.
alex
Thanks alex,but is there any way to make it work for anchors without href attribute?
Shore
It should work with anchors without a href attribute. May I ask why you omit it though?
alex
Because if href is added,underline will come out even when cursor not on,I don't know the reason yet..
Shore
You could possibly have a a[href=] selector, but I don't think that would be the case given that you described your CSS skills as 'noob' (no offence).
alex
It's fine,you are very frank,I'm indeed a CSS noob -,-
Shore
Don't worry, you'll get there! Everyone does :)
alex
A: 

You should have used:

a:hover {
    text-decoration: underline;
}

hover is a pseudo class in CSS. No need to assign a class.

Alan Haggai Alavi
It may be that he only wants <a>s that he puts his hover class on to have this behavior.
chaos
Yes, I missed that. Thanks, chaos.
Alan Haggai Alavi
A: 

The href is a required attribute of an anchor element, so without it, you cannot expect all browsers to handle it equally. Anyway, I read somewhere in a comment that you only want the link to be underlined when hovering, and not otherwise. You can use the following to achieve this, and it will only apply to links with the hover-class:

<a class="hover" href="">click</a>

a.hover {
    text-decoration: none;
}

a.hover:hover {
    text-decoration:underline;
}
PatrikAkerstrand