【发布时间】:2016-12-20 18:54:11
【问题描述】:
我正在制作一个幻灯片,其中包含多个用户生成的缩略图填充一个包含的 div。布局是响应式的,因此当页面调整大小时,拇指将相互堆叠。仅使用 css 调整页面大小时,有什么方法可以强制父 div 紧紧拥抱内容?下面是一些图片来说明我的问题,并包含我的代码。
有没有办法去掉图2中多余的空间??
.parent{background-color:#003300; overflow: auto; padding:0px 20px 20px 0px; position: absolute;}
.child{ width:100px; height:100px; float:left; background-color:#000066; margin:20px 0px 0px 20px;}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
编辑 - 我已经更改了 sn-p 以更好地代表我想要完成的工作,它可以工作,但它很丑陋,当父级变得太大时,浏览器滚动条可能会通过媒体标签搞乱更改给多个孩子。所以我想我现在的问题是,有没有更好的方法可以在没有多个媒体标签的情况下实现这一目标?
body { margin:0; padding:0;}
.cnt{text-align:center; max-width:960px; background-color:#000000; margin:0px auto; overflow:hidden}
.parent{background-color:#003300; overflow: auto; padding:0px 20px 20px 0px;display:inline-block}
.child{ width:100px; height:100px; float:left; background-color:#000066; margin:20px 0px 0px 20px; text-align:center; line-height:100px; font-size:36px}
@media (max-width:740px) and (min-width:621px){.child:nth-child(5n+6){ clear:both;}}
@media (max-width:620px) and (min-width:501px){.child:nth-child(4n+5){ clear:both;}}
@media (max-width:500px) and (min-width:381px){.child:nth-child(3n+4){ clear:both;}}
@media (max-width:380px) and (min-width:261px){.child:nth-child(2n+3){ clear:both;}}
@media (max-width:260px) and (min-width:0px){.child{clear:both;}}
<div class="cnt">
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
</div>
【问题讨论】:
标签: html css css-float elements responsive