【发布时间】:2010-12-26 22:55:48
【问题描述】:
我可以水平对齐一个 div 并且所有的内容看起来都不错。 希望垂直对齐不包含任何表格的 div。 我尝试将边距位置设置为#container 内的一些负值 但那种工作。我知道 CSS 还不支持这个?
这是我的标记:
body
{
background: #777; /* gray */
text-align: center;
}
#container
{
margin: 0 auto;
width: 968px;
text-align: left;
}
#toptab
{
background: #f77; /* red */
height: 14px;
width: 968px;
}
#middletab
{
background: #7f7; /* green */
width: 968px;
}
#data
{
width: 948px; /* 948 for the box plus we need 20 more px to handle the padding */
padding-left: 10px;
padding-right 10px;
}
#bottomtab
{
background: #77f; /* blue */
height: 14px;
width: 968px;
}
<div id="container">
<div id="toptab"></div>
<div id="middletab">
<div id="data">
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
The quick brown fox jumped over the big red bear.
</div>
</div>
<div id="bottomtab"></div>
</div>
运行上面的 sn-p 并单击“整页”以查看其当前的外观。 基本上,它在水平方向上看起来很棒,但现在我需要它在页面中垂直居中。
我要垂直对齐的元素是#container div。该效果将强制整个 div 和所有子 div 不仅水平对齐而且垂直对齐。我知道这是可能的,我知道 Andy Budd 发布了这样一个解决方案,但它似乎对我不起作用。
【问题讨论】:
-
您的意思是垂直分布/对齐还是仅将页面中垂直显示的内容居中?
-
@Eric 最终结果是带有内容的圆形框应位于页面中间。截至目前,您看到内容居中,但仅居中于顶部。我需要将它直接放在页面中间。
-
据我所知,除非你想要一个固定高度的页面,否则你必须做一些 javascript,我对此表示怀疑。