在Bootstrap 4中折叠时如何用图标替换导航栏品牌?

时间:2018-01-30 14:22:30

标签: html twitter-bootstrap bootstrap-4

这里是boostrap的新手。那么当导航栏在Bootstrap 4中进入折叠模式时,如何用图标(例如来自Fontawesome的图标)或自定义图像徽标替换导航栏品牌?

我的代码如下:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<!-- <div class="container">  --><!-- you can turn this container off/on-->
        <a href="#" class="navbar-brand">Anime Battlegrounds</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <!-- collapse class will hide everything in the navbar once we hit mobile display -->
        <div class="navbar-nav">
            <a class="nav-item nav-link active" href="#">Popular</a>
            <a class="nav-item nav-link active" href="#">Anime List</a>
            <a class="nav-item nav-link active" href="#">Schedule</a>
        </div>

        <div class="navbar-nav ml-auto">
            <a class="nav-item nav-link active" href="#">Sign Up</a>
            <a class="nav-item nav-link active" href="#">Login</a>
        </div>

        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-primary my-2 my-sm-0 btn1" type="submit">Search</button>
        </form>
    </div>
<!-- </div> -->

2 个答案:

答案 0 :(得分:4)

使用与导航栏断点对应的Bootstrap 4 display utils

演示:https://www.codeply.com/go/9KbjsZ6pGP

<a href="#" class="navbar-brand">
   <span class="d-lg-inline-block d-none">Anime Battlegrounds</span>
   <i class="fa fa-star d-inline-block d-lg-none"></i>
</a>

d-lg-inline-block d-none表示在lg及以上显示,隐藏(d-none)低于lg
d-inline-block d-lg-none表示隐藏在lg及以上,在lg

下显示(d-inline-block

答案 1 :(得分:2)

这里的例子(如果我理解你的问题)

e.g。 (fontawesome文档中的更多图标)

<a href="#" class="navbar-brand">
   <i class="fas fa-ambulance"></i>
</a>

不要忘记在head-tag中:

<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

或:

<a href="#" class="navbar-brand">
       <img src="./image1.gif" />
</a>