<div style="background-color:green"> <div style="width:40px;height:30px;background-color:red;"> </div> <div style="width:40px;height:30px;background-color:red;"> </div> <div style="width:40px;height:30px;background-color:red;"> </div> </div>
这时候咱们采起inline-block进行布局,神奇的事情就发生了css
<div style="background-color:green;"> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div> </div>
本来的3个div并排在一块儿,由于3个div变成了行内元素,因此也应该并排在一块儿,可是....
这里出现了两个问题:web
这里会出现这种问题的缘由是由于 inline-block水平呈现的元素间,换行显示或空格分隔的状况下会有间距
在这里我尝试过一个方法就是设置margin-right为负值,chrome
<div style="background-color:green"> <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px"> </div> </div>
在这里咱们能看到第一个div与第二个div仍然有一个1px的间距,而第二个和第三个没有间距,子div和父div没有间距,说明咱们的问题不是在这里segmentfault
我在参考张旭鑫大神的博客深感佩服以为采用这种方法最好:浏览器
font-size:0 -webkit-text-size-adjust:none;
po出代码frontend
<div style="background-color:green;font-size:0;-webkit-text-size-adjust:none;"> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div> <div style="width:40px;height:30px;background-color:red;display:inline-block;"> </div>
那么又有一个问题了,-webkit-text-size-adjust是什么????wordpress
哈哈,这样咱们的问题就解决了布局
最后说下inline-block的兼容性:参考这篇文章
-->
IE六、IE7不识别inline-block但能够触发块元素。其它主流浏览器均支持inline-block。这就够了,ie6,7就不考虑了,缘由嘛,你懂的...flex