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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了body高度100%怎么让div在body里面垂直居中相关的知识,希望对你有一定的参考价值。

参考技术A 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,htmlheight: 100%;
acolor: #333;
a:hovercolor: 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;
pmargin: 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>
参考技术B 先设置html及body的CSS的高,分别为height:100%
如下:
html,bodyheight:100%;margin:0px;

htmlheight:100%;
bodyheight:100%;margin:0px;
后面的高为100%才有效:
<table height="100%">

<div style="height:100%;">
参考技术C <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> 参考技术D <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>本回答被提问者和网友采纳

html,body设置了高度100%,body设置了overflow:hidden,但是body实际宽度和高度都要比浏览器大...

需要源码我可以发的
这是怎么回事啊,求告知,上面写不下了

这是因为里面的布局把body撑开了。追问

我设置了html,body的高度100%,又给body设置了overflow:hidden,body是撑不开的吧,最多body里面的div撑开

追答

你设置了百分之百,所以body的高度是由里面的子标签决定的,懂。

里面的子标签高度为1000body就为1000

追问

不是你说的问题,我body设置了overflow:hidden。我把同样的文件复制到另一个文件夹两个高度就相等了,莫名奇妙的

参考技术A 你body,html给个宽100%就可以了。追问

我那个时候就设置了的,后面我把同样的文件复制到另一个文件夹两个高度就相等了,莫名奇妙的

以上是关于body高度100%怎么让div在body里面垂直居中的主要内容,如果未能解决你的问题,请参考以下文章

关于html和body高度 登录页面登录框永远保持在页面垂直水平居中位置

怎么让一个不定宽高的div水平居中,垂直水平居中

html,body设置了高度100%,body设置了overflow:hidden,但是body实际宽度和高度都要比浏览器大...

html、body高度

如何让div滚动时页面不跟着滚动?

html怎么让div垂直和水平居中显示