Absolutely postioned div moving with margin top of another static postioned div - html

I have a body containing two div's one is an absolutely positioned div and another one is a static default positioned div, i want the absolutely positioned div to take the full height of the screen which it takes but the problem that next arises is when i try to apply margin top to the statically positioned div, it also gets added to the absolutely positioned div.
How can I make the absolutely positioned div not get the margin of the sibling div ?
body {
font-family: sans-serif;
margin: 0;
padding: 0;
position: relative;
}
.div-1 {
position: absolute;
border: 2px solid red;
width: 90%;
left: 0;
right: 0;
margin: 0 auto;
height: 100vh;
}
.div-2 {
height: 200px;
width: 90%;
background-color: blueviolet;
margin-top: 8rem;
}
<div class="div-1"></div>
<div class="div-2"></div>

The issue is that you have margin collapse on the body element. Margin collapse happens when there's no content separating parent and descendants elements (such as the body and .div-2). You can easily fix this by setting the display property of the body element to flow-root.
body {
font-family: sans-serif;
margin: 0;
padding: 0;
position: relative;
/* Set flow-root */
display: flow-root;
}
.div-1 {
position: absolute;
border: 2px solid red;
width: 90%;
left: 0;
right: 0;
margin: 0 auto;
height: 100vh;
}
.div-2 {
height: 200px;
width: 90%;
background-color: blueviolet;
margin-top: 8rem;
}
<div class="div-1"></div>
<div class="div-2"></div>

body {
font-family: sans-serif;
margin: 0;
padding: 0;
position: relative;
}
.div-1 {
position: absolute;
border: 2px solid red;
width: 90%;
left: 0;
right: 0;
margin: 0 auto;
height: 100vh;
z-index:1;
}
.div-2 {
height: 200px;
width: 90%;
background-color: blueviolet;
top: 8rem;
position: inherit;
}
Use top and position inherit instead of margin-top, check if it can be use.

Related

Why top:0 with position:absolute not working if another element has margin-top here? [duplicate]

This question already has answers here:
Why does this CSS margin-top style not work?
(14 answers)
Closed 5 years ago.
Why top:0 with position:absolute not working here?
I want to mention that in this condition I don't have control on any other element other than .heatmap
body {
position: relative;
margin: 0;
padding: 0
}
.section1 {
margin-top: 107px;
border: 1px solid green
}
.heatmap {
z-index: 2147483642;
top: 0px;
left: 0px;
position: absolute;
width: 1425px;
height: 1110px;
opacity: 0.7;
background: red
}
<div class="section1">something</div>
<div class="heatmap">hamara heatmap</div>
You have encountered collapsing margins.
heatmap is positioned with respect to the nearest ancestor which has position that is not static. This is the body element.
The first child of the body has a margin-top.
That margin collapses through the top of the body and pushes the body element down away from the viewport edge.
You can see this by applying an outline to the body element.
body {
position: relative;
margin: 0;
padding: 0;
outline: solid pink 10px;
}
.section1 {
margin-top: 107px;
border: 1px solid green
}
.heatmap {
z-index: 2147483642;
top: 0px;
left: 0px;
position: absolute;
width: 1425px;
height: 1110px;
opacity: 0.7;
background: red
}
<div class="section1">something</div>
<div class="heatmap">hamara heatmap</div>
To avoid this, prevent the margins from collapsing. This is most easily done by using padding on the body instead of margin on the heatmap.
body {
position: relative;
margin: 0;
padding: 107px 0 0 0;
outline: solid pink 10px;
}
.section1 {
border: 1px solid green
}
.heatmap {
z-index: 2147483642;
top: 0px;
left: 0px;
position: absolute;
width: 1425px;
height: 1110px;
opacity: 0.7;
background: red
}
<div class="section1">something</div>
<div class="heatmap">hamara heatmap</div>
You just remove position: relative from body, it will work
body {
margin: 0;
padding: 0
}
.section1 {
margin-top: 107px;
border: 1px solid green
}
.heatmap {
z-index: 2147483642;
top: 0px;
left: 0px;
position: absolute;
width: 1425px;
height: 1110px;
opacity: 0.7;
background: red
}
<div class="section1">something</div>
<div class="heatmap">hamara heatmap</div>
Just give padding-top: 1px; to body and it will work fine;
The problem was margin given to section1 which cause collapsing margin
See this link:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/

Push footer to the bottom of the page

The site structure is as follows - there is a common unit (content), which houses all of the elements of the site and the second unit, a footer which is to be pressed against the bottom of the site.
Content block is position: absolute for aligning the center (horizontal) - to decrease the screen when it is uniformly left for right and left its borders. The problem is that with such a block structure the footer doesn't stay pressed against the bottom of the page. Here's the code :
body {
margin: 0px;
padding: 0px;
}
.a_wrapper {
width: 600px;
left: 50%;
margin-left: -300px;
position: absolute;
border: 1px dotted #000000;
}
.a {
height: 800px;
}
.b {
width: 90%;
height: 50px;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
position: absolute;
border: 1px solid #000000;
}
<div class = "a_wrapper">
<div class = "a"></div>
</div>
<div class = "b">
</div>
https://jsfiddle.net/0k979ud5/
There are two things causing this - because of using only absolutely positioned elements, which takes them out of document flow, the body element itself has no height. So that would need to be set the same as the content. Then the footer is positioned according to the nearest positioned element, also because of position: absolute. It's direct parent is body which has no positioning so it will default to the window object. To solve this, body should be given position: relative :
body {
height: 800px;
position: relative;
padding: 0px;
margin: 0px;
}
.a_wrapper {
width: 600px;
left: 50%;
margin-left: -300px;
position: absolute;
border: 1px dotted #000000;
}
.a {
height: 800px;
}
.b {
width: 90%;
height: 50px;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
position: absolute;
border: 1px solid #000000;
}
<div class="a_wrapper">
<div class="a"></div>
</div>
<div class="b"></div>
It the footer should be below the content, body would have to be 850 pixels high of course...

How do i get last div to expand parent's height?

So i have .cont that's centered in using position absolute and is height 80% of body.
Inside it there are two divs. One is fixed height. And other one needs to expand to rest of parent, .cont.
So How do i make it expand to parent.
One other requirement is that content in both of these needs to be vertically and horizontally centered.
body
.cont
.top
.fillRest
Here is jsfiddle: http://jsfiddle.net/24jocwu5/
make .fillRest Expand to rest of .cont.
vertically and Horizontally center h1 headings in both divs.
Don't Use calc()
can use display table, flow, position, and other tricks.
Here you go. Absolutely position the white container with a top-padding that equals the height of your fixed-height top div. Then give the top div a z-index so it goes over your white box:
Fiddle - http://jsfiddle.net/24jocwu5/2/
* {margin: 0; padding: 0;}
html, body {
height: 100%;
background-color: #3dd;
color: #aaa;
font-family: helvetica;
}
.cont {
position: absolute;
top: 0; bottom: 0;
right: 0; left: 0;
background-color: #1af;
width: 400px;
margin: auto;
height: 80%;
}
.top {
height: 100px;
background-color: pink;
position: relative;
z-index: 1;
}
.fillRest {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding-top: 100px;
height: 100%;
background-color: #fff;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
h1 {
text-align: center;
width: 200px;
margin: auto;
background-color: #eee;
}
You can use flexbox for this
.cont {
display: flex;
flex-direction: column;
}
.cont > div {
display: flex;
}
.fillRest {
flex: 1;
}
Working Fiddle
This is what you want?
Only position fixed and right and left 0
http://jsfiddle.net/pabliiitoo/24jocwu5/1/
.fillRest {
position: fixed;
left: 0;
right: 0;
background-color: red;
min-height: 80px;
}
In order to expand the .fillRest to the rest of its parent .cont, you need to set it's height to a percentage. I estimate about 20~30% is what you want in order to maintain a similar look to the image you've provided here.
To test it, grab a very large paragraph full of letters and anything you want and put it where the 'Content' text is, that way you will be able to see it expanding in a responsive way. Another suggestion I will give you is to make your width percentages as well, so that they expand according to the width of the screen responsively.
Let me know if this helped you, otherwise I can take another look :)
CSS
.cont {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
background-color: #1af;
width: 400px;
margin: auto;
height: 80%;
}
Personally, I think the way you're going about this is all wrong. But maybe something like this would work.
http://jsfiddle.net/24jocwu5/5/
CSS selectors I changed:
.cont {
position: absolute;
top: 0; bottom: 0;
right: 0; left: 0;
background-color: #1af;
width: 400px;
margin: 10% auto;
overflow: hidden;
}
.top {
height: 20%;
background-color: rgba(255,255,255,.8);
}
.fillRest {
background-color: white;
position: relative;
width: 100%;
height: 80%;
}
h1 {
text-align: center;
width: 100%;
margin: auto;
background-color: #eee;
}
This is Summary Of Ways.. First two ways posted here--
FlexBox Method 100% WORKS
Padding Method 80% WORKS. Useful But not exactly.
Css Table Cell and Table Rows 100% WORKS. From Me.
Using Calcs Simplest One. 100% WORKS. From me.
Css Table Cell: http://jsfiddle.net/24jocwu5/7/
.cont is The Table. top & fillRest are table rows, and there is cell which can have vertical align middle.
Calc Method: http://jsfiddle.net/24jocwu5/9/
Works but doesn't scale well if content increases, so need to use another div which can contain the content. Like so http://jsfiddle.net/24jocwu5/10/
Default code:
* {margin: 0; padding: 0; }
html, body {
height: 100%;
background-color: #3dd;
color: #aaa;
font-family: helvetica;
}
.cont {
position: absolute;
top: 0; bottom: 0;
right: 0; left: 0;
background-color: #1af;
width: 400px;
margin: auto;
height: 80%;
}

Linkable section in screen fails for internet explorer

I have to centralize an image in both axis and then add a linkable area to that image's top left area. This works great for webkit and ff but ie fails. My html code is this:
<body>
<div class="content">
<img src="images/main_image.jpg" />
Logo
</div>
</body>
and my css code this:
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-color: #000;
overflow: hidden;
}
div.content {
position: relative;
width: 1001px;
height: 626px;
top: 50%;
margin: 0 auto;
padding: 0;
}
div.content img {
margin: 0;
padding: 0;
display: block;
position: relative;
top: -50%;
}
div.content a {
width: 14%;
height: 9%;
display: inline-block;
position: absolute;
top: -42%;
left: 7%;
text-decoration: none;
margin: 0;
padding: 0;
text-indent: -9999px;
}
this doesn't work for ie because i use an a tag displayed as inline-block positioned accordingly. Our friend ie doesn't show the linkable part in the screen at all because the text-indent. Can someone help a little bit? Thanks. This demo shall help you more i think.
Take a look at this demo (or results only here)
HTML is not changed. I assume that image has the same height/width as content div
CSS:
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-color: #000;
overflow: hidden;
}
div.content {
position: relative;
padding: 0;
border:solid 1px blue;
width: 1001px;
height: 626px;
/*below will center div on screen */
top: 50%;
margin: -313px auto 0;
}
div.content img {
margin: 0;
padding: 0;
display: block;
border:solid 1px white;
/*top:-50% removed. Assuming that image has the same height/width as content div*/
}
div.content a {
width: 14%;
height: 9%;
position: absolute;
/* top: -something changed. Remember that absolutely positioned div is always positioned from closest parent relative div*/
top: 10%;
left: 7%;
text-decoration: none;
margin: 0;
padding: 0;
text-indent: -9999px;
border:solid 1px green;
}
It looks a like you're creating a container, moving it to the bottom of the screen and then moving the image outside of it to the top-left corner of the screen. This last step is exactly what will fail in many cases. Child-elements usually will be hidden or cutted away when leaving their parent container. IE is more restrictive but correct in this case.
You can achieve your goal easier when you'll place the image outside the container. Keep in mind that body is a container by itself that is allways 100% wide and high (and cannot be changed to be 50% or whatsoever).
Here's the result on js-fiddle
The Html:
<body>
this is the body
<img class="my_image" src="images/main_image.jpg" />
<div class="content">
This is the container
<a href="#" >Logo</a>
</div>
</body>
CSS:
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-color: #000;
overflow: hidden;
color:silver;
}
div.content {
color:black;
background-color: silver;
position: relative;
width: 1001px;
height: 626px;
top: 50%;
margin: 0 auto;
padding: 0;
}
.my_image {
width:160px;
height:60px;
border:1px solid red;
margin: 0;
padding: 0;
display: block;
position: absolute;
top: 0;
left:0;
}
div.content a {
color:red;
font-size:14px;
display: inline-block;
position: absolute;
top: 20%;
left: 7%;
text-decoration: none;
margin: 0;
padding: 0;
}
In general it's the best to avoid negative values. They're misinterpreted in many browsers and produce problems.

CSS position absolute doesn't work in IE7

i have the following simple script, but it doesn't work in IE7
<div id="content">
<div id="left"></div>
<div id="right"></div>
<div id="bottom_menus">any text here...</div>
</div>
and CSS
#content
{
margin: 0 auto;
padding: 0;
width: 980px;
background-color: lime;
height: 800px;
overflow: hidden;
position: relative;
}
#left
{
width: 275px;
float: left;
background-color: olive;
margin: 0px 0px -5000px 0;
padding: 0 0 5000px 0;
min-height: 400px;
}
#right
{
width: 704px;
float: left;
background-color: red;
margin: 0px 0px -5000px 0;
padding: 0 0 5000px 0;
min-height: 400px;
}
#bottom_menus
{
background-color: orange;
height: 15px;
position: absolute;
bottom: 0px;
width: 100%;
}
why position absolute doesn't work?
thanks in advance
for absolute position to work, you must specify both direction: eg. top & left, or bottom & rightetc...
For you footer (bottom_menus) to take all space you need to set:
#bottom_menus {
background-color: orange;
height: 15px;
position: absolute;
left: 0;
right: 0; //assuming you need the footer to take the whole width
bottom: 0;
width: 100%;
}
ps: small remark, you dont need to set px unit when value is 0.
You haven't specified a left, so it's defaulting to 0px; Since you have a margin of -5000px on the box, I'm guessing it is working, and the bottom_menus div is off the screen to the left. Absolute positioning would ignore the padding of its parent container. Try setting left: 5000px, assuming you need the negative margin and positive padding. What are you trying to accomplish with that?