简体   繁体   English

如何使背景图像响应

[英]How to make a background-image on a image responsive

How do I get my #img-alfa image responsive? 如何使#img-alfa图片具有响应性? I already tried background-size:100% auto; 我已经尝试过background-size:100% auto; and if I delete the width and height of the background-image it totally disappears. 如果我删除background-imagewidthheight ,它将完全消失。

 .img-responsive { width: 100%; vertical-align: middle; } #img-alfa { top: -60px; left: 68px; height: 200px; width: 150px; position: absolute; background: url("../img/alfazwartwitsmall2.png") no-repeat; } #img-alfa:hover { top: -60px; left: 68px; height: 200px; width: 150px; position: absolute; background: url("../img/alfakleursmall2.png") no-repeat; } 
 <div class="col-xs-6 col-sm-3 placeholder"> <img class="img-responsive" src="img/background_white.jpg"> <span class="circle_inner"> <a href="#"><div style="border-radius: 0em;" class="img-responsive" id="img-alfa" ></div></a> </span> </div> 

Thanks! 谢谢!

You could use background-size: cover; 您可以使用background-size: cover; , with a trick to keep your image ratio: ,还有一个技巧来保持图像比例:

 .img-responsive { width: 100%; vertical-align: middle; } #img-alfa { padding-bottom: 75%; /* See comments under snippet */ background-image: url("https://placekitten.com/g/200/150"); background-size: cover; } 
 <img class="img-responsive" src="https://placekitten.com/200/150"> <div class="img-responsive" id="img-alfa"></div> 

The trick is to use a percent value for padding-bottom . 技巧是为padding-bottom使用百分比值。 The value will be calculated, based on the width value (= 100%). 根据宽度值 (= 100%)计算该值 Here how to define the 75% value: 以下是如何定义75%值:

padding-bottom = 100 * height / width = 100 * 150 / 250 = 75 底部填充= 100 *高度/宽度= 100 * 150/250 = 75

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

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