Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underline <a> tag when hovering

Tags:

html

css

tags

I want my <a> tag gets underlined when hovered. I have the following code, but it doesn't work.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">

     a.hover:hover {text-decoration: underline;}
     </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a class="hover" style=" text-decoration:none; color:red" href="">Site Map</a>

    </div>
    </form>
</body>
</html>

This:

a:hover {text-decoration: underline;}
<a  style=" text-decoration:none; color:red" href="">Site Map</a>

doesn't work either.

What should I do? What is the problem?

like image 737
jal nami Avatar asked May 30 '13 14:05

jal nami


People also ask

How do you underline a tag?

To underline a text in HTML, use the <u> tag. The <u> tag deprecated in HTML, but then re-introduced in HTML5. Now it represents a text different from another text stylistically, such as a misspelled word.


3 Answers

The style attribute is more specific than any selector, so it will always be applied last in the cascade (horrible !important rules not withstanding). Move the CSS to the stylesheet.

a.hover {
    color: red;
    text-decoration: none;
}

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

(I also suggest a more semantic name for the class).

like image 151
Quentin Avatar answered Nov 02 '22 18:11

Quentin


The inline style overrides the style on the page.

Remove the inline style and uses this:

<style type="text/css">
    a {text-decoration: none;}
    a:hover {text-decoration: underline;}
</style>

Also advise you not to use <style>, uses an external css file. Will greatly facilitate maintenance.

like image 26
Shankar Cabus Avatar answered Nov 02 '22 18:11

Shankar Cabus


I think that you should write code like: (Demo here: http://jsfiddle.net/BH7YH/)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
           a {text-decoration: none; color:red}
           a:hover {text-decoration: underline;}
     </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a class="hover" href="">Site Map</a>

    </div>
    </form>
</body>
</html>
like image 20
Nguyen Phung Hung Avatar answered Nov 02 '22 18:11

Nguyen Phung Hung