css - 将一个 div 在另一个高度为 100% 的 div 中垂直居中

标签 css responsive-design vertical-alignment

我想将一个 div 置于高度为 100% 的 div 中。基本上主要的 div 出现在网站之前。我上传了一张图片,这样您可以更好地理解我想做什么。

enter image description here

最佳答案

如果使用 CSS Flexbox是一个选项,您可以简单地通过将容器显示为 flex 框并在中间(水平和垂直)对齐内部 div 来实现,如下所示:

EXAMPLE HERE

#intro {
    height: 100%; /* Or 100vh */

    display: flex;
    align-items: center;     /* Align the inner div vertically */
    justify-content: center; /* Align the inner div horizontally */
}

在这个例子中,align-itemsjustify-content将使内部 div 垂直和水平显示在 #intro 的中间。

通过添加供应商前缀,它应该也适用于 IE 10。 (我在演示中使用了 Autoprefixer)。

然而,为了支持旧的网络浏览器,请尝试这种方法(查看垂直对齐部分):

关于css - 将一个 div 在另一个高度为 100% 的 div 中垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25590563/

相关文章:

html - 我可以在同一个表格列中有文本和 div 吗?

html - 我的左侧菜单和子菜单在低带宽情况下覆盖了我的整个页面

css - 如何在不同的断点处填充上面的折叠

html - 强制文本保持在一行

image - img 垂直对齐到 ul?

php - 如何仅模糊和透明背景而不是 jgrowl 通知中的内容

css - 是否可以阻止用户 Page Up、Page Down、向上和向下箭头键滚动功能?

html - 响应式内容

html - 如何垂直对齐与其 dd 标签相关的 dt 标签?

html - 将文本与图像垂直对齐