简体   繁体   English

CSS 菜单悬停下划线动画

[英]CSS Menu Hover Underline Animation

I'm trying to create an underline animation when hovering on menu items (based on this CodePen ).我正在尝试在将鼠标悬停在菜单项上时创建下划线动画(基于此CodePen )。 For some reason, all my menu items are underlined instead of just one when hovering.出于某种原因,我所有的菜单项在悬停时都带有下划线,而不仅仅是一个。 I only want the menu item that is hovered to be underlined, not everything.我只希望悬停的菜单项带有下划线,而不是所有内容。

Here is my code:这是我的代码:

 html { background-color: #131313; } .pages { position: absolute; margin: 0; padding: 0; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 17px; list-style: none; } .pages li { float: left; display: inline-block; padding: 15px; } .pages a { color: white; text-decoration: none; } .pages a:after { content: ''; position: absolute; width: 0; height: 3px; display: block; margin-top: 5px; right: 0; background: #fff; transition: width .4s ease; } .pages a:hover:after { width: 100%; left: 0; background: #fff; }
 <ul class="pages"> <li><a href="#top" id="menu-home">Home</a></li> <li><a href="#about" id="menu-about">About</a></li> <li><a href="#work" id="menu-work">Work</a></li> <li><a href="#projects" id="menu-proj">Projects</a></li> <li><a href="#connect" id="menu-connect">Connect</a></li> </ul>

Please help!请帮忙!

Remove position:absolute from .pages a:after ..pages a:after删除position:absolute Elements with absolute positioning are not in the normal document flow.具有绝对定位的元素不在正常的文档流中。 In your snippet your 100% width will not be 100% of a but 100% of nearest relativly/absolutly positioned parent(body by defaul; ul in your case).在您的代码段的100%的宽度不会是100% a但100%最近relativly /是绝对定位的父的(默认情况下将主体; UL你的情况)。

As an alternative, you may set position:relative to li, so that :after element will be underlying li.作为替代方案,您可以将position:relative设置为 li,这样:after元素将成为底层 li。

 html { background-color: #131313; } .pages { position: absolute; margin: 0; padding: 0; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 17px; list-style: none; } .pages li { float: left; display: inline-block; padding: 15px; position: relative } .pages a { color: white; text-decoration: none; } .pages a:after { content: ''; position: absolute; width: 0; height: 3px; display: block; margin-top: 5px; right: 0; background: #fff; transition: width .4s ease; } .pages a:hover:after { width: 100%; left: 0; background: #fff; }
 <ul class="pages"> <li><a href="#top" id="menu-home">Home</a></li> <li><a href="#about" id="menu-about">About</a></li> <li><a href="#work" id="menu-work">Work</a></li> <li><a href="#projects" id="menu-proj">Projects</a></li> <li><a href="#connect" id="menu-connect">Connect</a></li> </ul>

My explanation may be to complicated so you may look at snippet below.我的解释可能很复杂,所以你可以看看下面的片段。 I tried to explain positioning.我试图解释定位。

 .box { width: 100px; height: 100px; } #parent { background: pink; width: 200px; height: 200px; } #child1 { position: relative; top: 10px; left: 10px; background: lightgreen; } #child2 { position: absolute; background: lightyellow; }
 <div id="parent" class='box'> <!--parent--> <!-- relative element stays relative to it's original position but can be shifted--> <div id="child1" class='box'>position:relative with "top"and"left" of 10px</div> <div id="child2" class='box'>position:absolute</div> </div> <!-- notice how yellow box is stays where it should be without "top"&"left" assigned, but it's owerlaping shifted green box -->

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

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