我有绝对和相对定位的问题。
我试图将包含所有绝对div的相对DIV居中。我遇到的问题是当我试图将我的相对DIV居中时,我的绝对div“#mainForm”会缩小(高度问题)。
在下面的html中,如果删除“#main”类的位置和边距属性,您将看到页面布局正确显示。
如何在不影响绝对div的情况下将我的亲戚居中?
**我想要实现的是只有我的#mainForm是可滚动的。 我的sideBar,mainHeader和mainFooter必须“固定”。客户要求......
谢谢大卫
这是我的CSS和HTML。
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<style>
html {
box-sizing: border-box;
}
body {
font-family: Helvetica,Arial,sans-serif;
font-size: 8pt;
}
*, *:before, *:after {
box-sizing: inherit;
}
#main {
position: relative; /* if I removed this, page is not centered but mainForm height is ok */
margin: 0 auto; /* if I removed this, page is not centered but mainForm height is ok */
width: 960px;
}
#sideBar {
position: absolute;
top:0;
bottom:0;
left:0;
width: 180px;
}
#mainContent {
position: absolute;
top:0;
bottom:0;
right:0;
left:180px; /* Width of #sideBar. */
width: 780px;
}
#mainHeader {
position: absolute;
top:0;
height:40px;
width:100%; /* Mandatory. With is 100% of parent div. */
border: 1px solid blue; /* For developing purpose */
}
#mainForm {
position: absolute;
overflow:auto;
top:40px;
bottom:40px;
width:100%; /* Mandatory. With is 100% of parent div. */
border: 1px solid yellow; /* For developing purpose */
}
#mainFooter {
position: absolute;
bottom:0;
height:40px;
text-align:right;
width:100%; /* Mandatory. With is 100% of parent div. */
}
#topSideBar {
position: absolute;
top:0;
left:0;
background-image: url("../images/pas/contactLogo.png");
background-repeat: no-repeat;
height:110px;
width:100%; /* Mandatory. With is 100% of parent div. */
}
#middleSideBar {
position: absolute;
top:110px;
height:200px;
width:100%; /* Mandatory. With is 100% of parent div. */
}
#bottomSideBar {
position: absolute;
bottom:0;
height:100px;
width:100%; /* Mandatory. With is 100% of parent div. */
}
/* clearfix */
.clearFixaa:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
</style>
</head>
<body>
<div id="main" class="clearFix">
<div id="sideBar" >
<div id="topSideBar">
<!-- Contact Logo css backgound. -->
</div>
<div id="middleSideBar">
middleSideBar
</div>
<div id="bottomSideBar">
bottomSideBar
</div>
</div>
<div id="mainContent">
<div id="mainHeader">
mainHeader
</div>
<div id="mainForm">
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
mainForm
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
mainForm
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
mainForm
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
mainForm
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
mainForm
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div id="mainFooter">
mainFooter
</div>
</div>
</div>
</body>
答案 0 :(得分:0)
我修改了你的CSS。这将完成工作:
html {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 8pt;
}
*, *:before, *:after {
box-sizing: inherit;
}
#main {
position: fixed;
/* if I removed this, page is not centered but mainForm height is ok */
margin: 0 auto;
/* if I removed this, page is not centered but mainForm height is ok */
width: 960px;
height: 960px;
left: 0;
right: 0;
}
#sideBar {
position: absolute;
top:0;
bottom:0;
left:0;
width: 180px;
border: 1px solid red;
/* For developing purpose */
}
#mainContent {
position: absolute;
top:0;
bottom:0;
right:0;
left:180px;
/* Width of #sideBar. */
width: 780px;
height: 100%;
}
#mainHeader {
position: absolute;
top:0;
height:40px;
width:100%;
/* Mandatory. With is 100% of parent div. */
border: 1px solid blue;
/* For developing purpose */
}
#mainForm {
position: absolute;
overflow:auto;
top:40px;
bottom:40px;
width:100%;
/* Mandatory. With is 100% of parent div. */
border: 1px solid yellow;
/* For developing purpose */
}
#mainFooter {
position: absolute;
bottom:0;
height:40px;
text-align:right;
width:100%;
/* Mandatory. With is 100% of parent div. */
}
#topSideBar {
position: absolute;
top:0;
left:0;
background-image: url("../images/pas/contactLogo.png");
background-repeat: no-repeat;
height:110px;
width:100%;
/* Mandatory. With is 100% of parent div. */
}
#middleSideBar {
position: absolute;
top:110px;
height:200px;
width:100%;
/* Mandatory. With is 100% of parent div. */
}
#bottomSideBar {
position: absolute;
bottom:0;
height:100px;
width:100%;
/* Mandatory. With is 100% of parent div. */
}
/* clearfix */
.clearFixaa:after {
content:".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
诀窍是修复#main div并将其定位为left:0和right:0。我有一个主要div的静态高度,随意删除它,并添加你需要侧边栏的高度。