最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - Vertically center div with Javascript (variable height) - Stack Overflow

programmeradmin3浏览0评论

I'm trying to vertically center a div with Javascript. Because the text is going to be changing, I cannot use a fixed height.

I'd like to do this without Jquery.

#box2 {

    width: 100%;
    height: 100%;
    position:relative;
    background-color: orange;

}
            #informationBox {

            padding: 0.5em;
            background-color: #fff;
            border-radius: 12pt;
            border: solid black 3pt;
            max-width: 683px;
            margin: 0 auto;
            text-align: center;
        }

Javascript:

var container = document.getElementById("#box2");
var inner = document.getElementById("#informationBox");
var inHeight = inner.offsetHeight;

container.style.height=(window.innerHeight);
container.style.width=window.innerWidth;

var conHeight=container.offsetHeight;

inner.style.marginTop=((conHeight-inHeight)/2);

Any help would be great :)

/

I'm trying to vertically center a div with Javascript. Because the text is going to be changing, I cannot use a fixed height.

I'd like to do this without Jquery.

#box2 {

    width: 100%;
    height: 100%;
    position:relative;
    background-color: orange;

}
            #informationBox {

            padding: 0.5em;
            background-color: #fff;
            border-radius: 12pt;
            border: solid black 3pt;
            max-width: 683px;
            margin: 0 auto;
            text-align: center;
        }

Javascript:

var container = document.getElementById("#box2");
var inner = document.getElementById("#informationBox");
var inHeight = inner.offsetHeight;

container.style.height=(window.innerHeight);
container.style.width=window.innerWidth;

var conHeight=container.offsetHeight;

inner.style.marginTop=((conHeight-inHeight)/2);

Any help would be great :)

http://jsfiddle/tmyie/EttZQ/

Share Improve this question asked Jun 18, 2013 at 21:15 user1469270user1469270
Add a ment  | 

4 Answers 4

Reset to default 5

You pretty much have it, you just need to change a couple things. First, getElementById takes just an id-string, not a selector. Secondly, you need to add 'px' to your style declration.

var container = document.getElementById("box2");
var inner = document.getElementById("informationBox");
var inHeight = inner.offsetHeight;
container.style.height = window.innerHeight;
container.style.width = window.innerWidth;
var conHeight = container.offsetHeight;

inner.style.marginTop = ((conHeight-inHeight)/2)+'px';

Here's an updated fiddle: http://jsfiddle/EttZQ/1/

Using js with line-height property.

less javascript, and precise centering.

box/info can have min/max-width/height & % or px ...

also resizes with the window.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>center</title>
<style>
html,body{
 width:100%;
 height:100%;
 margin:0;
 padding:0;
}
#box{
 width:100%;
 height:100%;
 text-align:center;
}
#info{
 display:inline-block;
 margin:0;padding:0 16px;
 line-height:24px;
 border-radius: 12pt;
 border: solid black 3pt;
 text-align: center;
 vertical-align:middle;
 box-sizing: border-box;
}
</style>
<script>
var center=function(){
 var b=document.getElementById('box');
 b.style['line-height']=b.offsetHeight+'px';
}
window.onload=center;
window.onresize=center;
</script>
</head>
<body>
<div id="box"><div id="info">center me pls<br>test</div></div>
</body>
</html>

One simple CSS solution.

http://jsfiddle/antouank/EttZQ/2/

body {
    margin: 0;
}
#box2 {
    width: 100%;
    height: 100%;
    position:absolute;
    background-color: orange;
}
#informationBox {
    padding: 0.5em;
    background-color: #fff;
    border-radius: 12pt;
    border: solid black 3pt;
    max-width: 100px;
    margin: 0 auto;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
}

You only need to calculate the width/height of your element, and change 50% to whatever you need to center it.

Make container pos anything other than static. Box : pos absolute. top:50% When box height changes, set margin top of box to -1 * height/2.

发布评论

评论列表(0)

  1. 暂无评论