让div居中的方法

 var docWidth = document.body.offsetWidth;
       
// var docHeight = document.body.offsetHeight;
       
       
var divobj = document.getElementById("align");
       
var divWidth = divobj.offsetWidth;
       
// var divHeight = divobj.offsetHeight;
       
        
//让图片左右居中显示 
         var marginleft=(docWidth-divWidth)/2; 
         divobj.style.marginLeft = marginleft +"px"
        
        
//让图片上下居中显示 
         //var margintop=(docHeight-divHeight)/2; 
         //divobj.style.marginTop = margintop + "px";

你可能感兴趣的:(div居中)