简体   繁体   English

一个div在另一个div中的垂直对齐,高度百分比

[英]Vertical alignment of a div in another div with %-height

How can I vertical align middle a div that is positioned in another div that has a variable height? 如何垂直对齐位于另一个具有可变高度的div的div的中间?

For example: 例如:

#content {
    min-height:450px;
    margin-top:0px;
    padding-top:0px;
    border-bottom:1px solid #c9e3f3;
    margin-bottom:0;
    overflow:hidden;
}

#content .background {
    width:100%;
    min-height:450px;
    position:relative;
    left:0px;
    top:0px;
    z-index:-2;
}

#inside {
    position:absolute;
    height:200px;
    width:200px;
    right:-18px;
    top:50%; // doesn't work
}

    <div id="content">
    <img class="background" src="/background.jpg" alt="background" />
    <div id="inside">text
    </div>
    </div>

#content has a min-height of 450px but actually get's it's (variable) height from an image (.background) that is positioned in #content. #content的最小高度为450px,但实际上是从#content中定位的图片(.background)的高度(可变)开始的。

Try this: 尝试这个:

#inside{
    position:absolute;
    height:200px;
    width:200px;
    top:50%;
    margin-top:-100px;
}

The margin-top should fix the centering, and position:absolute should work, if not, try 'relative'. 上边距应固定居中,并定位:绝对值应起作用,否则,请尝试“相对”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM