日期:2014-05-16  浏览次数:20308 次

div如何定位于IE窗口或是屏幕的正中
div块如何定位于IE窗口或是屏幕的正中
请用最少的代码说明一下

------解决方案--------------------
div绝对定位
其left为 htmlWidth/2 - divWidth/2
其top为 htmlHeight/2 - divHeight/2
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>pos center </title>
    <style type="text/css" >
    body{ position:relative;}
    #div1{
     position:absolute;
     border:1px solid red;
    }
    </style>
</head>
<body>
<div id="div1">some text hello world<br />
some text
</div>

<input type="button" value="DIV定位正中" style="position:absolute;left:300px;" onclick="getwidthheight()" />
<script type="text/javascript">
    var div1 = document.getElementById("div1");
    function getwidthheight() {
        var windowwidth = document.documentElement.clientWidth;
        var windowheight = document.documentElement.clientHeight;
        var divwidth = div1.clientWidth;
        var divheight = div1.clientHeight;
        div1.style.left = (windowwidth - divwidth) / 2 + "px";
        div1.style.top = (windowheight - divheight) / 2 + "px";
     }
</script>
</body>
</html>