html - 使用哈希或 url 片段时设置活动链接样式?
问题描述
活动链接不显示当前页面的样式吗?我正在尝试在活动页面上应用样式,但它不起作用。我正在使用 url 哈希或 url 片段。
使用单页应用程序时,您可能有:
- /mypage.html#home
- /mypage.html#products
- /mypage.html#services
- /mypage.html#about
如果链接处于活动状态,我希望所选页面下方有一个红色下划线。
a {
text-decoration: none;
}
#menubar > a {
padding: 10px;
background-color: transparent;
border: 2px solid transparent;
}
#menubar > a:hover {
cursor: pointer;
border-bottom: 2px solid #585858;
}
#menubar > a:active {
cursor: pointer;
border-bottom: 2px solid #FF0000;
}
<div id="menubar">
<a id="Home" href="#home">
<span>Home</span>
</a>
<a id="Products" href="#products">
<span>Products</span>
</a>
<a id="Services" href="#services">
<span>Services</span>
</a>
<a id="About" href="#about">
<span>About</span>
</a>
<a id="Contact" href="#contact">
<span>Contact</span>
</a>
</div>
解决方案
您可以使用:target
伪类:
:target {
cursor: pointer;
border-bottom: 2px solid #FF0000;
}
:target CSS 伪类表示一个唯一元素(目标元素),其 id 与 URL 的片段匹配。
推荐阅读
- c# - UWP 类库 NuGet 包 XamlParseException 错误
- android - 蓝牙低功耗在两部手机之间工作?
- python - 如何在命令中从我的 .py 文件运行函数?
- python - 键'PRIMARY'的重复条目'0',我不能使用auto_increment,因为值并不总是增加
- angular - Angular:绑定并与数组保持同步
- javascript - 我想制作一个脚本来在用户触摸屏幕后在 WordPress 网站上显示谷歌广告
- javascript - 反应添加 FloatingMenuButton 包解析错误:意外的令牌
- c# - 为什么 Bridge.net 不编译 HashSet
或堆栈 - javascript - 如何使用 Javascript 中的函数加密和解密字符串
- javascript - 我得到第一个参数“电子邮件”必须是一个有效的字符串错误,而firebase身份验证反应