简体   繁体   English

网格图像库-宽度是自适应的,但也需要高度也要自适应

[英]Grid image gallery - Width is responsive but need height to be responsive too

My grid image gallery has four images on each line. 我的网格图像库每行有四个图像。 The width:25% in the CSS makes the width responsive but I need the height to also be responsive. CSS中的width:25%使宽度响应,但是我需要高度也要响应。 At the moment I have the height as 180px. 目前,我的身高为180px。 Could I set it as a percentage to make it responsive like the width? 我可以将其设置为百分比以使其像宽度一样响应吗? I've tried but when I set it as a percentage it doesn't work. 我已经尝试过,但是当我将其设置为百分比时它不起作用。 It only works if I move height:x% to line1 img/line2 img, but then the image becomes squashed. 仅当我将height:x%移到line1 img / line2 img时才起作用,但随后图像被压缩。

<div class="line1">
<img src="image1.jpg" alt="image1.jpg" />
<img src="image2.jpg" alt="image2.jpg" />
<img src="image3.jpg" alt="image3.jpg" />
<img src="image4.jpg" alt="image4.jpg" /></div>

<div class="line2">
<img src="image5.jpg" alt="image5.jpg" />
<img src="image6.jpg" alt="image6.jpg" />
<img src="image7.jpg" alt="image7.jpg" />
<img src="image8.jpg" alt="image8.jpg" /></div>


.line1{
overflow:hidden;    
height:180px;
}
.line1 img{
width:25%;
}
.line2{
overflow:hidden;    
height:180px;
}
.line2 img{
width:25%;
}

Set both .line1{ height: auto;} and .line1{ height: auto; 同时设置.line1{ height: auto;}.line1{ height: auto; this will keep it responsive. 这将使其保持响应。 Check the jsfiddle example. 检查jsfiddle示例。

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

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