简体   繁体   English

带有徽标图像和固定高度的自适应导航栏

[英]Responsive navbar with logo image and fixed height

I have a fixed top navbar in my page, the navbar is set to an image with a fixed height and the navbar itself has a height, what i want to do is to vertically align the navbar brand logo with the list on the right side in large and responsive screens, how can i do that? 我的页面上有一个固定的顶部导航栏,该导航栏设置为具有固定高度的图像,并且导航栏本身具有高度,我要做的是将导航栏品牌徽标与右侧列表中的列表垂直对齐大屏幕和响应式屏幕,我该怎么做? Here is my code: 这是我的代码:

 .navbar{ height:150px; } .navbar-brand img{ height:95px; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <html> <body><!-- Navigation --> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <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="http://placehold.it/150x50&text=Logo" alt=""> </a> </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> <a href="#">About</a> </li> <li> <a href="#">Services</a> </li> <li> <a href="#">Contact</a> </li> </ul> </div> <!-- /.navbar-collapse --> </div> <!-- /.container --> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> 

you can use flex . 您可以使用flex i have edited your snippet. 我已经编辑了您的代码段。

I have also written code for responsive behavior so that in small screen logo and toggle icon will also arrange properly. 我还编写了响应行为的代码,以便在小屏幕徽标和切换图标中也能正确排列。

i have used height: auto !important for .navbar-brand you can remove !important in your real code because it's not necessary. 我已经使用过height: auto !important .navbar-brand height: auto !important可以在实际代码中删除!important ,因为这是没有必要的。 i wrote this only because in this snippet, bootstrap style is overriding my style. 我之所以写这篇文章,仅是因为在这段代码中,引导程序样式覆盖了我的样式。

for small screen remove height of .navbar 对于小屏幕,请删除.navbar高度

.navbar{
  height:auto;
}

if you need, you can use min-height instead. 如果需要,可以改用min-height it's because bootstrap used collapse as static element not absolute, so the navbar will extend on menu open. 这是因为引导程序将折叠用作静态元素(不是绝对的),所以导航栏将在菜单打开时扩展。

 .navbar{ height:150px; } .navbar-brand img{ height:95px; } .navbar > .container { display: flex; align-items: center; } .navbar-brand { height: auto !important; } .navbar-toggle { margin-left: auto; order: 2; } @media (max-width: 767px) { .navbar > .container { display: block; } .navbar-header { display: flex; align-items: center; } .navbar{ height: auto; } } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <html> <body><!-- Navigation --> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <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="http://placehold.it/150x50&text=Logo" alt=""> </a> </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> <a href="#">About</a> </li> <li> <a href="#">Services</a> </li> <li> <a href="#">Contact</a> </li> </ul> </div> <!-- /.navbar-collapse --> </div> <!-- /.container --> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> 

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

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