Bootstrap 4徽章覆盖在左上方,即使在崩溃时也会保持在导航栏链接上

时间:2018-04-29 17:54:33

标签: css twitter-bootstrap bootstrap-4

我想在导航链接中添加通知徽章,我有两个问题:

1)我无法将其置于左上角的图像上。

2)折叠导航栏后,徽章一直向左移动:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light fixed-top" >
    <div class="container">
        <a class="navbar-brand mx-auto" href="#">
			Logo
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">         
            <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
                <li class="nav-item main-nav">
                    <a id="chats_link" class="nav-link" href="/group_chats.php" style="margin-right:15px;color:white;">                        
                        <img title="Chats" src="/resources/chat_navbar.png" alt="Chats" width="25" height="25" style="z-index:10;">
                        <span class="badge badge-primary" style="z-index:15;float:left;margin-bottom:-10px;left:5px; top:-10px;position:relative;">9</span>)    
                    </a>
                </li> 
           </ul>

        </div>
    </div>
</nav>

1 个答案:

答案 0 :(得分:2)

这会是你想要的吗? (左边添加:-20px;对徽章的样式,删除其他一些东西)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light fixed-top" >
    <div class="container">
        <a class="navbar-brand mx-auto" href="#">
			Logo
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">         
            <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
                <li class="nav-item main-nav">
                    <a class="nav-link" href="#" style="margin-right:15px;color:white;">                        
                        <img src="https://images.freeimages.com/images/large-previews/e71/frog-1371919.jpg" alt="image" width="35" height="35" style="z-index:10;">
                        <span class="badge badge-primary" style="z-index:15;position:relative; left: -40px; top:-10px"">9</span>)    
                    </a>
                </li> 
           </ul>

        </div>
    </div>
</nav>