让块级元素水平垂直居中
Posted To be better
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了让块级元素水平垂直居中相关的知识,希望对你有一定的参考价值。
function fun(){
var wWidth = null,
wHeight = null; if(window.innerHeight && window.innerWidth){ wHeight = window.innerHeight;
wWidth = window.innerWidth; }else if((document.body) && (document.body.clientHeight) && (document.body.clientWidth)){
wWidth = window.clientWidth; wHeight = window.clientHeight; }
var boxWidth = document.getElementById("box").clientWidth; var boxHeight = document.getElementById("box").clientHeight;
box.style.marginLeft = (wWidth - boxWidth)/2 + "px"; box.style.marginTop = (wHeight - boxHeight)/2 + "px"; }
window.onload = window.onresize = function(){fun()}
以上是关于让块级元素水平垂直居中的主要内容,如果未能解决你的问题,请参考以下文章