使用 Javascript 垂直居中 div(可变高度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17179077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:23:51  来源:igfitidea点击:

Vertically center div with Javascript (variable height)

javascriptdomvertical-alignment

提问by cocco

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

我正在尝试使用 Javascript 垂直居中一个 div。因为文本会发生变化,所以我不能使用固定的高度。

I'd like to do this withoutJquery.

我想在没有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:

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.net/tmyie/EttZQ/

http://jsfiddle.net/tmyie/EttZQ/

回答by rgthree

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

你几乎拥有它,你只需要改变一些事情。首先,getElementById只需要一个 id 字符串,而不是一个选择器。其次,您需要在样式声明中添加“px”。

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.net/EttZQ/1/

这是一个更新的小提琴:http: //jsfiddle.net/EttZQ/1/

回答by cocco

Using js with line-height property.

使用具有 line-height 属性的 js。

less javascript, and precise centering.

更少的 javascript 和精确的居中。

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

box/info 可以有 min/max-width/height & % 或 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>

回答by AntouanK

One simple CSS solution.

一种简单的 CSS 解决方案。

http://jsfiddle.net/antouank/EttZQ/2/

http://jsfiddle.net/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.

您只需要计算元素的宽度/高度,并将 50% 更改为您需要将其居中的任何值。

回答by CodeToad

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.

使容器 pos 成为静态以外的任何东西。框:绝对位置。top:50% 当框高度改变时,设置框的顶部边距为-1 *高度/2。