I have two sections, the 1st section is using position relative and contains 2 absolute children inside with the children being overlayed. The 2nd section contains a title.
I would like to keep the 1st section with position relative in flow so the 2nd section appears below. I understand position absolute takes elements outside of the document flow but is this the case even with a relative parent?
How can i keep the parent in flow?
.parent {
position: relative;
}
.child {
color: white;
position: absolute;
width: 100%;
}
.child1 {
background-color: red;
z-index: 1;
}
.child2 {
background-color: blue;
z-index: 0;
}
<div class="parent">
<div class="child child1">block1</div>
<div class="child child2">block2</div>
</div>
<div class="text">
<h1>block below</h1>
</div>
You just need to set the parent element dimensions, so its children can take place. As the absolute-positioned children are taken off the regular flow, it means that the parent div doesn't contain anything, thus it "disappears". I.e.:
.parent {
position: relative;
width: 200px;
height: 200px;
background-color: Wheat;
}
And the snippet:
.parent {
position: relative;
width: 200px;
height: 200px;
background-color: Wheat;
}
.child {
color: white;
position: absolute;
width: 100%;
}
.child1 {
background-color: red;
z-index: 1;
}
.child2 {
background-color: blue;
z-index: 0;
}
<div class="parent">
<div class="child child1">block1</div>
<div class="child child2">block2</div>
</div>
<div class="text">
<h1>block below</h1>
</div>
If you need the parent div not to exit the normal flow, then it should be static:
.child {
color: white;
position: relative;
width: 100%;
}
.child1 {
background-color: red;
z-index: 1;
position: absolute;
}
.child2 {
background-color: blue;
z-index: 0;
}
<div class="parent">
<div class="child child1">block1</div>
<div class="child child2">block2</div>
</div>
<div class="text">
<h1>block below</h1>
</div>
Related
The problem is i will stack html elements over each other but, in the div where i do this is the height always zero. And the html elements behind them are under them.
.parent {
position: relative;
}
.child {
height: auto;
width: auto;
position: absolute;
}
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
</div>
<p>under the other</p>
And now the height of the parent object should adapt to the children.
"And now the height of the parent object should adapt to the children."
That's what you'd expect from tags in normal flow, but the parent and children are position: relative and absolute so behavior is totally different
In the example is the OP code with outlines and another copy of OP but they have position: static, which is default.
.parent {
position: relative;
outline: green 5px dotted;
}
.child {
height: auto;
width: auto;
position: absolute;
}
.child:first-of-type {
outline: dashed 3px red;
color: red;
}
.child:last-of-type {
outline: dashed 3px blue;
color: blue;
}
.x {
position: static
}
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
</div>
<p>under the other</p>
<hr>
<div class="parent x">
<div class="child x">1</div>
<div class="child x">2</div>
</div>
<p>under the other</p>
I have a flexbox wrapper which has two descendants. They both have dynamic height. The second block could be higher than the first one, and I would like to limit the height of the second block to the same as the height of the first one.
.wrapper {
border: 1px solid black;
display: flex;
width: 400px;
margin-bottom: 100px;
}
.left {
background-color: blue;
height: 100px;
width: 200px;
}
.right {
width: 200px;
}
.first {
height: 70px;
background-color: yellow;
}
.second {
height: 70px;
background-color: green;
}
/*desired result */
.fixed-height {
height: 100px;
}
.overflow-value {
overflow: auto;
}
<div class="wrapper">
<div class="left"></div>
<div class="right">
<div class="first"></div>
<div class="second"></div>
</div>
</div>
<div class="wrapper fixed-height">
<div class="left"></div>
<div class="right overflow-value">
<div class="first"></div>
<div class="second"></div>
</div>
</div>
In the provided example, there two wrappers: the first one is the current wrapper, where the wrapper has the height of the tallest child. And the second one is the desired result (I added height to the wrapper, but I couldn't do it in real application)
CodePen Example
If you insist on using flexbox then there is a way to force the container to just take the height of specific child into account - this can be done by forcing the contents of the second item out of layout context with position: absolute. Unfortunately, this requires adding another wrapper inside the .right element. In addition, having the items positioned absolutely inside the second item will mean that the width of the contents will not be propagated to the .right element, but since your example has an explicit width set, then it works in this case. The code with those modifications is below:
.wrapper {
border: 1px solid black;
display: flex;
width: 400px;
margin-bottom: 100px;
}
.left {
background-color: blue;
height: 100px;
width: 200px;
}
.right {
width: 200px;
position: relative;
overflow: auto;
}
.first {
height: 70px;
background-color: yellow;
}
.second {
height: 70px;
background-color: green;
}
.right-wrapper {
position: absolute;
width: 100%;
}
<div class="wrapper">
<div class="left"></div>
<div class="right">
<div class="right-wrapper">
<div class="first"></div>
<div class="second"></div>
</div>
</div>
</div>
Is it possible to resume normal HTML after an absolutely positioned div element? I'm trying to use particle.js background in header div on top of which is child element (with some text only). Then from the end of parent div (particle-js div), I'd like normal HTML flow.
#parent {
position: absolute;
height: 200px;
width: 200px;
background-color: red;
}
#child {
position: relative;
left: 50px;
top: 50px;
height: 50px;
width: 50px;
background-color: green;
}
#after-parent {
background-color: blue;
}
<div id="parent">
<p>Parent div</p>
</div>
<div id="child">
<p>child div</p>
</div>
<div id="after-parent">
<h1>Normal HTML flow after parent div</h1>
</div>
You can wrap #parent and child inside another relative div e give it the height https://jsfiddle.net/f6phun8a/2/
#parent {
position: absolute;
height: 200px;
width: 200px;
background-color: red;
}
#outer {
position: relative;
height: 200px;
}
#child {
position: relative;
left: 50px;
top: 50px;
height: 50px;
width: 50px;
background-color: green;
}
#after-parent {
background-color: blue;
}
<div id="outer">
<div id="parent">
<p>Parent div</p>
</div>
<div id="child">
<p>child div</p>
</div>
</div>
<div id="after-parent">
<h1>Normal HTML flow after parent div</h1>
</div>
#flexwrap {
display:flex;
flex-direction:column;
}
#parent {
flex:0 0 200px;
position: absolute;
height: 200px;
width: 200px;
background-color: red;
}
#child {
flex:1;
position: relative;
margin-top: 200px;
margin-left: 50px;
height: 50px;
width: 50px;
background-color: green;
}
#after-parent {
flex:1;
background-color: blue;
}
<div id="flexwrap">
<div id="parent">
<p>Parent div</p>
</div>
<div id="child">
<p>child div</p>
</div>
<div id="after-parent">
<h1>Normal HTML flow after parent div</h1>
</div>
</div>
I'm having troubles positioning my divs. I want to have my child div stick to the bottom of the parent div, with grandchild_1 and grandchild_2 staying correctly put. By that, I mean having grandchild_1 before grandchild_2, like on the picture.
This is what I've tried, but the "child" div sticks to the top :
#parent {
position: relative;
}
#child {
position: absolute; bottom: 0;
}
<div id="parent">
<div id="child">
<div id="grandchild_1">
</div>
<div id="grandchild_2">
</div>
</div>
</div>
Anyone knows how I should proceed ? Thanks !
If you specify a height on the parent it will stick to the bottom.
Example: http://codepen.io/anon/pen/wGqzVd
HTML
<div id="parent">
Parent
<div id="child">
Child
<div id="grandchild_1">
Grandchild 1
</div>
<div id="grandchild_2">
Grandchild 2
</div>
</div>
</div>
CSS
div {
padding: 5px;
}
#parent {
position: relative;
background: lightgray;
height: 200px;
width: 150px;
}
#child {
position: absolute;
bottom: 5px;
background: yellow;
}
#grandchild_1 {
background: pink;
}
#grandchild_2 {
background: lightblue;
}
The provided code works as is...assuming that the parent has a height greater than that of the child.
#parent {
position: relative;
height: 200px;
background: pink;
}
#child {
position: absolute;
width: 100%;
bottom: 0;
background: green;
}
#grandchild_1,
#grandchild_2 {
height: 25px;
background: red;
margin: 10px;
}
<div id="parent">
<div id="child">
<div id="grandchild_1">GC1
</div>
<div id="grandchild_2">GC2
</div>
</div>
</div>
As an alternative to positioning, flexbox can do the same...and the child will affect the height of the parent which an absolutely positioned child cannot.
#parent {
position: relative;
height: 200px;
background: pink;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
#child {
width: 100%;
background: green;
}
#grandchild_1,
#grandchild_2 {
height: 25px;
background: red;
margin: 10px;
}
<div id="parent">
<div id="child">
<div id="grandchild_1">GC1
</div>
<div id="grandchild_2">GC2
</div>
</div>
</div>
I have 2 divs inside a parent:
<div class="parent">
<div class="foo1"></div>
<div class="foo2"></div>
</div>
foo1 will have a dynamic height, so I can't use the style below:
height: calc(100% - foo1Height);
Now, what I want to do is make sure that the lower child foo2 never expands outside of the parent div, and to show the scrollbar if it gets too big. I would prefer CSS only solutions.
You can either use flexbox. no markup changes.
.parent {
display: flex;
flex-direction: column;
height: 100px;
}
.foo2 {
flex: 1; /*expand to fit*/
background: silver;
overflow: auto; /*scroll as needed*/
}
<div class="parent">
<div class="foo1">1</div>
<div class="foo2">2<br>2<br>2<br>2<br>2<br>2<br>2<br>2</div>
</div>
Or use CSS table, additional markup is required.
.parent {
display: table;
width: 100%;
height: 100px;
}
.foo1, .foo2 {
display: table-row;
}
.cell {
display: table-cell;
position: relative;
}
.foo2 {
height: 100%; /*expand to fit*/
background: silver;
}
.scroll {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: auto; /*scroll as needed*/
}
<div class="parent">
<div class="foo1">
<div class="cell">1</div>
</div>
<div class="foo2">
<div class="cell">
<div class="scroll">2<br>2<br>2<br>2<br>2<br>2<br>2<br>2</div>
</div>
</div>
</div>