html - 为什么第二个 div 是向下滑动的?

标签 html css

我有下一个 html/css 代码:

.first,
.second {
  width: 200px;
  height: 100px;
  border: 1px solid #333;
  text-align: center;
  display: inline-block
}

.first > ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
<div class="first"><h1>first div</h1><ul><li>text</li></ul></div>
<div class="second"><h1>second div</h1></div>

为什么当我将 ul 元素放在第一个 div 中时,第二个 div 会向下滑动?

JS Bin link , 以防万一。 谢谢!

最佳答案

因为vertical-align(适用于inline-level和table-cell元素)的默认值是baseline,所以需要使用vertical-align : 顶部

.first,
.second {
  width: 200px;
  height: 100px;
  border: 1px solid #333;
  text-align: center;
  display: inline-block;
  vertical-align: top;
}
.first > ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
<div class="first">
  <h1>first div</h1>
  <ul>
    <li>text</li>
  </ul>
</div>
<div class="second">
  <h1>second div</h1>
</div>

关于html - 为什么第二个 div 是向下滑动的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012137/

相关文章:

html - 使用CSS隐藏循环中的最后一小时

Android WebView 没有正确加载本地 html 字符串

html - 图内 img 和 figcaption 之间不需要的空格

html - 电子邮件中的表格背景图像

Javascript:只运行一次setInterval

php - 为 div 中的每个数字添加边框

javascript - 在其他页面上显示div

css - 页眉背景 - 左边距

html - 如何使用 auto bootstrap4 内联列

javascript - 测试自定义 Angular Directive(指令)是否显示/隐藏元素