在bootstrap 4中为活动导航栏链接加下划线

时间:2017-08-23 07:32:34

标签: css twitter-bootstrap bootstrap-4

我将网站从bootstrap 3升级到4(测试版)。我用来强调活动导航栏链接的CSS已不再有效。它不是强调项目的下划线,而是强调页面的整个宽度。如何在bootstrap 4中为活动导航项加下划线?

Example Navbar

Bootstrap 3 css

.navbar-default .navbar-nav > .active:after {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    content: " ";
    border-bottom: 5px solid #5BC0EB;
}

尝试Bootstrap 4 css

.navbar-light .navbar-nav .active::after {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    content: " ";
    border-bottom: 5px solid #5BC0EB;
}

HTML

<nav class="navbar fixed-top navbar-light bg-light navbar-expand-md">
    <div class="container">
        <a class="navbar-brand" href="#">My Website</a>

        <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>

        <div class="collapse navbar-collapse justify-content-end" id="main-nav-collapse">
            <ul class="navbar-nav">
                <li class="nav-item active"><a class="nav-link" href="#about">About</a></li>
                <li class="nav-item"><a class="nav-link" href="#classes">Classes</a></li>
                <li class="nav-item"><a class="nav-link" href="#gallery">Gallery</a></li>
                <li class="nav-item"><a class="nav-link" href="#events">Events</a></li>
                <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
                <li class="nav-item"><a class="nav-link" href="#offers">Offers</a></li>
            </ul>
        </div>
    </div>
</nav>

1 个答案:

答案 0 :(得分:12)

这是因为你的定位。

尝试将relative位置设置为li,然后设置a li内的absolute标记,.navbar-nav > li { float: left; position: relative; } .navbar-light .navbar-nav .active a::after { border-bottom: 5px solid #5bc0eb; bottom: -10px; content: " "; left: 0; position: absolute; right: 0; } 位置如下

-c:v libx264 -pix_fmt yuv420p

希望它有所帮助。