2 回答
TA贡献1858条经验 获得超8个赞
为了防止当您将鼠标悬停在链接上时导航栏变大,请指定与 :hover 边框大小相同的透明边框底部。
.nav-link {
border-bottom: 5px solid transparent;
}
.nav-link:hover {
border-bottom: 5px solid red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar" style="border: 1px solid gray">
<ul class="navbar-nav">
<li>
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
</nav>
TA贡献1966条经验 获得超4个赞
经过一番挖掘后,我发现我需要将 h-100 添加到 ul 中。然后,这使得链接填充它的父元素,然后在 css 中使用 height:100% 的 .nav-link
完成的代码是:
<div class="container-fluid p-0">
<div class="row no-gutters shadow">
<div class="col-sm-12 col-md-12 col-lg-1">
<img src="<?php echo URLROOT.'/public/img/msplogo.jpg'; ?>" class="header-logo mx-auto d-block"/>
</div>
<div class="col-sm-12 col-md-12 col-lg-11">
<div class="row no-gutters h-50">
<div class="d-none d-lg-block col-12">
<nav class="navbar navbar-expand-lg navbar-dark bg-msp-darkblue h-100 py-0">
<ul class="navbar-nav h-100 mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
还有 CSS - 我必须在链接中添加一个 padding-top 才能将其移回中间。
/* Header Logo */
.header-logo {
width:100%;
max-width: 160px;
}
/* Small devices (landscape phones, less than 768px) */
@media (max-width: 768px) {
.header-logo {
width:100%;
max-width: 80px;
margin:10px;
}
}
/* Large viewport navbar */
.nav-link {
font-family: 'Open Sans', sans-serif;
font-size: 0.8vw;
font-weight: bolder;
text-transform: uppercase;
border-bottom: 5px solid transparent;
height:100%
padding-top:27px;
}
.nav-link:hover {
border-bottom: 5px solid #ff7240;
color:#fff!important;
}
- 2 回答
- 0 关注
- 129 浏览
添加回答
举报