html - 为什么明显合适的子内联 block 会溢出?

标签 html css

<分区>

如果我将蓝色 div 的宽度从 212px 更改为 211px,为什么最后一个绿色 div 会溢出? 绿色 div 之间的空格来自哪里?

开发人员工具显示没有边距、没有填充、没有边框,根本没有任何可以产生这些间隙的东西。它们来自哪里?

212px 宽度 --> 无溢出

212px width --> no overflow

211px 宽度 --> 溢出

211px width --> overflow

#test {
  background-color: blue;
  height: 50px;
  width: 211px;
}

#test>div {
  display: inline-block;
  background-color: green;
  height: 50px;
  width: 50px;
}
<div id="test">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

最佳答案

你把你的div变成了内联元素,而内联元素对你代码中的空白很敏感,会占用空间。删除空格和间隙消失:

#test {
  background-color: blue;
  height: 50px;
  width: 211px;
}

#test>div {
  display: inline-block;
  background-color: green;
  height: 50px;
  width: 50px;
}
<div id="test">
  <div></div><div></div><div></div><div></div>
</div>

关于html - 为什么明显合适的子内联 block 会溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58490961/

上一篇:css - Angular Material : How to change the background color for the mat spinner path

下一篇:javascript - jQuery 在文本有限时隐藏“显示更多”按钮

相关文章:

css - D3 svg css - 使用 css 围绕中心旋转线

jquery - 如何在drupal本地网站上显示css

css - 如何删除 Bootsfaces 中的 dropMenu 插入符号?

html - 合并溢出-y : scroll with overflow-x: visible

java - 将 HTML 表从 JSP 传递到 Java 请求对象

php - 按钮发出 HTTP 请求并将数据存储在数据库中

html - 如何从 WCF WebAPI WebGet 返回原始 html

css - 如何更改在 R shiny 中动态创建的文本框的背景颜色?

java - 提取 HTML 标记外的文本

html - 相当于SVG中的背景位置百分比(%)