现在的位置: 首页 > 综合 > 正文

div在整个页面水平居中 垂直居中

2013年09月18日 ⁄ 综合 ⁄ 共 700字 ⁄ 字号 评论关闭

!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>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>div在整个页面水平居中 垂直居中</title>
<style type=”text/css”>
html,body { margin:0; padding:0; overflow:hidden; height:100%; }
#test { margin:0 auto; border:1px solid #666; background-color:#CCC; width:500px; }
</style>
<script type=”text/javascript”>
window.onload = function() {
document.getElementById(‘test’).style.marginTop = (document.getElementsByTagName(‘body’)[0].offsetHeight – document.getElementById(‘test’).offsetHeight) / 2 + ‘px’;
}
</script>
</head>

<body>
<div id=”test”><p>&nbsp;</p></div>
</body>
</html>

抱歉!评论已关闭.