我认为这很简单,但事实证明这有点头疼.当用户调整浏览器大小并导致其中一个(或多个)换行到下一行时,我正试图让图像网格重新居中.
我试过给网格包装器显示:inline-block;它的父级值为text-align:center;但是当它们换行到一个新行时,这并不会使元素重新居中.帮助赞赏.
对于我想要实现的视觉picture here http://ianclarke.ca/div-reflow.png的视觉效果.
HTML:
<div class="parent-wrapper"> <div class="child-wrapper"> <!-- Worpress loop to load many thumnails --> <?PHP if(have_posts()) : ?><?PHP while(have_posts()) : the_post(); ?> <div class="project-thumbnail"> <?PHP the_post_thumbnail('thumbnail'); ?> </div> <?PHP endwhile; ?> </div> </div>
CSS:
.parent-wrapper { width:100%; text-align: center; } .child-wrapper { display:inline-block; } .project-thumbnail{ float:left; border:2px solid black; min-width: 269px; max-width: 269px; }
解决方法
这是我只能用CSS思考的最佳解决方案,神奇的部分是@media查询.显然你必须做数学计算以适应你的情况.
body { margin: 0; } .parent-wrapper { margin: auto; width: 500px; padding: 5px 0; font-size: 0; } .child-wrapper { display: inline-block; width: 100px; font-size: 16px; } .child-wrapper img { max-width: 100%; height: auto; padding: 5px; vertical-align: top; Box-sizing: border-Box; } @media screen and (max-width: 499px) { .parent-wrapper { width: 400px; } } @media screen and (max-width: 399px) { .parent-wrapper { width: 300px; } } @media screen and (max-width: 299px) { .parent-wrapper { width: 200px; } } @media screen and (max-width: 199px) { .parent-wrapper { width: 100px; } }
<div class="parent-wrapper"> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> <div class="child-wrapper"> <img src="//dummyimage.com/100" /> </div> </div>