I am trying to create fixed position div inside relative container. I am using bootstrap css framework. I am trying to create a fixed position cart. So whenever user scroll page it will show cart contents. but now problem is, it ran outside that container div.
This has to work in responsive mode.
Here my try:
<div class="wrapper">
<div class="container">
<div class="element">
fixed
</div>
</div>
</div>
CSS Code:
.wrapper {
width:100%
}
.container {
width:300px;
margin:0 auto;
height:500px;
background:#ccc;
}
.element {
background:#f2f2f2;
position:fixed;
width:50px;
height:70px;
top:50px;
right:0px;
border:1px solid #d6d6d6;
}
Screenshot:
This is how
position: fixed;
behaves:MDN link
Hence, to get what you want you have to use something more than fixed positioning:
Probably this: demo
The behavior of the positioning is mentioned in the AdityaSaxena's answer https://stackoverflow.com/a/18285591/5746301
For creating a fixed position cart, you can also do it with using the jquery.
If we apply the Left or right value or margin, we may face some issue while responsive.
In the below snippet, I have placed the fixed element at the right of the container.
Even if the width of the container increased the fixed element placed accordingly.
Here is the jsfiddle Demo URL
Hope this may help you.
Thanks
I found the answer to that :
You want that class=inContainer are in class=Container in fixed position but if you scroll with the navigator scroll you don't want that the class=inContainer move outside the class=container
So you can make something like that
So class=inContainer will be always on the top of you're class=Container and move with you're class=container if you scroll with navigator scroll =)
(tested only with chrome)
No it's impossible because fixed property throws the element out of the flow so it doesn't depend to anything on the document and yes it is no more contained in your container : )
Make the element's parent container have
position: relative
Instead of using
top
orleft
usemargin-top
and/ormargin-left
If you only use
top
that will position the element based on the window, but if you usemargin-top
that will position based on the parent element. Same goes forleft
orright
If you are looking to show the cart even when the user scrolls that is fixed then you should use
position:fixed
for the cart (if.container
is your cart), because it should be shown with respect to screen/viewport. Your current code will only show theelement
which is positioned absolutely inside thecontainer
. If you want it to be like that then give :