Fixed position div inside pos:relative & overflow-y:scroll - html

I want to have a div with fixed position inside a div with overflow-y:scroll, meaning I want the div to stay in place while the rest of the content scrolls normally.
And I can't figure out what is wrong, could anyone help? thanks in advance...
.foo {
position:relative;
display:block;
width:100%;
height:300px;
overflow-y:scroll;
}
.bar {
position:fixed;
top:0;
right:0;
}
And here is the HTML
<div class="foo">
<div class="bar"><div><!-- end of div that should be fixed -->
<div class="someOther">...</div>
<div class="someOther">...</div>
<div class="someOther">...</div>
<div class="someOther">...</div>
</div><!-- end of container -->

When you apply position:fixed to an element, you are positioning it in relation to the window itself, not its parent element. You'll want to use position:absolute to position a child in relation to its parent, as long as the parent has a position other than position:static, the default position.
As you correctly did in your example, apply position:relative to the parent .foo and then apply position:absolute to the child .bar. Normally, this would achieve the desired result of snapping the .bar to the top of the parent, but since there is an overflow of child content in the parent div, and overflow-y:scroll scrolls all the child content, .bar has to scroll as well. See the top example in my Fiddle here.
To fix that, wrap the content you want to scroll in another container with overflow-y:scroll on and remove overflow-y:scroll on .foo to prevent .bar from scrolling.
To see the working example that you can adapt, see the bottom example in my Fiddle here.

A fixed elements position is relative to the entire document you are viewing, not whatever the parent element is. If you want that to work, you'd need something like this:
CSS
.foo {
height : 300px;
position : relative;
width : 100%;
}
.bar {
border-bottom : 1px solid #000;
height : 50px;
}
.scollable_content {
height : 250px;
overflow-y : auto;
}
HTML
<div class="foo">
<div class="bar"></div>
<div class="scrollable_content">
<div class="someOther">...</div>
<div class="someOther">...</div>
<div class="someOther">...</div>
<div class="someOther">...</div>
</div>
</div>
Here, I created a fiddle for you.

Related

Two Absolute child divs inside relative parent div are hidden with overflow:hidden, expected to hover over eachother

When I use float, childs pop out of parents.
I just put overflow:hidden on the parent and the child pops back into its parent.
However, I can't do the same with an absolute div and a relative div.
.parent {
position: relative;
overflow: hidden;
}
.child {
position: absolute;
}
<div class="parent">
<div class="child">First</div>
<div class="child">Second</div>
</div>
The goal is to float 2 images one over another to create a slideshow, but still make the page respect the slideshow as an item.
Expected: "First" hovers above "Second" inside parent
Behavior: "First" and "second" are hidden, parent is 0px in height.
Upon removing overflow:hidden;, they appear outside the parent.
Since you have only absolute div inside a relevant parent div there is effectively no content in the parent div. You can set a preferred height to the parent div but also need to set html and body height to 100%.
Note: You would likely set your parent div to the size of your images to be displayed
I have colored the parent black and the children red for visual point of view.
Is this what you are trying to achieve? Sorry if I have miss understood your question.
html, body {
height:100%;
width:100%;
}
.parent {
position: relative;
height:50%;
width:50%;
background-color:Black;
}
.child {
position: absolute;
background-color:red;
width:20%;
height:20%;
}
<div class="parent">
<div class="child">First</div>
<div class="child">Second</div>
</div>

Expand absolute parent div to height of relative child

I need to animate my ng-view with a slide effect. Therefore I got 3 divs
<div style="overflow:hidden">
<div ng-view style="position:absolute">
<div style="position:relative"> LONG CONTENT </div>
</div>
</div>
I´m testing these effect. The outer div needs to be overflow:hidden to let my slide effekt work.The inner div needs to be absolute. If the inner div contains some text, the other 2 divs should expand the height according to the very inner div. Same like when all divs would be relative. How to achieve this?
Like in my example link but with variable height.
EDIT: This is a complete other question then the "possible" duplicate.
The problem is the size of your contents, you`re making your inner div absolute, so the parents it won’t
have any height. First thing you have to do is define size to your elements and than you can use absolute elements to make your layout.
see the example below, i define a div named .page with min-height:200px, that will be enough to appear your element (.slide), because if you don’t do this, your element(.page) height will be 0, and it will be cut by overflow:hidden.
<style>
.page {
background-color: #DDD;
overflow: hidden;
position: relative;
min-height: 200px;
}
.slider{
position: absolute;
}
.slide{
position: relative;
}
</style>
<div class="page">
<div class="slider" ng-view>
<div class="slide"> LONG CONTENT </div>
</div>
</div>

Div content overlapping each other

This problem is related to how i can show div with absolute position regarding the div, Not the page.
I have div#Div_Report, Which has multiple page div#page1, div#page2,..., And each page div has content with same css.
My problem is if i give absolute position to page's div.Div_ClientName, Then div#page2 data is overlapping to div#page1.
I know this can easily fix by relative position, But due to some reason i cant use relative position.
So how we can set page content with in page div with absolute position?
Here is my HTML:-
<div id="Div_Report">
<div id="page1">
<div class="Div_ClientName">Childrens Network - Hendry Glades</div>
</div>
<div id="page2">
<div class="Div_ClientName">Childrens Network - Hendry Glades</div>
</div>
</div>
CSS:-
.Div_ClientName{
top:180px;
}
.Div_ClientName{
position:absolute;
}
.Div_ClientName{
left: 175px;
}
#page2{
margin-top:200px
}
Fiddle Link
Try this CSS:
#page1, #page2 {
position:relative;
float:left;
}
... it isn’t, by any means, a nice solution ... but it solves the problem with things overlapping.

How can I get a relative div to expand to fit its absolute contents?

I'd prefer to do this using only CSS.
I have a relative element which contains an absolute element. I want the relative element to be sized based on how big the absolute element is, so in other words it should wrap neatly around it. To illustrate, in this fiddle, "footer" is positioned underneath "header-wrapper", but it overlaps its contents because "header-wrapper" is ignoring its absolute contents: http://jsfiddle.net/cxmjdL78/1/
HTML
<div class="wrapper">
<div class="header-wrapper">
<div class="header-1">HEADER HEADER HEADER</div>
<div class="header-2">HEADER HEADER<br>HEADER HEADER</div>
</div>
<div class="footer">this text should go below the header</div>
</div>
CSS
.wrapper {
position:relative;
width:300px;
height:300px;
}
.header-wrapper {
position:relative;
}
.header-1 {
position:absolute;
background:#232323;
width:100%;
height:auto;
opacity:0.4;
}
.header-2 {
position:absolute;
background:#323232;
width:100%;
height:auto;
opacity:0.4;
}
.footer {
position:relative;
background:#26d452;
opacity:0.4;
}
When you use position:absolute on an element, you "take it out of the flow". You can't get a size based on a absolutely positioned element as far as I know. You have to set the height on your header-wrapper, or use javascript to achieve the effect.
Why do you have to use position absolute?
For a CSS only solution, the only thing I can think of is to add a height to your header-wrapper. If the content in your div is static, this will solve your problem, but if it's dynamic, you're going to be forced into some kind of JavaScript solution.

Absolute positioned div does not show outside the relative positioned div

Please look at the following example
http://jsfiddle.net/GANeX/90/
I want to show my green colored div outside the wrapper. I cannot change the positioning of the wrapper div as inner-wrapper also holds some other content which may come out when we change the position:relative to position:static.
How can I do this?
You can wrap the things you don't want to overflow in an element with a class called dont-overflow. Set the width of that class to the current .wrapper width, and then remove the overflow from the parent and add it to that class:
CSS dont-overflow class:
.dont-overflow
{
overflow-x: hidden;
overflow-y: scroll;
width: 200px;
height: 200px;
}
HTML:
<div class='wrapper'>
<div class='inner-wrapper'>
<div class='content-wrapper'></div>
<div class='dont-overflow'>
<div class='content-wrapper'></div>
<div class='content-wrapper'></div>
<div class='content-wrapper'></div>
</div>
</div>
</div>
http://jsfiddle.net/GANeX/97/
I think you need to use z-index.
.inner-wrapper{
position:absolute;
left:10px;
right:10px;
top:5px;
bottom:5px;
z-index: 999;
background:yellow;}