[英]Fit image to bootstrap navbar height responsively
I would like to know how to make an image fit bootstrap navbar height (proportionally), here's a descriptive image: 我想知道如何使图像适合引导导航栏高度(按比例),这是一个描述性图像:
Any idea? 任何想法?
I'm using this structure: 我正在使用此结构:
<nav class="navbar navbar-default">
<divclass="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img alt="Brand" src="...">
</a>
</div>
</div>
</nav>
If you're simply trying to adjust the size of an image to correspond to the height of a default navbar
( min-height: 50px
), simply use the below CSS as a base depending on how you want the image to fit. 如果您只是尝试调整图像的大小以使其与默认navbar
min-height: 50px
( min-height: 50px
)相对应,则根据您希望图像的放置方式,使用以下CSS作为基础。
In this example, the image will cover it's part of the navbar
completely. 在此示例中,图像将完全覆盖navbar
的一部分。
See working Snippet. 请参阅工作片段。
body { padding-top: 70px; } .navbar.navbar-inverse { border: none; } .navbar .navbar-brand { padding-top: 0px; } .navbar .navbar-brand img { height: 50px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <nav class="navbar navbar-inverse navbar-fixed-top"> <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 src="http://placehold.it/350x150/f00/fff"> </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="#">Link </a> </li> <li><a href="#">Link </a> </li> <li><a href="#">Link </a> </li> <li><a href="#">Link </a> </li> </ul> </div> </div> </nav> <div class="container text-center"> <img src="http://placehold.it/350x150/f00/fff" /> <h4>The Same Image At Full Scale</h4> </div>
in a previous project of mine I also encountered some issues with this. 在我以前的项目中,我也遇到了一些问题。 Here is how I solved the problem: 这是我解决问题的方法:
HTML: HTML:
<nav id="myNavbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<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="index.html" class="smoothScroll"><img src="img/logo.png" ondragstart="return false;" alt="logo"/></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav" >
<li>
<a>Menu Item1</a>
</li>
</ul>
</div> <!-- /.navbar-collapse -->
</div> <!-- /.container -->
</nav>
And the CSS for it: 和它的CSS:
.navbar-fixed-top {height: 40px;}
.navbar-brand img {height: 40px;}
So setting the navbar and the logo-image to the same height (and not giving it width, thus it will be automatically adjusted) solved the problem for me with good positioning. 因此,将导航栏和徽标图像设置为相同的高度(不设置其宽度,这样它将被自动调整)以良好的定位为我解决了这个问题。 Two possible cases: 两种可能的情况:
you want a fixed navbar: 您需要固定的导航栏:
.navbar-brand img {position:fixed;right:0;top:0} .navbar-brand img {position:fixed; right:0; top:0}
you want a header on top: 您想在顶部放置标题:
.navbar-brand img {position:absolute;right:0;top:0} .navbar-brand img {position:absolute; right:0; top:0}
In both cases I set it to top-right corner of the site, but you can adjust that easily. 在这两种情况下,我都将其设置为网站的右上角,但是您可以轻松地对其进行调整。 So the key is: same height navbar and image, with the image positioned well . 因此,关键是: 导航栏和图像的高度相同,并且图像定位良好 。 (Then margin-padding or exact position of the image can easily be set in CSS) (然后可以在CSS中轻松设置图片的边距填充或确切位置)
Hope it helped, Andrew 希望能有所帮助,安德鲁
EDIT: Regarding fixed navbar please check the Bootstrap documentation (it has built-in class like navbar-fixed-top and such). 编辑:关于固定的navbar,请检查Bootstrap文档(它具有内置类,如navbar-fixed-top等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.