简体   繁体   English

如何在悬停时在导航中获得此边框底部?

[英]How can I get this border bottom in nav on hover?

I'm trying to achieve this border bottom on hover.我正在尝试在悬停时实现此边框底部。 在此处输入图片说明

What I have so far is this:到目前为止,我所拥有的是:

And here's the code for what I have so far:这是我到目前为止所拥有的代码:

.navbar-nav li > a:after {
margin-top: .4rem;
content: '';
display: block;
width: 0;
height: 2px;
background: var(--yellow);
transition: width .3s;
}
.navbar-nav li > a:hover::after {
width: 100%;
}

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

You could use the ::before and ::after pseudo elements to create and position those two dots at the end of the lines:您可以使用::before::after伪元素在行的末尾创建和定位这两个点:

Update: Now using an additional ::before on the <li> element instead of text-underline-offset on the <a> , as Browsers seem to calculate the underline differently, resulting in misalignment of the line and the dots.更新:现在在<li>元素上使用额外的::before而不是<a>上的text-underline-offset ,因为浏览器似乎以不同的方式计算下划线,导致线条和点未对齐。

 body { background: #333 } nav { background: #eee; padding: 1em 0; } nav ul li { list-style-type: none; display: inline-block; position: relative; box-sizing: content-box; margin: 0 1em; } nav li a { font-weight: bold; font-family: sans-serif; text-transform: uppercase; text-decoration: none; position: relative; display: inline-block; } nav li:hover::before { content: ""; display: block; width: 100%; border-top: solid orange 2px; position: absolute; bottom: -7px; box-sizing: content-box; } nav li a:hover::before, nav li a:hover::after { content: ""; display: block; width: 4px; height: 4px; background: orange; border-radius: 100%; position: absolute; bottom: -8px; } nav li a:hover:before { left: -3px; } nav li a:hover:after { right: -3px; }
 <nav> <ul> <li> <a href="#">Lorem</a> </li> <li> <a href="#">Ipsum</a> </li> <li> <a href="#">Dolor</a> </li> </ul> </nav>

You can mix-up pseudo element and radial-gradient您可以混合伪元素和radial-gradient

 body { background: #333 } nav { background: #eee; padding: 1em 0; } nav ul li { list-style-type: none; display: inline-block; padding: 0 1em; } nav li a { font-weight: bold; font-family: sans-serif; text-transform: uppercase; text-decoration: none; position: relative; } nav li a:before { content: ""; width: 100%; height: 4px; background: radial-gradient(circle at 4%, red, red 4%, transparent 4%, transparent 75%), radial-gradient(circle at 96%, red, red 4%, transparent 4%, transparent 75%); position: absolute; bottom: -8px; opacity: 0; transition: opacity 300ms; } nav li a:after { content: ""; left: 0; width: 100%; height: 2px; background: red; position: absolute; bottom: -7px; opacity: 0; transition: opacity 300ms; } nav li a:hover:before, nav li a:hover:after { opacity: 1; }
 <nav> <ul> <li> <a href="#">Lorem</a> </li> <li> <a href="#">Ipsum</a> </li> <li> <a href="#">Dolor</a> </li> </ul> </nav>

Extend the background image of the pseudo element by adding a radial gradient to the left and to the right:通过向左和向右添加径向渐变来扩展伪元素的背景图像:

 body { --yellow: gold; /* set to gold just so easier to see in the demo */ } .navbar-nav ul{ list-style: none; } .navbar-nav li > a { display: inline-block; width: auto; } .navbar-nav li > a:after { margin-top: .4rem; content: ''; display: block; width: 0; height: 4px; background-image: radial-gradient(circle at left, var(--yellow) 0%, var(--yellow) 4px, transparent 4px), radial-gradient(circle at right, var(--yellow) 0%, var(--yellow) 4px, transparent 4px), linear-gradient(transparent 1px, var(--yellow) 1px, var(--yellow) 3px, transparent 3px, transparent); background-repeat: no-repeat no-repeat; transition: width .3s; } .navbar-nav li > a:hover::after { display: inline-block; width: 100%; }
 <div class="navbar-nav"> <ul> <li><a hre="">Home</a></li> </ul> </div>

The things that are at the beginning of the background image definition get drawn on top of the things that come after.背景图像定义开头的内容被绘制在后面的内容之上。

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

相关问题 Ant 设计:如何禁用 hover 上菜单项的边框底部? - Ant Design: How can I disable border-bottom of a menu item on hover? 如何修复 textarea 边框底部 css 效果? - How can I fix the textarea border bottom css effect? 如何删除网格行的边框底线? - How can I remove the border bottom lines of grid rows? 如何在不使用伪状态的情况下在悬停时为现有边框底部上的边框底部设置动画 - How to animate a border-bottom over an existing border-bottom on hover without using pseudo-states 如何在移动视图中将突出显示的导航项更改为蓝色背景而不是蓝色边框底部 - How do I change the highlighted nav item to have a blue background instead of a blue border-bottom in mobile view 悬停/鼠标悬停时如何从 TextField 中删除边框底部? - How to remove border-bottom from TextField when hover/mouseOver? 当我在菜单文本上 hover 时,如何让 Sub Nav Dropdown 仅显示? - How to get Sub Nav Dropdown to only display when I hover over the menu text? 如何修复多级侧导航菜单鼠标 hover 问题? - How can i fix Multi-level Side Nav Menu mouse hover issue? 将边框悬停在边框上方时,如何使边框变大? - How do I make a border-box get bigger when I hover over something on top of it? 单击时如何保持 href 的边框底部颜色? - How can I maintain the border-bottom color of a href when I click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM