简体   繁体   English

如何使图像以设定的高度填充整个容器

[英]How can I make the image fill the whole container with a set height

I want my image to take up the whole container with a set height of 350px without sacrificing its aspect ratio 我希望我的图像以350px的高度占据整个容器,而不会牺牲其长宽比

I tried this 我试过了

<div class="container-fluid">
 <div class="row">
  <div class="background-container" >
   <img src="bgImageUrl" class="img-responsive" height="350px">

.background-container {
  max-height: 350px;
}

The image is 1400x700. 图片为1400x700。 When I set the height to 350px it just covers the left side and the rest as a blank space. 当我将高度设置为350px时,它仅覆盖左侧,其余作为空白。 And setting its width to 100% just stretched the image 并将其宽度设置为100%只会拉伸图像

EDIT 编辑

I also want the image responsive (hence the img-responsive class) so when the screen is smaller it'll shrink as well 我还希望图像响应(因此为img-responsive类),所以当屏幕较小时,它也会缩小

Why not something like this? 为什么不这样呢? Move the image to a class so looks like this. 将图像移到一个类上,如下所示。 Also is responsive and scales as the page gets smaller. 也是响应式的,并随着页面变小而缩放。

 .background-container { height: 350px; width: 100%; background: url('https://static.pexels.com/photos/68672/beach-beverage-caribbean-cocktail-68672.jpeg') 0 center no-repeat; background-size: cover; } @media only screen and (max-width: 640px) { .background-container { height: 180px; width: 100%; background: url('https://static.pexels.com/photos/68672/beach-beverage-caribbean-cocktail-68672.jpeg') 0 center no-repeat; background-size: cover; } } 
  <div class="container-fluid"> <div class="row"> <div class="background-container"> </div> </div> </div> 

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

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