body高度100%怎么让div在body里面垂直居中

如题所述

第1个回答  2012-06-13
1、DIV固定高度的垂直居中代码:

<style type="text/css">
<!--
div {
position:absolute;
top:50%;
left:50%;
margin:-150px 0 0 -200px;
width:400px;
height:300px;
border:1px solid #008800;
}
-->
</style>
<div>让层垂直居中于浏览器窗口</div>

2、DIV未知高度的垂直居中代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Vertical centering in valid CSS</title>
<style type="text/css">
body {padding: 0; margin: 0; font-size: 75%; line-height: 140%; font-family:Arial, Helvetica, sans-serif;}
body,html{height: 100%; }
a{color: #333;}
a:hover{color: green;}
#outer {height: 100%; overflow: hidden; position: relative;width: 100%; background:ivory; }
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;text-align:center;} /* for explorer only*/
#middle[id] {display: table-cell; vertical-align: middle; position: static;}
#inner {position: relative; top: -50%;width: 600px;margin: 0 auto;text-align:left;}/*for explorer only */
div.greenBorder {border: 1px solid green; background-color: #FFF;}
p{margin: 1em;}
</style>
<script type="text/javascript">
// <![CDATA[

function toggleContent(name,n) {
var i,t='',el = document.getElementById(name);
if (!el.origCont) el.origCont = el.innerHTML;

for (i=0;i<n;i++) t += el.origCont;
el.innerHTML = t;
}

// ]]>
</script>
</head>
<body>
<div id="outer">
<div id="middle">
<div id="inner" class="greenBorder">
<p><a href="javascript:toggleContent('inner',1)">默认长度</a> <a href="javascript:toggleContent('inner',2)">加长页面</a></p>
<p> 1.打开illustrator,新建一个文件,画个矩形,比你要导入的图片大一些,白色填充。 <br />
2.选中矩形,菜单:Effect > Distort & Transform > Zig Zag,设置如下图。 <br />
3.菜单:Effect > Stylize > Drop Shadow,设置如下图。 <br />
1.打开illustrator,新建一个文件,画个矩形,比你要导入的图片大一些,白色填充。 <br />
2.选中矩形,菜单:Effect > Distort & Transform > Zig Zag,设置如下图。 <br />
3.菜单:Effect > Stylize > Drop Shadow,设置如下图。</p>
</div>
</div>
</div>

</body>
</html>
第2个回答  2013-05-19
先设置html及body的CSS的高,分别为height:100%
如下:
html,body{height:100%;margin:0px;}

html{height:100%;}
body{height:100%;margin:0px;}
后面的高为100%才有效:
<table height="100%">

<div style="height:100%;">
第3个回答  2012-06-27
<style type="text/css"><!-- div { position:absolute; top:50%; left:50%; margin:-150px 0 0 -200px; width:400px; height:300px; border:1px solid #008800; }--></style><div>div 垂直水平居中</div>
第4个回答  2012-06-14
<body>
<div align="center">
把网站内容放这里,就会居中了
</div>
<\body>
第5个回答  2012-06-14
随滚动条移动的层 给你一个参考,你这个都了解明白了,那做起一来就很容易了.....

<style>
<!--
.div{
position: absolute;
border: 2px solid red;
background-color: #EFEFEF;
line-height:90px;
font-size:12px;
z-index:1000;
}
-->
</style>

<div id="Javascript.Div1" class="div" style="width: 240px; height:90px" align="center">正中...</div>
<SCRIPT LANGUAGE="JavaScript">
function sc1(){
document.getElementById("Javascript.Div1").style.top=(document.documentElement.scrollTop+(document.documentElement.clientHeight-document.getElementById("Javascript.Div1").offsetHeight)/2)+"px";
document.getElementById("Javascript.Div1").style.left=(document.documentElement.scrollLeft+(document.documentElement.clientWidth-document.getElementById("Javascript.Div1").offsetWidth)/2)+"px";
}
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
<!--
function scall(){
sc1();}
window.onscroll=scall;
window.onresize=scall;
window.onload=scall;
//-->
</SCRIPT>
<div style="position: absolute; top: 0px; left: 0px; width: 10000px; height: 4000px;"></div>本回答被提问者和网友采纳
相似回答