简体   繁体   中英

How to position element as fixed in relatively positioned container?

I want to make .side-menu element's position fixed, but I have no success with the code below. .side-menu element moves with .child-container content.

Why it doesn't work and how to make it work? :)

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;
}

I have updated the code with the html of the question:

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM