简体   繁体   English

如何设置父div宽度以匹配a / img?

[英]How to set the parent div width to match the a/img?

I want to have 4 images in a line, equally spaced. 我想一行中有4张图像,等距排列。 Then when the screen size is of a particular resolution, i want 2 images to drop to the line below. 然后,当屏幕尺寸为特定分辨率时,我希望将2张图像拖放到下面的行。

Setting the 4 images in a line, equally spaced is the easy part. 将4张图像排成一行,等距排列是容易的部分。 I'm struggling to get two of the images on the line below. 我正在努力在下面的行中获取两个图像。 My thought is to wrap two images in a div and do this twice. 我的想法是将两个图像包装在div中,然后执行两次。 So there is two divs, with 2 images in them each. 因此,有两个div,每个div中都有2张图片。 When the screen resolution is large, set the divs to inline-blocks . 当屏幕分辨率较大时,将div设置为inline-blocks Then when the resolution drops too low, set the divs to block. 然后,当分辨率下降得太低时,将div设置为block。

The issue: The div's width will not match the width of the two images. 问题: div的宽度与两个图像的宽度不匹配。 Instead, the divs span across the entire width of its parent div. 而是,div跨越其父div的整个宽度。

How do I fix this? 我该如何解决? Or is there a different way that's better? 还是有其他更好的方法?

JSFiddle JSFiddle

 .Containter{ text-align: center; display: inline-block; margin: 0 auto; border: 1px solid red; } .Sections{ display: inline-block; margin: 0 auto; border: 1px solid green; } .Sections>a{ border: 1px solid brown; } .Photo{ width: 20%; height: auto%; border-radius: 10px; /*margin: 0px 15px 0px 15px;*/ border: 1px solid blue; } 
 <div class="Containter"> <div class="Sections"> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> </div> <div class="Sections"> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> </div> </div> 

Thanks 谢谢

You can use media query to achieve what you want like this: 您可以使用媒体查询来实现所需的结果:

Here a working JSFiddle to play with 这是一个可以工作的JSFiddle

 .Containter { text-align: center; display: inline-block; margin: 0 auto; border: 1px solid red; } .Sections { display: inline-block; margin: 0 auto; border: 1px solid green; } .Sections>a { border: 1px solid brown; } .Photo { width: 20%; height: auto%; border-radius: 10px; /*margin: 0px 15px 0px 15px;*/ border: 1px solid blue; } @media (max-width: 768px) { /*change to desired resolution*/ .Photo { width: 45%; /*change to desired width*/ } 
 <div class="Containter"> <div class="Sections"> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> <a href="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"><img class="Photo" src="https://www.wonderplugin.com/wp-content/plugins/wonderplugin-lightbox/images/demo-image0.jpg"/></a> </div> </div> 

Check out this for more info 查看此以获取更多信息

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

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