html - "inline-block"div 之间的神秘空白

标签 html css

<分区>

我有这样的布局:Fiddle link

.td 之间有一些空白,即使边距和填充设置为 0。

为什么会发生这种情况以及如何解决这个问题?可能是负边距?或者有什么更好的解决方案?

<style>
.tr {
    height: 20px;
    border: 1px solid black;
    overflow: hidden;
    white-space: nowrap;
    word-spacing: 0;
}
.td {
    display: inline-block;
    height: 20px;
    margin: 0;
    padding: 0;
}
</style>
<div class="tr" style="width: 150px;">
    <div class="td" style="width: 50px; background-color: #CCC;"></div>
    <div class="td" style="width: 50px; background-color: #AAA;"></div>
    <div class="td" style="width: 50px; background-color: #666;"></div>
</div>

最佳答案

解决方案一:添加评论:

<div class="tr" style="width: 150px;">
    <div class="td" style="width: 50px; background-color: #CCC;"></div><!--
    --><div class="td" style="width: 50px; background-color: #AAA;"></div><!--
    --><div class="td" style="width: 50px; background-color: #666;"></div>
</div>

你也可以把所有的东西都写在同一行上,但是加上注释看起来更干净。


解决方案 2: 添加 font-size:0 到父元素。不要忘记为子元素定义字体大小:

.tr {
  font-size: 0;
}
.td {
  font-size: 15px;
}

关于html - "inline-block"div 之间的神秘空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11982197/

相关文章:

javascript - 如何通过 JavaScript 监听音频按钮

jquery - jquery 中 mousemove() 上的 div 的最佳方法是什么?

html - 使用 JSX 在 ReactJS 中使用 <code> 或类似标签

javascript - 列压缩

html - 带有 CSS 下拉菜单的联系表单 - 无响应

javascript - Bootstrap 菜单附加在带有品牌形象的滚动时收缩 - 移动行为

javascript - 页面向下滚动时调整其高度的图像

html - @media 屏幕和(最大宽度 :768px) not working

html - 如何从底部而不是顶部扩展我的内容框的高度

CSS3 RotateY Tile Flip 在 Internet Explorer 中不起作用?