[英]Aspect ratio divs with CSS background images
I have images of equal aspect ratios (300px x 255px) in divs taking up ~31% of the width to make 3 columns on desktop/tablet, then full width on mobile. 我在div中有相等长宽比(300px x 255px)的图像,占据了大约31%的宽度,以便在台式机/平板电脑上打印3列,然后在移动设备上显示全宽。 The images scale to 100% within the div, with the height set as auto.
图像在div内缩放为100%,高度设置为自动。 I need to change them from img tags to background images
我需要将其从img标签更改为背景图片
http://codepen.io/anon/pen/yYagJd http://codepen.io/anon/pen/yYagJd
**HTML:**
<div class="hotels">
<img src="http://www.telodesign.com/test/cavallo-300.jpg" alt=""><br>
Here's a title
</div>
<div class="hotels">
<img src="http://www.telodesign.com/test/cavallo-300.jpg" alt=""><br>
Here's a title
</div>
<div class="hotels">
<img src="http://www.telodesign.com/test/cavallo-300.jpg" alt=""><br>
Here's a title
</div>
**CSS:**
.hotels {
display: inline-block;
width: 31.8%;
vertical-align: top;
margin-bottom: 22px;
}
*emphasized text*.hotels img {
width: 100%;
height: auto;
}
Is there a way to make these images background images, and still have the aspect ratio dictate the height of the div--allowing the same responsive scaling? 有没有办法使这些图像成为背景图像,并且纵横比仍然决定div的高度-允许相同的响应缩放? I'm hoping there's a way to do this without using .js, if possible.
我希望有一种方法,如果可能的话,不使用.js。 Can it be done with just CSS?
可以仅使用CSS完成吗? Thanks!
谢谢!
You could use % to set a padding taking width for reference to keep ratio. 您可以使用%来设置填充宽度,以作为保持比例的参考。 example : http://codepen.io/anon/pen/vNXgJp or http://codepen.io/anon/pen/VvKPMM (demos below)
例如: http : //codepen.io/anon/pen/vNXgJp或http://codepen.io/anon/pen/VvKPMM (下面的演示)
.hotels { display: inline-block; width: 31.8%; vertical-align: top; margin-bottom: 22px; background: url(http://www.telodesign.com/test/cavallo-300.jpg) no-repeat red; background-size: 100% auto; } .hotels:before { content: ''; padding-top: 85%; float: left; }
<div class="hotels"> Here's a title</div> <div class="hotels"> Here's a title</div> <div class="hotels"> Here's a title</div>
or 要么
.hotels { display: inline-block; width: 31.8%; vertical-align: top; margin-bottom: 22px; background: url(http://www.telodesign.com/test/cavallo-300.jpg) no-repeat red; background-size: 100% auto; } .hotels:before { content: ''; padding-top: 85%; display: inline-block; vertical-align: bottom; margin-left: -0.25em; } p { display: inline-block; width: 100%; text-align: center; background: rgba(0, 0, 0, 0.5); margin: 0; padding: 1em; box-sizing: border-box; }
<div class="hotels"> <p>Here's a title</p> </div> <div class="hotels"> Here's a title</div> <div class="hotels"> Here's a title</div>
You could try using view widths to keep the proportions on resize: 您可以尝试使用视图宽度来保持比例调整大小:
HTML
<div class="hotels">
Here's a title</div>
<div class="hotels">
Here's a title</div>
<div class="hotels">
Here's a title</div>
CSS
.hotels {
display: inline-block;
width: 31.8vw;
height: 26.3vw;
vertical-align: top;
margin-bottom: 22px;
background-image:url(http://www.telodesign.com/test/cavallo-300.jpg);
background-size: cover;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.