Here's the (simplified) scenario. I have a div which contains another div. The outer div is of fixed size, has scrollbars and relative positioning. The inner div is larger than and is positioned relative to the outer div. I want the inner div to be fully visible, "floating" above the outer div.
I appreciate this sounds artificial as there is no point in constraining the size of the outer div in this way. It is. In the real application though, there is other content inside the outer div which does need to be controlled with scrollbars.
How can I do this, using css alone? Please note, the inner div must be positioned relative to the external one, so using position:fixed is not an option.
Markup:
<div id="container">
<div id="popup">Popup Text</div>
</div>
Css:
#container{
overflow: scroll;
height:50px;
width:50px;
position:relative;
}
#popup {
position:absolute;
top:20px;
border:1px solid #000;
height:100px;
width:100px;
}
Here's a Plunker
You can add extra wrapper and set position: relative and overflow to different wrappers:
HTML
<div id="base">
<div id="container">
<div id="popup">Popup Text</div>
</div>
</div>
CSS
#base {
height:50px;
width:50px;
position:relative;
}
#container{
overflow: scroll;
height:100%;
width:100%;
}
#popup {
position:absolute;
top:20px;
border:1px solid #000;
height:100px;
width:100px;
}
Fiddle: http://jsfiddle.net/UGftq/
The "secret" is that the overflow property
...affects the clipping of all of the element's content except any
descendant elements (and their respective content and descendants)
whose containing block is the viewport or an ancestor of the element.
The containing block for absolutely positioned elements is the nearest ascestor with non-static position. Therefore, when element with overflow is not positioned itself, it doesn't clip absolutely positioned descendants.
Related
All:
Thanks for help.
The html structure is like:
<div id="container" style="position:relative; top:100px; left:100px; width:200px; height:300px; overflow: auto;">
<div id="absoluteinner" style="position:absolute; top:10px; left:10px; width:100px; height:100px; background-color:red;">
</div>
<div id="staticinner" style="width:100px; height:800px; background-color:purple;">
</div>
</div>
I thought that absoluteinner can keep a fixed position relative to its container, but when I scroll the container, the absoluteinner moves as the staticinner. How can I make it position fixed?
Look at the jsfiddle I created for you. Just give it position fixed, if you don't set top and left explicit, it will inherit for the parent, making it fixed for the parent and not for the window. So look at the css I wrote.
Remember if you set explicit top and left offset it will be referenced to the window, but if you make this trick, inherit from the parent and instead of using top and left you use margin-top and margin-left you will have your element fixed to the parent element and the offset given by the margin.
http://jsfiddle.net/carloscalla/q6ffnm09/
body{
margin: 0;
}
#absoluteinner{
position: fixed;
margin-top: 10px;
margin-left: 10px;
top: inherit;
left: inherit;
}
NOTE: margin: 0; of body is just to correct the margin given by the jsfiddle.
UPDATE: What you want to achieve can not be done with the html structure you have right now. You will have to wrap up your container in another div and inside this div put your #absoluteinner so #absoluteinner and #container are siblings. Then you give the parent position: relative; , and your #absoluteinner position: absolute; , but #absoluteinner does not get into the #container scroll, they are separated but you simulate that they are one inside of the other one.
I created an extra #outter-container so you see another scroll and you see that #absoluteinner is "fixed" to #container.
If you use position fixed you will not be able to achieve this since position fixed takes the element out of flow, you can get that working for the basic scenario as the first answer I gave you but not for what you want to do if you want to insert this piece of html inside another containers.
Take a look at this jsfiddle I created for you: http://jsfiddle.net/carloscalla/gwphpfgy/4/
See the
<div id="container-wrapper" ...
this wraps up your container so you simulate a position fixed but this works inside other containers as well, as this sticks to the wrapper. Note the z-index: 1; just because it was going behind the purple box, you can avoid this by reordering the html structure but this is not a big deal.
I'm not quite sure wether i understood your question correctly, but I think, you can make its position:fixed and use margin to push it into the container.
Place absoluteinner inside of another position:absolute div and change absoluteinner'sposition to fixed. Then style the outer div with the position you want, relative to the parent, not the page. Do not put any position on the fixed element.
<div id="container" style="position:relative; width:200px; height:300px; overflow: auto;">
<div style="position:absolute; top:10px; left:10px; width: 100px;">
<div id="absoluteinner" style="position:fixed; width:100px; height:100px; background-color:red;"></div>
</div>
<div id="staticinner" style="width:100px; height:800px; background-color:purple;"></div>
</div>
JSFiddle
so here's my problem. I have two divs, and i want to be able to see both of them when i scroll down the page. Both divs are in the same parent container. If i give the parent a position:fixed, the the bottom div get's cut off, because you have to scroll to see it's full height. So i tried this. I gave position:fixed to the top div, and position relative to the bottom one. The fixed one now scrools but the relative doesn't "follow it" or stay beneath it.
Any ideas?
If I understand you correctly, the answer is to position both divs absolutely within the fixed parent.
JSFiddle DEMO
HTML
<div class="parent">
<div class="topDiv"></div>
<div class="bottomDiv"></div>
</div
CSS
.parent {
height:1500px;
width:200px;
border:1px solid grey;
position: fixed;
}
.topDiv {
display:block;
background:silver;
width:100px;
height:100px;
position:absolute;
top:0;
left:0
}
.bottomDiv {
background:red;
width:100px;
height:100px;
position:absolute;
top:130px;
left:0
}
I have a simple HTML page and it contains two divs aligned vertically. The page is scrollable because of second div. I want the first div's position to be fixed, or nonscrollable, so that only the second div is scrollable. I added position:fixed to first div's css but this time, the second div was placed on first div, so the first div disappears under the second div.
CSS
body {
width:1000px;
height:100%;
margin:0 auto;/*body ortalama*/
}
#div1 {
height:300px;
background-color:#00CC66;
}
#div2 {
display:block;
word-wrap:break-word;
padding:30px;
font-size:72px;
background-color:#FF3;
}
HTML
<div>
<div id="div1"></div>
<div id="div2">
<p>
<!--Content Here-->
</p>
</div>
</div>
Fixed is always relative to the parent window, never an element. Once the position is set to fixed its taken out of the document flow.
Fixed positioning is a subcategory of absolute positioning. The only difference is that for a fixed positioned box, the containing block is established by the viewport.
so in the second div2 add these
position:relative;
top:300px; /*Bump it down by the height of div1;*/
Hope it helps;
You should add a height and set overflow auto instead of scroll because with scroll you will have the scrollbar always even if the content is less than the specified height. For example:
#div2 {
background-color: #FFFF33;
display: block;
font-size: 72px;
height: 200px;
overflow: auto;
padding: 30px;
word-wrap: break-word;
}
Add this css to #div2 (you'll need to specify a height for #div2 otherwise the the scroll bar won't know where to start):
overflow-y:auto;
height:50px;
See the example here: http://jsfiddle.net/38xkn/1/ (scroll to the right first as you've set the body width to 100px, then you'll see the scroll bar for #div2).
Okay, here is another option. It's layout is somewhat different but it should get the job done. It uses absolute positioning on div1 to get it to the top, and a percentage width to stop it covering the scroll bar for div2. It's not perfect so you may need to tweek it slightly.
HTML
<body>
<div>
<div id="div1">a</div>
<div id="div2">
<p> SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSDDDDDDDDDLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLDDDDDDDDDDDDDDDDDDDDDDDDDDDDDAMSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSDDDDDDDDDLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLDDDDDDDDDDDDDDDDDDDDDDDDDDDDD</p>
</div>
</div>
</body>
CSS:
body{
width:100%;
height:100%;
margin:0 auto;/*body ortalama*/
overflow:hidden;
}
#div1{
height:300px;
background-color:#00CC66;
position:absolute;
top:0;
width:97.5%;
}
#div2{
display:block;
word-wrap:break-word;
padding:30px;
font-size:72px;
background-color:#FF3;
overflow-y:auto;
max-height:50px;
padding-top:300px;
}
EXAMPLE:
http://jsfiddle.net/38xkn/6/
I have a absolute positioned div with overflow auto. Inside this div is another absolute positioned div. My problem is that this child div gets cut off due to the overflow. I want it to escape the container div as it would if overflow was not set. I have tried setting z-indexes but it does not help. What can I do?
HTML
<div class="parent">
<div class="child"></div>
</div>
CSS
.parent {
position:absolute;
z-index:0
overflow:auto;
width:400px;
height:400px;
border:1px solid #000;
}
.child {
poisiton:absolute;
z-index:1
width:300px;
height:450px;
border:1px solid #f00;
}
See if you can rely on another method to clear your floats. Changing your CSS to overflow: visible is definitely a good solution.
Your other solution is to take the div outside of its container so it doesn't get cut off, and put them both inside of a new container:
<div class="container">
<div class="parent">
</div>
<div class="child">
</div>
</div>
CSS:
.container {
/* apply positioning from .parent */
}
.parent {
position: absolute;
top: 0;
left: 0;
}
.child {
/* apply positioning from .child */
}
If you want some elements to not overflow the parent and some elements to not, you'd be better off placing the current child div outside of the current parent. Just make it an absolutely positioned peer.
How do I align an element on the inside edge of a tag? That is, so that it works like absolute positioning but only inside the tag.
I think a way it would work is if it were possible to force the element to think the tag was the actual page, but I don't know if this is possible/ideal.
EDIT:
http://www.flickr.com/photos/chustar/3736370208/
here's a mockup of what i mean
What you need to do here is make the container element position:relative; and then make the "positioned" elements position:absolute;
.container {
position:relative;
}
.child1 {
position:absolute;
top:0;
left:0;
}
.child2 {
position:absolute;
bottom:0;
right:0;
}
<div class="container">
<div class="child1">Top left element</div>
<div class="child2">Bottom right element</div>
</div>
When you set the container element (a <div> for example) to "position: relative", and you set the inner element to "position: absolute", the inner element is positioned relative to the outer one, not the page.