我试图创建一个响应显示宽度的扑克牌队列.
我希望这个队列适合显示宽度,这样就不会有任何卡从显示中丢失.我想覆盖手机,平板电脑和台式机的传统宽度.
我也想将这个队列对齐到中心,这样它看起来不会很糟糕.
<div> <img src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> <img class="rest" src="http://chetart.com/blog/wp-content/uploads/2012/05/playing-card-back.jpg" alt=""> </div> div{ width:100%; } img{ display: inline-block; height: 200px; } .rest{ margin-left: -102px; }
解决方法
这是新
Flexbox model的完美契合!
在包装div上使用display:inline-flex,并删除img上的display:inline-block:
div{ width:100%; display:inline-flex; } img{ height: 200px; } .rest{ margin-left: -102px; /*flex-grow:1;*/ /* Bonus : all available width is occupied */ }
flex-grow:1会使你的img成长,同时空间可用,但它们会变形.
> Compatibility chart在Caniuse.com上
> MDN tutorial on Flexbox
> Flex tutorial on CSSTricks.com
> Flex reference on MDN