html - 如何在不给出屏幕高度的情况下垂直对齐 div?

标签 html css flexbox positioning

我正在尝试构建一个水平和垂直对齐的 slider 。 我在这段代码中使用 display: flex;:

body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}
<div id="slider">

  the content

</div>

我希望 div 位于屏幕中央,但如果不给 body 一定的高度,这似乎对我不起作用。我必须使用 height: 100vh; 还是有更好的解决方案?

最佳答案

您还需要在 html 上设置 height: 100%

html {
  height: 100%;
}
body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}
<div id="slider">
  the content
</div>

关于html - 如何在不给出屏幕高度的情况下垂直对齐 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943128/

相关文章:

css - angular-flex-layout:fxLayoutWrap 将 ... 附加到不适合的文本,而不是将文本换行到下一行

html - 当 div 在 float 元素下方折叠时保持 div 的垂直间距

html - Outlook 2007 到 2013 显示设备媒体查询

css - 包含 UL 列表的滚动条框

css - Flexbox 和溢出 : hidden not working properly

css - 在滚动条上方显示悬停元素

html - 旋转木马图像不在 bootstrap css 中旋转

html - 折叠 div 的边框

html - 如何仅使用 CSS Flex 在行上居中和右对齐

javascript - 倒计时 Javascript 不适用于较小的屏幕/移动设备