How to use 'hover' in CSS

omg picture omg · May 25, 2009 · Viewed 132.9k times · Source

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?

Answer

David says reinstate Monica picture David says reinstate Monica · May 25, 2009

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.