html - built from scratch link menu
问题描述
This is the first time I built a navigation menu from scratch and I can't get the active part to work. it should be if the home page is active it should be a different color but here that is not the case. Also, if you have any optimization ideas I'll take them.
@media only screen and (max-device-width: 1540px) {
.middle {
margin: 0 auto;
justify-content: center;
display: flex;
}
.middle a {
padding: 5px;
color: pink;
-webkit-transition: color 1.2s;
-moz-transition: color 1.2s;
-ms-transition: color 1.2s;
-o-transition: color 1.2s;
transition: color 1.2s;
}
.middle a:active {
color: pink;
}
.middle a:hover {
color: black;
}
}
<nav>
<div class="middle">
<a href="/../Time%20out%20for%20Inc/Index/index.html" class="active">Home</a>
<a href="/../Time%20out%20for%20Inc/contact/contacts.html">Contact Information</a>
<a href="/../Time%20out%20for%20Inc/event/events.html">Activities</a>
<a href="/../Time%20out%20for%20Inc/donations/donations.html">Ways You Can Help</a>
<a href="/../Time%20out%20for%20Inc/application/applications.html">Application</a>
<a href="/../Time%20out%20for%20Inc/video/videos.html">Videos</a>
<a href="/../Time%20out%20for%20Inc/photo/photo.html">Photos</a>
<a href="/../Time%20out%20for%20Inc/sponsor/sponsor.html">Sponsors</a>
</div>
</nav>
解决方案
:active
is a CSS pseudo-class which applies when an element is being activated by the user. It specifies and selects an element based on a state—the active state—and is used to apply styles to an element when it matches that state.
When we click on the link, our web page refereshes and we navigate to another page. In this case :active
will not work
You need to define active (any name) class, add styles for active link and assign it to the link of the displayed page.
For Example:
.active{
color: black;
}
If you want to asign .active class dynamically when URL changes. Check this article: https://css-tricks.com/snippets/jquery/add-active-navigation-class-based-on-url/
推荐阅读
- javascript - Apexcharts 在工具提示中未显示正确的 x 值
- php - PHP 访问本地文件系统 -> 警告:stat(): stat failed for file:///.../test.txt
- datagridview - 如何避免 datagridrow 突出显示功能在 Null 上爆炸?
- .net - Sharepoint:从 .NET 应用程序中按文件名访问文件。超过 5000 个项目的问题。需要按文件“名称”列索引吗?
- docker - docker swarm 不会将任务分配给新的工作节点
- java - Spring Controller - 显示 JSON 而不是 Whitelabel 错误页面(在浏览器中)
- git - 无法将大项目上传到 git
- repository - 如何将 Nexus 存储库从 1.7 迁移到 3.x?
- javascript - 如何使用 jsPsych 心理物理学插件中的“手动”属性调用函数来绘制刺激?
- javascript - 你如何获得它的最高数字(值)并使用 Javascript 在 Json 的 ObjectArray 中显示键 [name]?