我需要一个网页布局,即上 div , 左div,右div,下div,如下面的设计,可是我希望在右div内的内容,在整个右div内都居中 ,应该怎样才能实现呢?下面是我自己的程序代码细节:
index.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=gb2312" />
<title>2列左侧固定右侧自适应宽度+头部+尾部</title>
<link href="layout.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div id="container">
<div id="header">This is the Header</div>
<div id="mainContent">
<div id="sidebar">This is the sidebar</div>

<!--如何让下面的div内容左右和上下都居中?-->
<div id="content" >2列左侧固定右侧自适应宽度+头部+尾部 <br />
<input type='button' onclick='MyDemo.TextString="用js和控件交互"; MyDemo.ShowMessage()' value='用js和控件交互'/> <p />

<object id="MyDemo" classid="clsid:D992A002-789B-4094-A846-2BF9DB9DB5C8" width="212" height="104" codebase="setup.exe">

<param name="TextString" value="用param传递控件属性"/>
</object>
</div>
<!--上面的内容上下和左右都居中-->

</div>
<div id="footer">This is the footer</div>
</div>
</body>
</html>

layout.css:
body { font-family:Verdana; font-size:14px; margin:0;}

#container {margin:0 auto; width:100%;}
#header { height:100px; background:#9c6; margin-bottom:5px;}
#mainContent { height:500px; margin-bottom:5px;}
#sidebar { float:left; width:200px; height:500px; background:#cf9;}
#content { margin-left:205px !important; margin-left:202px; height:500px; background:#ffa;}
#footer { height:60px; background:#9c6;}

后来又找到了一个以下的样式,可以自适应浏览器的大小,效果还不错,如下面所示的代码细节:
HTML code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
.right{
position: absolute;
top: 50px;
right: 0px;
bottom: 50px;
width: 300px;
text-align: center;
border: 1px solid blue;
}
.rightContent{
position: absolute;
top: 50%;
bottom: 50%;
height: 20px;
}
</style>
</head>
<body>
<div class="right">
<span class="rightContent">右DIV显示的内容</span>
</div>
</body>
</html>

此文由Web开发之答疑解惑源www.znjcx.com整理,若需转载,请注明原文出处:http://www.znjcx.com/html/y2012/3608_how-to-center-the-div-content-above-and-below.html,谢谢!