87,917
社区成员
发帖
与我相关
我的任务
分享
<!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" />
<meta name="author" content="Chomo" />
<link rel="start" href="http://www.14px.com" title="Home" />
<title>根据内容宽度、高度自适应,垂直水平居中,内容高度超过窗体时,垂直居顶</title>
<style type="text/css">
* { margin:0; padding:0; list-style:none; font-size:14px;}/*---该css reset仅用于demo,请自行换成适合您自己的css reset---*/
html { height:100%;}
body { height:100%; text-align:center;}
.centerDiv { display:inline-block; zoom:1; *display:inline; vertical-align:middle; text-align:left; width:200px; padding:10px; border:1px solid #f60; background:#fc0;}
.hiddenDiv { height:100%; overflow:hidden; display:inline-block; width:1px; overflow:hidden; margin-left:-1px; zoom:1; *display:inline; *margin-top:-1px; _margin-top:0; vertical-align:middle;}
</style>
</head>
<body>
<div class="centerDiv">
蓝色理想<br/>
</div><div class="hiddenDiv"></div>
</body>
</html>
<!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>
</head>
<body>
<div style="width:100px; height:100px; left:50%; top:50%; margin-left:-50px; margin-top:-50px; position:absolute; background-color:red;"></div>
</body>
</html>
<!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>无标题文档</title>
<script type="text/javascript">
function returnStyle(obj,styleName){
var myObj = typeof obj == "string" ? document.getElementById(obj) : obj;
if(document.all){
return eval("myObj.currentStyle." + styleName);
} else {
return eval("document.defaultView.getComputedStyle(myObj,null)." + styleName);
}
}
window.onload = function(){
var page = document.documentElement.clientHeight;
var div = document.getElementById('test');
var height = parseInt(returnStyle(div,'height'));
div.style.top = ((page-height)/2) + 'px';
};
</script>
<style type="text/css">
#test {
height:100px;
width:100px;
background-color:#eee;
position:absolute;
}
</style>
</head>
<body>
<div id="test"></div>
</body>
</html>