如何在Bootstrap 4导航栏导航项中创建小文本对齐的中心和下方图标而不影响导航栏高度?

时间:2018-02-23 05:50:42

标签: html twitter-bootstrap bootstrap-4

使用Bootstrap 4,我有一个带有以下导航项的导航栏:

<form class="form-inline">
        <a> <i class="fa fa-gear"></i> Option A</a>
      </form>

我想添加小文本对齐中心和图标下方。做这个的最好方式是什么? (选项A文本应该足够大,以便清晰,但不够大,导航栏引导类需要修改以适应)

注意:我想确保文本和图标在导航栏中间垂直对齐,而不增加导航栏的高度,“a”上的pt-3或mt-3似乎会影响。

添加<p>似乎也使导航栏“更高”......我想避免这种情况。

2 个答案:

答案 0 :(得分:1)

第一步是将其包装在<small>元素中,并将d-block类添加到图标中。然后,所有内容都包含在<span>中,类navbar-nav text-center pt-1 pb-0将两者都居中,并将它们垂直对齐在导航栏的中间。锚标记上的类nav-item nav-link p-0将其转换为导航链接并删除填充:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand navbar-light bg-primary">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav 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>
        </ul>
        <span class="navbar-nav text-center pt-1 pb-0">
            <a href="#" class="nav-item nav-link p-0"><small><i class="fa fa-gear d-block"></i>Option A</small></a>
        </span>
    </div>
</nav>

答案 1 :(得分:0)

未测试,但将选项放入<p>标记

 <form class="form-inline">
            <a> <i class="fa fa-gear"></i> <p>Option A</p></a>
        </form>