如何让div中的内容在上下和左右都居中?

neighbornet 2012-10-04 10:19:40
我需要一个网页布局,即上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;}
...全文
2433 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
横向的居中 text-align:center 垂直的居中 可以 人为 定义好里面内容的高度然后 margin:auto
天使小宝 2012-10-07
  • 打赏
  • 举报
回复
楼主可以用以下的样式试试,可以自适应浏览器的大小。

<!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>

p2227 2012-10-06
  • 打赏
  • 举报
回复
mizuho_2006 2012-10-06
  • 打赏
  • 举报
回复
设置margin-left,margin-right,margin-top,margin-buttom为auto。

61,112

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧