简体   繁体   English

将高度未知的div垂直居中

[英]Vertically centering a div with unknown height

I have a div ( #box ) on a page that is absolutely positioned a percentage from the top and bottom of the window. 我在页面上有一个div( #box ),它绝对位于窗口顶部和底部的一个百分比处。 Naturally, this will resize when the window dimensions change, 自然地,当窗口尺寸改变时,它将调整大小,

Inside this div is content that can be anything from a few lines to several paragraphs. 该div内的内容可以是几行到几段。 I would like this content to be vertically centred at all times, never exceeding #box 's height - with the main content overflowing when necessary. 我希望此内容始终保持垂直居中,不要超过#box的高度-主要内容在必要时会溢出。

HTML: HTML:

<div id="box">

    <div id="content">
        <h1>HEADING</h1>
        <div id="left">
            <p>
            // Lorem ipsum etc...
            </p>
        </div>
    </div>

</div>

CSS: CSS:

#box{
    position:absolute;
    top:15%; bottom:15%;
    background:white;
    left:50%;
}
#content{
    position:absolute;
    top:50%;
    overflow:auto;
    background:lightgrey;
}

jQuery(JavaScript): jQuery(JavaScript):

function alignContent(){

    // Position box
    $box = $('#box');
    $box.css({
        'width' : $box.height() + 'px',
        'margin-left' : '-' + $box.height()/2 + 'px'
    });

    // Position content
    $content = $('#content');
    console.log( $content.outerHeight() );
    $content.css({
        // Set maxheight smaller for aesthetic purposes
        'max-height' : $box.height()-72 + 'px',
        'margin-top' : '-' + ($content.outerHeight()/2) + 'px'
    });
}
alignContent();

// Update when resized
$(window).resize(function(){
    alignContent();
});

See it working. 看到它的工作。

The approach is - I think - quite simple and works most of the time. 我认为这种方法非常简单,并且在大多数情况下都有效。 The problem arises when the content is too tall for the #box on page load - the content is incorrectly positioned (notice also that the console.log returns the wrong value). 当内容对于页面加载时的#box太高时,就会出现问题-内容的位置不正确(还请注意console.log返回错误的值)。 Resizing aligns everything correctly again. 调整大小可以再次正确对齐所有内容。

What's making it align incorrectly on page load, when the content is taller than the container - and is it easily fixable? 当内容比容器高时,是什么导致它在页面加载时无法正确对齐?它是否易于修复?

Edit : Excuse my British inclination to spell it "centre" 编辑 :请原谅我的英国倾向将其拼写为“中心”

The resize event isn't triggered on page load, and your explicit call is too early during the DOMReady (it's fired when the DOM is complete, but does not yet have all secondary assets and stuff loaded) event. 调整大小事件不会在页面加载时触发,并且您的显式调用在DOMReady期间为时过早(DOM完成后会触发,但尚未加载所有辅助资产和填充物)事件。 You need to explicitly do the same for the document onLoad by adding: 您需要通过添加以下内容来显式地对文档onLoad进行此操作:

$(window).load(function(){
    alignContent();    
});

Fiddle sample here, works fine . 小提琴样本在这里,效果很好

More info on difference between onDOMReady and onLoad found here . 有关onDOMReadyonLoad之间的onDOMReady更多信息,请参见此处

Change the CSS for the box as below. 如下更改框的CSS。 Add the overflow:scroll; 添加overflow:scroll; . It'll work. 会的

#box{
    position:absolute;
    top:15%; bottom:15%;
    background:white;
    left:50%;
    overflow:scroll;
}

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

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