html - 添加文本元素会破坏 div 布局

标签 html css position

我有 3 个 div 显示在水平对齐的行内 block 中:

div {
  background:blue;
  height:200px;
  width:30%;
  margin:0px;
  padding:0px;
  border:10px solid red;
  display:inline-block;
  box-sizing:border-box
}

enter image description here

当我将一个文本元素添加到其中一个 div 中时,它会向下移动 *(除非文本是 position:absolute;)。
enter image description here

这是什么原因?

http://jsbin.com/suweba/2/edit

最佳答案

您需要使用 CSS 将 vertical-align: top 添加到您的 divvertical-align 的默认属性是 baseline,这就是为什么您的 div 内容会向下移动到底部。

这是一个 jsBin demo .

div {
  background: blue;
  height: 200px;
  width: 30%;
  margin: 0px;
  padding: 0px;
  border: 10px solid red;
  display: inline-block;
  vertical-align: middle;
  box-sizing: border-box
}
#b {} text {
  /* position:fixed; */
  color: white;
  font-size: 20px;
}
<body>
  <div id="a">
    <text>hello</text>
  </div>
  <div id="b"></div>
  <div id="c"></div>

关于html - 添加文本元素会破坏 div 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26621226/

相关文章:

html - 渐进式 Web 应用程序未在浏览器栏中显示安装按钮

CSS 悬停在同一元素内的另一个元素

html - 对齐侧边栏

javascript - 从 Javascript 访问 SVG 对象时收到 Uncaught SecurityError

html - 图像上的文本仅包含 css 和 html

css - 我的 Html 文件没有显示任何样式

CSS 叠加图像总是在同一个地方

html - 位置为 :absolute to dynamically extend to bottom of viewport? 的 DIV

html - 使用 CSS 将元素的宽度设置为 "0"(不是绝对定位)

javascript - Angular2 - 限制 p 内的行数