我有一个导航栏,当悬停列表中的任何项目时,我在底部添加了一条红线,但我想将该红线移到标题下方(例如“服务”),知道如何实现此目的?
我在 codepen 中添加了一个小示例,以便您可以轻松检查 HTML 和 CSS 代码
header {
background-color: lightblue;
padding-top: 1rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li:hover {
height: 100%;
border-bottom: 2px solid red;
}
<header>
<a href="/">
<p>Whatever logo</p>
</a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
<a href="/">CONTACT</a>
</header>
查看代码的链接
我认为只需为所有列表元素提供与标题相同的高度即可。
像这样:-
header { background-color: lightblue; padding-top: 1rem; height: 3rem; position: sticky; top: 0; display: flex; align-items: center; justify-content: space-around; } header nav { min-width: 50%; height : 100%; } header nav ul { margin: 0; height: 100%; list-style: none; padding-left: 0; display: flex; align-items: center; justify-content: space-between; } header li{ height: inherit; } header li:hover { border-bottom: 2px solid red; }Whatever logo
CONTACT您可以修复标题高度,也可以修复导航栏项目的高度。 另外,您还遇到一个问题,即悬停时 li 元素会移动。您还可以通过始终向元素添加透明颜色的边框来解决此问题,这样元素的整体高度在悬停状态下不会改变。
这是固定的CSS
header { background-color: lightblue; position: sticky; display: flex; height: 60px; align-items: center; justify-content: space-around; } header nav { min-width: 50%; } header nav ul { margin: 0; height: 100%; list-style: none; padding-left: 0; display: flex; align-items: center; justify-content: space-between; height: 60px; } header li { display: flex; align-items: center; border-bottom: 2px solid transparent; height: 60px; } header li:hover { border-bottom: 2px solid red; }https://codepen.io/swarajgk/pen/JjZewPo?editors=1100