如何使div中的內容上下左右居中?

我需要一個網頁佈局,即上 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,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章