如何将元素定位在相对定位的容器中?

时间:2014-08-02 09:38:41

标签: html css

我想修复.side-menu元素的位置,但我对下面的代码没有成功。 .side-menu元素随.child-container内容移动。

为什么它不起作用以及如何使其工作? :)

HTML:

<div class="pageWrapper">
    <div class="container">
        <div class="side-menu">
            Menu content
        </div>
        <div class="child-container">
            Large Content
        </div>
    </div>
</div>

CSS:

body, html { 
    height: 100%; 
    width: 100%; 
    margin: 0; 
    padding: 0; 
}

.pageWrapper { 
    overflow-x: hidden;
    min-width: 1200px;
    height: 100%;
}

.container {
    position: relative;
    width: 1170px;
    margin: 0 auto;
    height: 100%;
}

.side-menu {
    position: fixed;
    width: 80px;
    height: 300px;
}

.child-container {
    position: relative;
    margin: 40px auto 0 auto;
    width: 900px;
}

1 个答案:

答案 0 :(得分:0)

我已使用问题的html更新了代码:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style type="text/css">

*
{
 margin: 0;
}

.container
{
  position: static;
  padding: 0px;
  width: 1200px;
  margin: 0 auto 0 auto;
}

.side-menu
{
  position: fixed;
  background: red;
  width: 200px;
}

.child-container
{
  background: orange;
  float: left;
  width: 1000px;
  margin-left: 200px;
}

</style>
<body>
<div class="pageWrapper">
    <div class="container">
        <div class="side-menu">
            Menu content
        </div>
        <div class="child-container">
            Large Content
        </div>
    </div>
</div>
</body>
</html>