自定义高度Bootstrap导航栏

时间:2013-11-07 13:40:07

标签: html css twitter-bootstrap

我想要一个高度为150像素的标题,其中包含一个导航栏。导航栏应在标题中垂直居中。

HTML

<header>
    <div class="navbar navbar-static-top">
        <div class="navbar-inner">
            <div class="container" style="background:yellow;">
                <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-th-list"></span>
                </a>
                <a href="/"><img src="img/logo.png" class="logo" /></a>
                <nav class="nav-collapse collapse pull-right" style="line-height:150px; height:150px;">          
                    <ul class="nav" style="display:inline-block;">
                        <li><a href="">Portfolio</a></li>
                        <li><a href="">Blog</a></li>
                        <li><a href="">Contact</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

CSS

header {
    width: 100%;
    line-height: 150px;
    color:red;
    height: 150px;

    .navbar-inner {
        border:0;
        border-radius: 0;
        background: blue;
        padding:0;
        margin:0;
        height: inherit;
    }
}

导航/菜单/垂直列表现在位于标题的右上角。如何让它垂直居中? bootstrap.css是未定制的。

4 个答案:

答案 0 :(得分:114)

你的标记有点搞砸了。这是你需要的样式和正确的HTML

<强> CSS

.navbar-brand,
.navbar-nav li a {
    line-height: 150px;
    height: 150px;
    padding-top: 0;
}

<强> HTML

<nav class="navbar navbar-default">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>

        <a class="navbar-brand" href="#"><img src="img/logo.png" /></a>
    </div>

    <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            <li><a href="">Portfolio</a></li>
            <li><a href="">Blog</a></li>
            <li><a href="">Contact</a></li>
        </ul>
    </div>
</nav>

或者查看小提琴:http://jsfiddle.net/TP5V8/1/

答案 1 :(得分:12)

您还需要设置.min-height: 0px;,请参阅以下内容:

.navbar-inner {
    min-height: 0px;
}

.navbar-brand,
.navbar-nav li a {
    line-height: 150px;
    height: 150px;
    padding-top: 0;
}

如果你设置.min-height: 0px;,那么你可以选择你想要的任何高度!

祝你好运!

答案 2 :(得分:4)

我相信你正在使用Bootstrap 3.如果是这样,请尝试使用此代码,这里是bootply

<header>
    <div class="navbar navbar-static-top navbar-default">
        <div class="navbar-header">
            <a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="glyphicon glyphicon-th-list"></span>
            </a>
        </div>
        <div class="container" style="background:yellow;">
            <a href="/">
                <img src="img/logo.png" class="logo img-responsive">
            </a>

            <nav class="navbar-collapse collapse pull-right" style="line-height:150px; height:150px;">
                <ul class="nav navbar-nav" style="display:inline-block;">
                    <li><a href="">Portfolio</a></li>
                    <li><a href="">Blog</a></li>
                    <li><a href="">Contact</a></li>
                </ul>
            </nav>
        </div>
    </div>
</header>

答案 3 :(得分:4)

对于Bootstrap 4,现在有spacing utilities因此通过导航链接上的填充更改高度更容易。这可以仅响应地应用于特定断点(即:py-md-3)。例如,在较大(md)屏幕上,此导航为120px高,然后缩小到移动菜单的正常高度。不需要额外的CSS ..

<nav class="navbar navbar-fixed-top navbar-inverse bg-primary navbar-toggleable-md py-md-3">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Brand</a>
    <div class="navbar-collapse collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item py-md-3"><a href="#" class="nav-link">Home</a></li>
            <li class="nav-item py-md-3"><a href="#" class="nav-link">Link</a></li>
            <li class="nav-item py-md-3"><a href="#" class="nav-link">Link</a></li>
            <li class="nav-item py-md-3"><a href="#" class="nav-link">More</a></li>
            <li class="nav-item py-md-3"><a href="#" class="nav-link">Options</a></li>
        </ul>
    </div>
</nav>

Bootstrap 4 Navbar Height Demo