下划线<a>悬停时标记

2022-01-18 00:00:00 tags html css

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 过渡//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><标题></标题><style type="text/css">a.hover:hover {文本装饰:下划线;}</风格></头><身体><form id="form1" runat="server">

<a class="hover" style=" text-decoration:none; color:red" href="">站点地图</a></div></表格></身体></html>

这个:

a:hover {text-decoration: underline;}<a style=" text-decoration:none; color:red" href="">站点地图</a>

也不行.

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

解决方案

style属性更多比任何选择器都特定,所以它总是在级联中最后应用(可怕的 !important 规则无法承受).将 CSS 移至样式表.

a.hover {红色;文字装饰:无;}a.hover:悬停{文字装饰:下划线;}

(我还建议为该类命名更具语义化的名称).

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?

解决方案

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).

相关文章