如何使 HTML 链接显示悬停样式?
我的 ASP.NET 母版页中有一些 HTML 标记,代表基本的导航菜单。三个词链接到三个页面。下面包含我的 CSS 和 HTML 供您参考。
当我加载页面时,链接会以正确的颜色(红色)显示。如果我将鼠标悬停在链接上,该链接将更改为正确的颜色(蓝色)。到目前为止,我们都很好。单击链接会将链接颜色更改为正确的颜色(黄色)。剩下的两个链接仍然如预期的那样是红色/蓝色。单击第二个链接也会将该链接更改为黄色。现在我有两个黄色链接。两个黄色链接都没有像我希望的那样显示悬停颜色(蓝色)。单击第三个链接也会使其变为黄色,并且没有任何链接显示悬停样式。
尽管已单击链接,但我希望存储颜色并显示悬停颜色。我该如何实现这个目标?这是一个 ASP.NET Web 应用程序项目,但此时我只使用直接 HTML。
/* --- css --- */
a:link
{
color: red;
text-decoration: none;
}
a:hover
{
color: blue;
text-decoration: none;
}
a:active
{
color: green;
text-decoration: none;
}
a:visited
{
color: yellow;
text-decoration: none;
}
/* --- HTML --- */
<p class="MenuItems">
<a href="1.aspx">Cars. </a>
<a href="2.aspx">Trucks. </a>
<a href="3.aspx">Vans. </a>
</p>
I have some HTML markup in my ASP.NET master page representing a basic navigation menu. THree words that link to three pages. My CSS and HTML are included below for your reference.
When I load the page, the links appear with the correct color (red). If I hover over a link, the link changes to the correct color (blue). So far, we're good. Clicking a link changes the link color to the correct color (yellow). The two remaining links are still red / blue as expected. Clicking a second link changes that link to yellow also. Now I have two yellow links. Neither yellow link displays the hover color (blue) like I'd prefer. Clicking the third link causes it to be yellow, too and none of the links display the hover style.
Although a link has been clicked, I'd like the color to be stored and have the hover color displayed. How do I accomplish this? This is an ASP.NET web application project but I'm only using straight HTML at this point.
/* --- css --- */
a:link
{
color: red;
text-decoration: none;
}
a:hover
{
color: blue;
text-decoration: none;
}
a:active
{
color: green;
text-decoration: none;
}
a:visited
{
color: yellow;
text-decoration: none;
}
/* --- HTML --- */
<p class="MenuItems">
<a href="1.aspx">Cars. </a>
<a href="2.aspx">Trucks. </a>
<a href="3.aspx">Vans. </a>
</p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如此处所述,
:hover
声明必须位于:visited
和:active
声明之后。基本上,在当前样式的级联中,您将永远不会看到
:hover
颜色。As described here, the
:hover
declaration must come AFTER the:visited
and:active
declarations.Basically, in the cascade of your current styles, you won't ever see the
:hover
color.您的
声明必须位于样式表中的声明之后,
因为当前访问的状态具有优先权。始终将鼠标悬停在样式声明块的末尾以防止出现这种情况。
a:链接-> a:访问过-> a:活动-> a:hover 是最佳排序。
Your
declaration must come after your
declaration in the stylesheet because the visited state is currently taking priority. Always put hover at the end of the a styles declaration block to prevent this.
a:link -> a:visited -> a:active -> a:hover is the optimal ordering.
只需使用这个:
或按照描述 - 使用这个顺序:
Just use this:
or as described - use this order: