简体   繁体   English

CSS 菜单下划线 Animation

[英]CSS Menu Underline Animation

I'm trying to follow this tutorial on codepen to make an animated underline when a user hovers over a link in the navbar.当用户将鼠标悬停在导航栏中的链接上时,我正在尝试按照 codepen 上的教程制作动画下划线。 Currently, I have the line appearing but only shows one underline underneath the whole nav list.目前,我有这条线出现,但只在整个导航列表下方显示一个下划线。 I am trying to achieve the line appearing underneath the hovered link.我正在尝试实现出现在悬停链接下方的线条。

 .navbar-fixed-left.navList a.link { text-decoration: none; } /*Removing bullet points */.navbar-fixed-left.navList li { list-style-type: none; }.link::before { transition: 300ms; height: 2px; content: ""; position: absolute; background-color: #031D44; }.link-ltr::before { width: 0%; bottom: 10px; } /* Length of the line */.link-ltr:hover::before { width: 100%; }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="navbar navbar-fixed-left mt-4"> <ul class="navList"> <li><a class="link link-ltr" href="about.html">About</a></li> <li><a class="link link-ltr" href="resume.html">Resume</a></li> <li><a class="link link-ltr" href="projects.html">Projects</a></li> <li><a class="link link-ltr" href="databasediagram.html">Database Diagrams</a></li> <li><a class="link link-ltr" href="apiunittests.html">API Unit Tests</a></li> <li><a class="link link-ltr" href="bucketlist.html#">Bucket List</a></li> </ul> </div>

Any help would be appreciated thank you!任何帮助将不胜感激,谢谢!

you need to add a relative position to .link so that the underline will be relative to the link's position.您需要将相对的 position 添加到.link ,以便下划线相对于链接的 position。 and then just set the top or bottom position of the underline to make it appear on the bottom of the link.然后只需设置下划线的顶部或底部 position 使其出现在链接的底部。

.link { position: relative }

You must just add this element in CSS:您必须在 CSS 中添加此元素:

.link {
  padding: 20px 0px;
  display: inline-block;
  position: relative;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM