强调 tag when hovering

问题描述 投票:8回答:6

我希望我的<a>标签在悬停时加下划线。我有以下代码,但它不起作用。

<%@ 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>

这个:

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

也不起作用。

我该怎么办?问题是什么?

html css html5 css3 tags
6个回答
25
投票

style属性比任何选择器都更加specific,所以它总是在级联中最后应用(可怕的!important规则不能承受)。将CSS移动到样式表。

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

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

(我还建议为该类提供更多的语义名称)。


3
投票

内联样式会覆盖页面上的样式。

删除内联样式并使用:

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

还建议你不要使用<style>,使用外部css文件。将大大方便维护。


1
投票

我认为你应该编写如下代码:(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>

0
投票

当您使用悬停时,您无法使用内联样式。你必须像这样写:

<!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
{
    text-decoration: none;
    color:red
}

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

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

-1
投票

这对你有用。

<%@ 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>ABC</title>
    <style type="text/css">
       a.hover {text-decoration: none; color: red;}
       a.hover:hover {text-decoration: underline;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
             <a class="hover" href="">Site Map</a>
        </div>
    </form>
</body>
</html>

阅读有关css规范的更多信息。


-3
投票

这可能有所帮助!

a.hover:link {text-decoration: none;}
a.hover:hover {text-decoration: underline;}
© www.soinside.com 2019 - 2024. All rights reserved.