如何使用弹性框让BS4导航项目用垂直居中的文本填充导航栏高度



我正在尝试使用flexbox自定义引导程序4导航栏。我希望导航项目填充导航栏div的整个高度,同时保持其内容垂直居中,3个水平链接之间的间距一致(使用前后对齐内容(。我没有成功使用下面的代码。有可能吗?

如有任何帮助,我们将不胜感激。

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<nav id="mainNav" class="navbar navbar-expand-md navbar-light bg-primary pt-0 pb-0">
<a class="navbar-brand d-none d-sm-block mr-auto my-2" href="#"><img class="img-fluid" src="https://via.placeholder.com/139x60"></a>
<div class="collapse navbar-collapse mx-5" id="navbarCollapse">
<!-- Stetch these virticall -->
<ul class="navbar-nav nav-fill w-100 align-items-start">
<li class="nav-item active">
<a class="nav-link bg-danger" href="#">Shop Now<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Why Buy From Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
<form class="form-inline mt-2 mt-md-0 mr-auto mr-sm-0 mx-md-auto">
<div class="input-group">
<input class="form-control form-control-sm" type="text" placeholder="Search" aria-label="Search">
<div class="input-group-append">
<span class="input-group-text"><i class="far fa-search"></i></span>
</div>
</div>
</form>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</nav>

似乎为了让引导导航栏链接填充导航栏的整个高度并垂直居中文本,我需要在.nav链接类中添加一个可定向的容器,以将align self:center应用于.

我使用了一个span标记,并决定使用CSS而不是内联类来保持代码的整洁,在我的情况下,它仅限于#mainNav ID。

想知道是否有一种更干净的方法可以在没有额外跨度标签的情况下做到这一点。

#mainNav,
.collapse,
.nav-link {
display: flex;
align-items: stretch;
}
#mainNav .navbar-nav {
display: flex;
justify-content: space-around;
width: 100%;
}
#mainNav .nav-item {
display: flex;
}
#mainNav .nav-link span {
align-self: center;
}
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>full height nav</title>
</head>
<body>
<div class="container">
<nav id="topNav" class="navbar navbar-expand navbar-dark text-white bg-primary justify-content-sm-end">
<ul class="navbar-nav d-flex align-items-end">
<li class="nav-item d-sm-none">
<a class="nav-link" href="#">Company Name</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Support</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Product Registration</a>
</li>
<li class="nav-item d-none d-sm-block">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</nav>
<nav id="mainNav" class="navbar navbar-expand-md navbar-light pt-0 pb-0">
<!-- Logo -->
<a class="navbar-brand d-none d-sm-block my-2 order-md-first" href="#">
<img class="img-fluid" src="https://via.placeholder.com/139x60.png?text=LOGO">
</a>
<!-- Search Bar -->
<form class="form-inline order-md-last m-0">
<div class="input-group">
<input class="form-control form-control-sm" type="text" placeholder="Search" aria-label="Search">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-search"></i></span>
</div>
</div>
</form>
<!-- Toggler -->
<button class="navbar-toggler align-self-center" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navigation -->
<div class="collapse navbar-collapse mx-5" id="navbarCollapse">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">
<span>Shop Now</span><span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span>Why Company</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span>Buying Guide</span></a>
</li>
</ul>
</div>
</nav>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>

尝试在您的品牌形象中添加类"my-0 p-0">

然后,将大约"高度:3.7em"添加到您的链接,并将"py-3"类添加到它们

相关内容

  • 没有找到相关文章

最新更新