繁体   English   中英

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

[英]Responsive navbar with logo image and fixed height

我的页面上有一个固定的顶部导航栏,该导航栏设置为具有固定高度的图像,并且导航栏本身具有高度,我要做的是将导航栏品牌徽标与右侧列表中的列表垂直对齐大屏幕和响应式屏幕,我该怎么做? 这是我的代码:

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

您可以使用flex 我已经编辑了您的代码段。

我还编写了响应行为的代码,以便在小屏幕徽标和切换图标中也能正确排列。

我已经使用过height: auto !important .navbar-brand height: auto !important可以在实际代码中删除!important ,因为这是没有必要的。 我之所以写这篇文章,仅是因为在这段代码中,引导程序样式覆盖了我的样式。

对于小屏幕,请删除.navbar高度

.navbar{
  height:auto;
}

如果需要,可以改用min-height 这是因为引导程序将折叠用作静态元素(不是绝对的),所以导航栏将在菜单打开时扩展。

 .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