简体   繁体   English

如何垂直对齐bootstrap navbar品牌?

[英]How can I align a bootstrap navbar brand vertically?

The brand is centered until i change it to an img. 该品牌一直居中,直到我将其更改为img。

I have tried many different ways of fixing it in css, but it wont work. 我尝试了多种不同的方法来修复它的CSS,但无法正常工作。

How can i vertically align this logo? 我如何垂直对齐此徽标? and why doesnt my css work? 为什么我的CSS不起作用? For some reason, not all of my css styles are being applied. 由于某些原因,并不是我的所有CSS样式都被应用了。

<!DOCTYPE html>
<html>
<head>

<title>Stuff</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="app.css" >

</head>

<body>

<nav class="navbar navbar-default">


<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">
<img id="logo" alt="Brand" src="images/logo.jpg" width="40px" height="40px">
</a>
</div>
<div>


<div class="container" id="mynav">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>

    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Home</a></li>

      </ul>

      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Login</a></li>

      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

</body>

</html>

css: CSS:

#mynav
{
    width: 80%;
}

#logo
{
    width:50px;
    height: 50px;
    vertical-align: middle;
}

这是问题的图片

You should only need one .navbar-header div, just place the .navbar-brand into your standard .navbar-header and adjust the padding for the .navbar-brand class. 您只需要一个.navbar-header div,只需将.navbar-brand放入您的标准 .navbar-header并为.navbar-brand class.调整填充.navbar-brand class.

.navbar .navbar-brand {
  padding-top: 5px;
}

Working Example: 工作示例:

 .navbar .navbar-brand { padding-top: 5px; } .navbar .navbar-brand img { height: 40px; width: 40px; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <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 alt="Brand" src="https://unsplash.it/40/40/?random"> </a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a> </li> <li><a href="#">Home</a> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="#">Login</a> </li> </ul> </div> </div> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM