简体   繁体   English

如何保持 <div> 具有背景图像的具有动态高度

[英]How to keep <div> with a Background image have dynamic height

I need to mimic the following behavior of the div with a background Image. 我需要用背景图像模仿div的以下行为。

The page has 3 div on top of one another. 该页面彼此之间有3格。 They have a maximum width of 600px. 它们的最大宽度为600像素。 When opened on a smaller screen the width adjusts to the the screen, and the height should adjust too, keeping the aspect ratio of the image which is always 10/7 在较小的屏幕上打开时,宽度会根据屏幕调整,高度也应调整,以保持图像的纵横比始终为10/7

I tried playing with the background-size style attribute, but that doesn't change the height of the div, so the picture ends up being clipped. 我尝试使用background-size样式属性,但这并没有改变div的高度,因此图片最终被裁剪了。 I can't afford to clip the image. 我无法裁剪图像。

Please advise 请指教

one 在此处输入图片说明

two 在此处输入图片说明

three 在此处输入图片说明

Well, actually it depends on your real markup but this could be a starting point: 好吧,实际上这取决于您的实际标记,但这可能是一个起点:

The key point is that a percentage value on top/bottom padding is relative to the width of the box's containing block. 关键是顶部/底部padding的百分比值是相对于框包含块的width而言的。

Also, using background-size: contain resizes the background image with aspect ratio. 另外,使用background-size: contain contains可以使用宽高比调整背景图像的大小。 It's worth noting that this feature is supported in IE9+. 值得注意的是IE9 +支持此功能。

For further reading on responsive containers, you could refer to my answer here . 要进一步了解响应式容器,可以在这里参考我的答案。

Example Here . 这里的例子

 .box { background: url(http://placehold.it/300) 50% 50% / contain no-repeat; width: 80%; /* or whatever else */ overflow: hidden; position: relative; } .box > span { position: absolute; top: 0; right: 0; bottom: 0; left: 0; padding: 5% 0; } .box > span:before, .box > span:after { content: ""; position: absolute; left: 0; right: 0; padding: 2% 0; } .box > span:before { top: 0; background-color: rgb(255, 90, 79); } .box > span:after { bottom: 0; background-color: rgb(159, 31, 56); } .box:before { content: ""; display: block; padding-bottom: 70%; } 
 <div class="box"> <span> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis dolores doloremque id quia esse! Doloribus voluptate nihil quam natus, similique, vero amet nesciunt excepturi. Optio dicta quas, dolores facere voluptatem. </span> </div> 

If your div only serves to show this image, you can do it simply with this css "trick" 如果您的div仅用于显示此图像,则可以使用此CSS“技巧”轻松完成此操作

 div{ background: url(http://gogoodscout.com/blog/wp-content/uploads/2015/02/Do-YOu-Feel-Good.jpg); width: 100%; padding-bottom: 49%; background-size: cover; } 
 <div></div> 

Notice how if you adjust the padding to reflect the aspect ratio, the div will always have the aspect ratio of the image. 请注意,如果您调整填充以反映长宽比,div将始终具有图像的长宽比。

This trick could help you: 这个技巧可以帮助您:

 #container{ background-image:url("http://cdn4.colorlib.com/wp/wp-content/uploads/sites/2/2014/02/Olympic-logo.png"); background-size:cover; } #ghostImg{ width:100%; visibility:hidden; } 
 <div id="container"> <img src="http://cdn4.colorlib.com/wp/wp-content/uploads/sites/2/2014/02/Olympic-logo.png" id="ghostImg"/> </div> 

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

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