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>
Related
I am new to CSS and I came across a coding question where I had to create three boxes one inside the other. Below is my code. As of now it only creates two boxes. What changes should I make in my code to include the third box.
#first {
width: 100px;
height: 100px;
background: red;
overflow: hidden;
}
#first #second {
position: relative;
width: 50%;
height: 50%;
margin: auto;
margin-top: 25%;
background: black;
}
#first #second #third {
position: relative;
width: 25%;
height: 25%;
margin: auto;
margin-top: 25%;
background: orange;
}
<body>
<div id="first">
<div id="second"></div>
<div id="third"></div>
</div>
</body>
#first {
width: 100px;
height: 100px;
background: red;
overflow: hidden;
}
#second {
position: relative;
width: 50px;
height: 50px;
margin: auto;
margin-top: 25px;
background: black;
overflow: hidden;
}
#third {
width: 12.5px;
height: 12.5px;
margin-top: 18.75px;
margin-left: 18.75px;
background: orange;
}
<body>
<div id="first">
<div id="second">
<div id="third">
</div>
</div>
</div>
</body>
You missed to place the #third div inside #second div. Try placing it inside the second one. Try this one.
<body>
<div id="first">
<div id="second">
<div id="third"></div>
</div>
</div>
</body>
It's not a matter of css rather html.
<div id="first">
<div id="second">
<div id="third"></div>
</div>
</div>
In the code above. Third is the child of Second and Second is child of First.
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>
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 a html markup like this
<div class="relative">
<div id="absolute">
<p>absolute content</p>
</div>
<p>Parent div</p>
</div>
<div>outer content</div>
and css is
.relative {
position: relative;
width: 600px;
height: 400px;
background: #ddd;
}
#absolute {
position: absolute;
top: 120px;
right: 0;
width: 300px;
height: 200px;
background: #eee;
}
Now for some reason I want to take out the child div (id=absolute) out of its parent (id=relative) while pushing down whatever content in below the parent div.
This is what I want to get,
any help is appreciated
Try this - http://jsfiddle.net/8eXEE/
HTML
<div class="relative">
<div id="absolute">
<p>absolute content</p>
</div>
<p>Parent div</p>
</div>
<div style="background-color:#ff0000; width:400px; height:100px; position: relative; top:200px;">outer content</div>
CSS
.relative {
position: relative;
width: 600px;
height: 400px;
background: #ddd;
}
#absolute {
position: absolute;
bottom: 0px;
left: 0px;
width: 300px;
height: 200px;
background: #eee;
margin-bottom: -200px;
}
If your #absolute divs height does not dynamically changes (i.e. remains 200px always)
The markup
<div class="relative">
<div id="absolute">
<p>absolute content</p>
</div>
<p>Parent div</p>
</div>
<div id="outer">outer content</div>
The CSS
.relative {
position: relative;
width: 600px;
height: 400px;
background: #ddd;
}
#absolute {
position: absolute;
top: 100%;
left: 0;
width: 300px;
height: 200px;
background: #eee;
}
#outer {
position:relative;
margin-top:200px;
background:blue;
}
Here is the plunker , you can play with it.
I need to expand parent div by child div, please look at this code:
HTML
<div class="wrapper">
<header class="header"> </header>
<div class="middle">
<div class="container">
<main class="content">
<div id="child">
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
</div>
</main>
</div>
<aside class="left-sidebar"></aside>
</div>
<footer class="footer"></footer>
</div>
CSS
.wrapper {
width: 500px;
margin: 0 auto;
min-height: 100%;
}
.header {
height: 100px;
background: blue;
}
.footer {
height: 60px;
width: 100%;
bottom: 0;
position: relative;
background:yellow;
clear:left;
}
.middle {
width: 100%;
min-height: 300px;
position: relative;
}
.container {
min-height: 300px;
width: 100%;
float: left;
}
.content {
width: 800;
min-height: 300px;
left: 280;
position: relative;
background:red;
padding:10px;
}
#child {
position:relative;
top:100px;
left:160px;
min-height:500px;
width: 200px;
border: solid 1px white;
background:green;
}
.left-sidebar {
float: left;
width: 100px;
min-height: 500px;
margin-left: -100%;
position: relative;
background: black;
}
DEMO: JSFIDDLE
the problem is that main.content is not fully expanded by #child vertically on the value of "top:200" that is in #child relative positioning properties, how can I fix it? because it currently overlaps the footer.
Actualy, when ou apply the "top" property with position: relative, it doesn't interfeer on the other elements of the page. so it will just overlap the parent div. Try using margin-top instead:
#child {
margin-top: 200px;
left:60;
min-height:500;
border: solid 1px white;
}
You need to clear:left; on the footer.
.footer {
clear:left;
}
http://jsfiddle.net/ac6s7/