1

How I can make a box to be fixed within a div with scroll?

I'm trying like this:

HTML:

<div id="wrapper">
    <div class="main">
        <div class="container">
            <div class="container2">
                <div class="test"></div>
                <div class="test"></div>
                <div class="test"></div>
            </div>
        </div>
    </div>
</div>

CSS:

#wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    color: #a3265e;
    font-family: 'GillSans-SemiBold';
}
.main {
    border: 1px solid red;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding-top: 380px;
}
.container {
    border: 1px solid green;
    position: relative;
    /*width: 946px;*/
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container2 {
    height: 1500px;
    margin: 0 auto;
}
.test {
  width: 500px;
  height: 500px;
  position: fixed;
  left: 50%;
  margin-left: -250px;
  background: black;
}

But the box is going along with the page, not only within the div.

What am i doing wrong here??? Can someone show me the way?

Thank you guys.


EDIT

Example -> https://jsfiddle.net/kzhuh7sv/embedded/result/

3
  • please make a jsfiddle Commented Apr 8, 2015 at 23:52
  • Of course! jsfiddle.net/kzhuh7sv/embedded/result Commented Apr 9, 2015 at 0:00
  • I'm not sure I understand exactly. Did you want the box to stay in the exact same spot when scrolling, or even when you scroll it stays there? Commented Apr 9, 2015 at 0:10

3 Answers 3

1

Try this solution https://jsfiddle.net/yyt8eope/2/ I added a div that wraps both the container div and the class='test' div at the same level so the test div can be absolute inside the wrapper and be always at a fixed position

<div id="wrapper">
    <div class="main">
        <div class="scroll-container">
          <div class="container">
            <div class="container2">
            </div>
          </div>
          <div class="test">Fixed inside scroll container</div>
        </div>
     </div>
</div>

CSS:

#wrapper {
position: relative;
width: 100%;
height: 100%;
color: #a3265e;
font-family: 'GillSans-SemiBold';
}
.main {
    border: 1px solid red;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding-top: 380px;
}
.scroll-container{
    position: relative;
    height: 500px;
}
.container {
    border: 1px solid green;
    position: relative;
    /*width: 946px;*/
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container2 {
    height: 1500px;
    margin: 0 auto;
}
.test {
  width: 500px;
  height: 200px;
  color: white;
  position: absolute;
  top:0;
  left: 50%;
  margin-left: -250px;
  background: black;
  z-index: 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works, one minor nitpick: test is rendered before container so any content will appear over the fixed div. If you move the test div after the container (or give it a z-index) it will properly flow over.
thanks a lot Halcyon you were right, the text was overlapping, I added the changes you suggested to my answer
0

Try getting rid of 'position: fixed;' and add this 'overflow: scroll;'.

JSFiddle.

EDIT

Changed the JSFiddle, has been updated.

2 Comments

Can you explain how this is a solution? The element is not fixed.
Sorry, @Halcyon. Just re-read the question. Is this better?
0

You can't do it with position: fixed since it always ties to the viewport. You want it fixed within it's context.

http://jsfiddle.net/zq1m49wf/2/

The black box stays in place as container3 scrolls

<div id="wrapper">
    <div class="main">
        <div class="container">
            <div class="container2">
                <div class="container3"></div>
            </div>
            <div class="test"></div>
        </div>
    </div>
</div>

#wrapper {
    position: relative;
    width: 100%;
    height: 100%;
}
.main {
    width: 100%;
    height: 100%;
}
.container {
    position: relative;
    height: 500px;
    padding-top: 200px;
}
.container2 {
    height: 500px;
    margin: 0 auto;
    overflow: scroll;
}
.container3 {
    height: 1500px;
}
.test {
    width: 500px;
    height: 500px;
    bottom: 0;
    background-color: #000000;
    position: absolute;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.