Div内的垂直自动保证金

时间:2013-09-28 21:02:04

标签: html css

我现在正在尝试另一种奇怪且不起作用的事情:儿童div垂直自动对齐。

我希望contentpanel内垂直居中,因为panel的{​​{1}}%符合窗口大小,这对于我要严格对齐。

好的,这是我的代码:JSFiddle

HTML

height

CSS

<div id="panel">
    <div id="content">
    </div>
</div>

为什么这么简单的事情不起作用?

希望有人可以帮助我,我已经尝试过这些解决方案:margin : auto not working vertically?但它实际上没有成功的诀窍

1 个答案:

答案 0 :(得分:1)

这是一个简单的垂直对齐解决方案,使用Pure CSS而不修复任何上边距,顶部填充。所以它完全响应。

请参阅 Working Fiddle

HTML:(相同)

<div id="panel">
    <div id="content">
    </div>
</div>

<强> CSS:

html, body
{
    height: 100%;
    background-color: #273034;
    margin: 0;
}

#panel
{
    height: 100%;
    width: 380px;
    margin: 0 auto;
    background-color: rgba(255,255,255,0.3);
}

/*this is new*/
#panel:before
{
    content: '';
    height: 100%;
    margin-left: -5px;
    vertical-align: middle;
    display: inline-block;
}

#content
{
    vertical-align: middle;     /*this is new*/
    display: inline-block;    /*this is new*/
    height: 100px;
    width: 100%;    /*this is new*/
    background-color: rgba(117,169,56,0.9);
}