简体   繁体   English

如何垂直对齐具有不同高度的div?

[英]How to vertically align a div with varying height?

I have a popup at MY PAGE and I need the div that pops up to be vertically centered. 我在弹出我的页面 ,我需要弹出来垂直居中的DIV。 There will be more, since it's Tumblr and the blog has different posts so the divs will vary in height. 将会有更多,因为它是Tumblr,并且博客中的帖子不同,因此div的高度会有所不同。 I need it to be centered VERTICALLY in the middle so the content expands upwards and downwards. 我需要将其垂直居中放置,以使内容向上和向下扩展。 Anyway this can be done? 无论如何都可以做到?

With the following simple HTML: 使用以下简单的HTML:

<div id="wrap">
    <div id="holder">
        <div id="modal">some<br>content<br>in<br>here</div>
    </div>
</div>

wrap is what you want modal vertically centered in, but holder is necessary because, with the following CSS: wrap是您希望modal垂直居中的内容,但是需要使用holder ,因为使用以下CSS:

#wrap {
    height:300px;
}

#holder {
    margin:0 auto;
    width:60px; height:auto;
    position:relative; top:50%;
}

#modal {
    text-align:center;
    margin:0 auto;
    width:50px;
    position:relative;
}

...the top of holder and modal will be centered within wrap , but we want the center of this dynamic content to be in the center, so with a little simple Javascript: ... holdermodal的顶部将在wrap居中,但是我们希望此动态内容的中心在中部,因此需要一些简单的Javascript:

document.getElementById('modal').style.top = 
    "-" + document.getElementById('holder').offsetHeight/2 + "px";

...we shift modal up by half the automatic height of holder and voila! ...我们将modal上移了holder和瞧的自动高度的一半! There we have it! 到了! You can see an example with background colors to give a visual indication of what's going on here: http://jsfiddle.net/jRSZV/ 您可以看到一个带有背景色的示例,以直观地指示此处发生的情况: http : //jsfiddle.net/jRSZV/

Try changing the contents of modal to see that it works regardless of its height. 尝试更改modal的内容,以查看其高度如何均可正常工作。

var w = $(window);
$('.modal-dialog').css({
    'position': 'absolute',
    'top': Math.abs(((w.height() - $('.modal-dialog').outerHeight()) / 2) + w.scrollTop())
    //'left': Math.abs(((w.width() - $('.modal-dialog').outerWidth()) / 2) + w.scrollLeft())
});

$('html, body').animate({
    scrollTop: $('.modal-dialog').offset().top - 50
}, 500);

This piece of script will do what you are asking for. 这一段脚本将满足您的要求。 $('.modal-dialog') - This is the div you want to centralize vertically. $('。modal-dialog') -这是您要垂直集中的div。 See the commented line on the code. 请参阅代码上的注释行。 Uncomment it if you also want to centralize horizontally. 如果您还想水平居中,请取消注释。

for a vertically aligned div look here http://stylizedweb.com/2008/02/01/vertical-align-div/ and this might help! 对于垂直对齐的div,请参见http://stylizedweb.com/2008/02/01/vertical-align-div/ ,这可能会有所帮助!

but like what @blender said, this doesnt do dynamic. 但是就像@blender所说的那样,这并不动态。 So I would browse through http://plugins.jquery.com/project/valign 因此,我将浏览http://plugins.jquery.com/project/valign

Oh noes, not vertical centering with CSS. 哦,不,不是CSS的垂直居中。 It's complicated, but read here: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html . 它很复杂,但请在此处阅读: http : //www.jakpsatweb.cz/css/css-vertical-center-solution.html

If you are set on JS, just position the element like so (crudely typed, 90% chance it won't work): 如果您是在JS上设置的,则只需像这样放置元素即可(错误键入,90%的机会将不起作用):

$('#element').css('top', parseInt(($(document).height() - $(this).height()) / 2));
$('#element').css('left', parseInt(($(document).width() - $(this).width()) / 2));

It's not really elegant, but it might work. 它不是真的很优雅,但可能会起作用。 Good luck (I would tweak that animation. It's a good design, but the animation overlap lowers it's quality). 祝您好运(我会调整该动画。这是一个不错的设计,但是动画重叠会降低其质量)。

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

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