体宽960px和2 divs一个固定左,右边

时间:2014-01-06 20:17:54

标签: html5 html css-position

我一直在各处搜索,但找不到一个好的解决方案。 所以我有两个div,相同的高度,将页面分成不相等的部分,一个较小的(“小”)和一个较大的(“较大”)。我希望他们都有position: fixed。 “较小”是可以的,我想要它但我不能将“Bigger”固定为右侧,车身宽度为960px 。当我放right: 0时,他将div放在身体宽度960px之外,这不是我想要的。

对此有何想法?

这是CSS:

.Bigger {
position: fixed;
margin: 0 auto;
top: 160px;
width: 700px;
height: 800px;
background-color: blue;
}

.Smaller {
position: fixed;
top: 160px;
margin: 0 auto;
width: 215px;
height: 800px;
background-color: blue;
}

1 个答案:

答案 0 :(得分:0)

所以我假设身体960px,你的意思,另外两个div的父div(内容包装器)是自动保证0自动保持居中。

只需使用绝对位置,它将div固定为父级。固定位置将div固定到窗口......这就是为什么它出现在父div之外。

例如,如果你的html就像

<div class="container">
   <div class="Bigger">
   </div>
   <div class="Smaller">
   </div>
</div>
你可以拥有css:

.container {
   position: relative;
   width: 960px; 
   margin: 0 auto;
 }

.Bigger {
position: absolute;  
top: 160px;
right: 0;
width: 700px;
height: 800px;
background-color: blue;
}

.Smaller {
position: absolute;
top: 160px;
left: 0;
width: 215px;
height: 800px;
background-color: blue;
}
相关问题