日期:2014-05-17  浏览次数:20608 次

如何让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和控件交互'/> &nbsp;<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;}

------解决方案--------------------
设置margin-left,margin-right,margin-top,margin-buttom为auto。
------解决方案--------------------
http://aliceui.com/vertical-horizoncal/
------解决方案--------------------
楼主可以用以下的样式试试,可以自适应浏览器的大小。
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>

------解决方案--------------------
横向的居中 text-align:center 垂直的居中 可以 人为 定义好里面内容的高度然后 margin:auto