html - 垂直对齐 div 的内容

标签 html css alignment

我想垂直对齐我的 div 的内容。

所以基本上“Hello test”应该垂直放置在 div 的中心。

Demonstration

.parent {
    background:blue;
    float:left;
    width:100%
}

.parent div {
    float:left;
    width:50%;
}

h1 {
    font-size:50px;
    margin:0;
    padding:0;
}
<div class="parent">

<div>
    <h1>hello!</h1>
    <p>test</p>
</div>

<div>
    <img src="http://placehold.it/250x250" />
</div>

</div>

最佳答案

您可以为此使用表格布局:

.parent {
    background:blue;
    width:100%;
    display: table;
}

.parent div {
    display:table-cell;
    vertical-align:middle;
    width:50%;
}

h1 {
    font-size:50px;
    margin:0;
    padding:0;
}
<div class="parent">

<div>
    <h1>hello!</h1>
    <p>test</p>
</div>

<div>
    <img src="http://placehold.it/250x250" />
</div>

</div>

关于html - 垂直对齐 div 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31556111/

相关文章:

html - 当图像穿过一条线时反转图像的颜色?

html - 为什么在同一个输入标签中有不同的样式

css - 我可以使用 font-family :Helvetica Neue Light 获得与 photoshop 中相同的效果吗

c - 对齐结构数组以制作表格

javascript - 将地址簿更改为表格

php - HTML PHP 添加数量和价格到我的菜单项

html - 如何将 div 放置在另一个 div 中的 div 中,并使它们全部为 'resizable' 并适合任何浏览器大小?

css - 自定义 ionic 3 中的侧边菜单滚动条?

css - 如何水平对齐导航栏中的三个元素?

css - 如何在 CSS 中允许水平溢出?