让div绝对居中显示的css&js

使DIV居中css代码,其中899px,507px分别改成自己的div的宽度和高度

.divname {
    width: 899px;
    height: 507px;
    position: absolute;
    left: expression(( body.clientWidth-899)/ 2 );
    top: expression(( body.clientHeight-507)/ 2 );
    background-image: url(images/login/login-bg.png);
    background-repeat: no-repeat;
}


js代码

function doMargin()
{
   var a = document.getElementById("divname");
   a.style.left=((document.body.clientWidth-divname.clientWidth)/2)+"px";
   a.style.top=(document.body.scrollTop+document.body.clientHeight/2-divname.clientHeight/2)+"px";
}


 

 

 

你可能感兴趣的:(css,function,url,div)