【发布时间】:2009-03-07 23:41:51
【问题描述】:
我想要一个居中的标题 DIV,并在其中包含以下绝对定位的 DIV:
- 徽标
- 菜单
- 行
- 标题
但是我的 HTML/CSS 有两个问题:
- 由于某种原因,页面现在变宽了(见底部滚动条)
- 如果我的标题更长,我当然希望它是右对齐的
我真正想要的是一个居中的 DIV,并且我想在其中定位 DIV绝对在其居中的父级内(但不是绝对的,因为它们不会居中)。这可能吗?
你将如何完成这个布局?
alt text http://tanguay.info/web/external/centeredLayoutProblem.png
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style type="text/css">
#headerArea {
background-color: yellow;
margin: 0px auto;
width: 760px;
height: 150px;
}
#logo {
position: relative;
top: 20px;
left: 20px;
background-color: orange;
text-align: center;
font-size: 32pt;
width: 150px;
padding: 10px;
z-index: 10;
}
#menu {
position: relative;
top: -52px;
right: -480px;
background-color: tomato;
text-align: center;
font-size: 14pt;
width: 240px;
padding: 10px;
}
#title {
position: relative;
top: -35px;
right: -620px;
font-style: italic;
font-size: 14pt;
}
#line {
position: relative;
top: -60px;
border-bottom: 1px solid blue;
margin: 0 20px;
}
</style>
</head>
<body>
<div id="headerArea">
<div id="logo">LOGO</div>
<div id="menu">One Two Three Four Five</div>
<div id="title">This is the Title a Bit Longer</div>
<div id="line"></div>
</div>
</body>
</html>
【问题讨论】:
标签: css