简体   繁体   English

CSS在元素之间显示内联块间隙

[英]CSS display inline-block gaps in between elements

HTML: HTML:

<div id='container'>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
</div>

CSS: CSS:

#container {
    width: 960px;
    height: 960px;
    background-color: #ccc;
}

.box {
    width: 300px;
    height: 300px;
    background-color: blue;
    display: inline-block;
}

Result: https://jsfiddle.net/58fmcabz/ 结果: https : //jsfiddle.net/58fmcabz/

When I use table-cell in the display property the gaps disappear. 当我在display属性中使用 table-cell ,间隙消失了。 I tried removing margin and padding using the .box class to no avail. 我尝试使用.box类删除边距和填充无效。 Why are the gaps there? 为什么会有差距?

The easy solution is to add a float to your elements: 简单的解决方案是在元素中添加浮点数:

.box{width:300px; height:300px; display:inline-block; background-color:blue; float:left;}

That's it solved. 解决了。 The inline-block property is kinda creepy and I had to bear that for a whole day when I had the problem, a simple float solves the problem. 内联块属性有点令人毛骨悚然,当我遇到问题时,我不得不忍受一整天,一个简单的浮点数就可以解决问题。

Someone asked the same question here- Unwanted margin in inline-block list items 有人在这里问了同样的问题- 内联阻止列表项目中的多余边距

try this.... 尝试这个....

.box {
     width: 300px;
     height: 300px;
     background-color: blue;
     display: inline-block; 
     float:left;
}

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

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