把div放在另一个div上

时间:2013-09-22 14:57:24

标签: html css

这是我想要的网站设计:Design

所以我在第一步,我的查询是如何将滑块div放在标题div上,我希望它在中心。我的代码我用过的是:

<div id="header">
<div class="slider">
</div>
</div>
<div class="content">
</div>
<div class="footer">
</div>


body {
    margin:0px;
    padding:0px;
    background:#fff;
}

#header
{
    background:#859685;
    height:300px;

}

.slider
{
    margin-left:auto;
    margin-right:auto;
    margin-top:50px;
    position:absolute;
z-index:1;
width:980px;
height:200px;
border: 4px #666 solid;  
}

.content
{
    margin-left: auto;
  margin-right: auto;
  margin-bottom:10px;
  margin-top:10px;
  width:980px;
  height:400px;
  background:#fff;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
}

.footer
{
    margin-top:10px;
    margin-bottom:10px;
padding: 0;
height:300px;
background:#98AFC7;
-moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    background:#111312;
}

这是我的小提琴jsfiddle.net/hdmep /

提前Thanx!

3 个答案:

答案 0 :(得分:1)

将滑块类更改为以下内容:

.slider
{
    margin-left:auto;
    margin-right:auto;      
    left:0;
    right:0;
    margin-top:250px;
    position:absolute;
    width:980px;
    height:200px;
    border: 4px #666 solid;  
}

答案 1 :(得分:0)

查看此Working Fiddle 我改变了一些尺寸(所以它在小提琴中看起来很漂亮)

这完全是关于绝对定位的。

如果您打算使用背景颜色,请使用background-color代替background 并且,请注意一次性使用margin的所有方面的简短方法。

<强> CSS:

#header
{
    background-color: #859685;
    height: 100px;
    position: relative; /*the slider is now relative to the header*/
}
.slider
{
    position:absolute;
    width: 80%; /*80% of header*/
    height: 50%; /* 50% of header*/
    border: 4px #666 solid;
    top: 70%;
    left: 10%;
}
.content
{
    margin: 10px auto;
    height: 100px;
    background-color: azure;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
}
.footer
{
    margin: 10px 0;
    height: 100px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    background-color: #111312;
}

答案 2 :(得分:0)

试试这个: DEMO

更新滑块类:

.slider {
    margin-left: auto;
    margin-right: auto;
    bottom: -150px;
    position: relative;
    z-index: 1;
    width: 980px;
    height: 200px;
    border: 4px #666 solid;
}