【发布时间】:2012-11-26 04:43:53
【问题描述】:
假设我在一个容器 div 中有 2 个 div,如下所示:
<div id="wrapper">
<div id="title">A</div>
<div id="text">Some text... Some text... Some text... Some text... Some text... Some text... Some text... Some text... Some text... Some text... Some text... </div>
</div>
正如您从标题中看到的那样,我正在尝试将 div title 和 text 垂直对齐,并且在父 div wrapper 内彼此相邻。到目前为止,我的 CSS 是这样的:
#wrapper
{
vertical-align:middle;
display:table-cell;
}
#title
{
background: url('path_to_purple_background') no-repeat;
width:45px;
height:45px;
color:white;
}
#text {
width: 700px;
}
#title, #text {
display: inline-block;
vertical-align: middle;
}
但到目前为止,我得到的是字母 A 不在我的 div title 中居中(而是位于 div 的左上角)。有人知道我该如何解决这个问题吗?
谢谢
【问题讨论】:
-
这里有两种简单的方法可以让 div 在 div 中居中,垂直居中,水平居中或两者兼而有之(纯 CSS):stackoverflow.com/a/31977476/3597276