CSS: Overlaping two images and proper flow - html

As you can see in http://jsfiddle.net/omarjuvera/v93us4n9/6/ the paragraph is not where it should be, since I am overlapping two images. How can I fix this?
HTML
<h1>Overlaping two images</h1>
<div>
<img src="http://img.youtube.com/vi/brMyE7To7Sg/0.jpg" />
<img src="http://oi57.tinypic.com/2u8kr2s.jpg" />
</div>
<br/>
<p>For some reason, this paragraph is not below DIV, but under/over</p>
CSS
div {
position: relative;
}
img {
position: absolute;
left: 0px;
top: 0px;
}

It's because both of the img elements are absolutely positioned and removed from the flow.
Due to this, the parent div element collapses upon itself and has a height of 0. Unless the parent div element has explicit dimensions, in this case a height, the text will overlap.
Setting an explicit height on the parent element will solve this, but that's not a very flexible solution.
In your case, since the img elements are the same size, you could solve this by only absolutely positioning a single img element. In doing so, the height of the parent div element will be defined based on the height of the img element that isn't absolutely positioned.
Updated Example
<div>
<img src="//placehold.it/480x360" />
<img class="overlay" src="http://oi57.tinypic.com/2u8kr2s.jpg" />
</div>
div {
position: relative;
}
img.overlay {
position: absolute;
left: 0px;
top: 0px;
}

Related

What's the standard of width % for child positioned: absolute [duplicate]

This question already has answers here:
Pseudo element not full container width when border used
(3 answers)
Closed 1 year ago.
As I know, child's width percentage's standard is parent's content box(only the content, without padding or margin.). So if there's a padding in parent and child's width is 100%, child's width is smaller than parents. But If I position child as a absolute and parent as a relative, child's width is just equal to the parent's no matter padding and margin in parents. Like this:
<div class="first">HI
<div class="second">
HELLO
</div>
</div>
css code
.first{
background-color: yellow;
width:100px;
height:100px;
padding:10%;
position:relative;
}
.second{
background-color: blue;
position:absolute;
width: 100%;
height:100%;
opacity:40%;
}
Eventhough parent's position and relative so Child is totally dependent on '.first'. What's the standard of child's width in this case?
This snippet shows the result of setting the second div to have position relative and then position absolute. You can see that the absolutely positioned element takes on the width of its parent including the padding.
.first {
background-color: yellow;
width: 100px;
height: 100px;
padding: 10%;
position: relative;
}
.second {
background-color: blue;
width: 100%;
height: 100%;
opacity: 40%;
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
<h2>The blue square has relative position</h2>
<div class="first">HI
<div class="second relative">
HELLO
</div>
</div>
<h2>The blue square has absolute position</h2>
<div class="first">HI
<div class="second absolute">
HELLO
</div>
</div>
The reason seems to be that:
when a box has position: absolute its containing box is the parent's padding box.
See the accepted answer to: Absolute positioning ignoring padding of parent though I am struggling to find the exact description of that in the actual standard documents and it would be good if someone could point out a primary reference.
UPDATE: thanks to Temani Afif who has pointed out this SO answer which has info. from an actual specification:
The standard of the % for position:absolute is of the nearest positioned ancestor block and if no ancestor is positioned, it is relative to body element. In your case since the first is positioned relative the second will be relative to first and if u remove the position attribute of first, second will be positioned relative to body.
You can also check this - https://www.w3schools.com/css/css_positioning.asp

Why does height 100% work on absolutely positioned element?

As far as I know for the height to work as percentage the container element must have a specific height mentioned. But this doesn't hold true for absolutely positioned element with the ancestor being relatively positioned. Here is a working example of what I meant:
.container {
width: 400px;
background: cyan;
text-align: right;
position: relative;
color: white;
}
.child {
width: 90%;
height: 100%;
background: blue;
}
.absolute {
position: absolute;
}
.second {
margin-top: 30px;
}
<div class="container">
<div class="child absolute">Absolute</div>
one <br> two <br> three <br> one <br> two <br> three <br>
</div>
<div class="container second">
<div class="child">Static</div>
one <br> two <br> three <br> one <br> two <br> three <br>
</div>
As you can see the absolutely placed div applied 100% height onto it but not the statically positioned div. Why?
From MDN
relative
This keyword lays out all elements as though the element were not positioned, and then adjusts the element's position, without changing layout (and thus leaving a gap for the element where it would have been had it not been positioned). The effect of position:relative on table-*-group, table-row, table-column, table-cell, and table-caption elements is undefined.
Read more. Is very nicely described.
Here is a great read about the different position types:
Absolute is relative to the parent element and is not affected by other elements and are removed from the flow of the page i.e. you can see the list with one, two, three unaffected.
It's height is 100% as .child specifies.

Absolute item is not located relative to its container

I have two items in a 'relative' container.
The problem is that the 'absolute' item should be located relative to the container but it is located relative to the second 'static' item.
Example:
<div style="position:relative">
<button style="position:absolute;top: 200px">Botton</button>
<div style="margin-top: 500px">hi</div>
</div>
The div (with 'hi' text) should be located below the button but somehow the button is located 200px below the 'hi' div.
Note: If I will add border to the container it will behave as expected.
Example for the difference where there is a border and where there is not: JSfiddle
it is working as expected, because one is using position:absolute the other is using margin-top, the second one won't be doing margin-top to the sibling because the sibling is out of the flow (using position:absolute)
so instead of margin-top use position:absolute, or use margin-top on both children.
position
div {
position: relative
}
div > * {
position: absolute
}
button {
top: 200px
}
span {
top: 300px
}
<div>
<button>Botton</button>
<span>hi</span>
</div>
margin
button {
margin-top: 200px
}
span {
margin-top: 300px;
display: block
}
<div>
<button>Botton</button>
<span>hi</span>
</div>
Maybe if you delete the 200px from the <button style="position:absolute; top: 200px>Botton</button>?

Div doesn't have height

Why does wrapper div not have a height? If I set the height (height:200px) the green background appears but how to set with auto height?
Here is my code (JSFiddle):
HTML:
<div class="wrapper">
<div class="effect"></div>
<div class="content">
...content
</div>
</div>
CSS:
.content {
position: absolute;
background-color:red;
}
.wrapper, .effect {
background: green;
}
.wrapper {
position: relative;
width: 630px;
}
.effect {
width:100%;
position: absolute;
}
It is not working (i.e. parent element not having any height) because all the immediate descendant of the .wrapper element is absolutely positioned — this will have the effect of taking them out of the flow of the document, therefore causing the parent's dimension to collapse to nothing.
You will also notice that the effect is the same when you float all
descendants of the parent wrapper, because float also has the
effect of taking normal elements out of the document flow.
There are only two ways to prevent this from happening, both of which involving declaring a certain height for the parent .wrapper element:
Either you explicitly state a height for the parent (see example fiddle)
Or use a relative height (say, in percentages or viewport units) that is not dependent on its own content.
You should reconsider your design strategy, and what you're trying to achieve. There is probably other ways to achieve what you intend to do, will you mind showing us?

Positioning Elements in Tableless Layout

I am trying to do a tableless
layout, and I have the following
HTML snippet:
<div class="slider-inner">
<div class="slider-pane">
<div class="container">
<p>...</p>
<div class="did-you-knoow">
<div class="facts">
</div>
<div class="marquee-container">
</div>
</div>
</div>
</div>
</div>
Which is styled w/ this CSS:
div.slider-pane {
width: 1024px;
}
div.container {
display: block;
}
div.facts {
margin-right: 60%;
}
div.marquee-container {
position: absolute;
top: 0;
right: 0;
margin-left: 0;
margin-right: 0;
padding: 10px;
width: 60%;
}
I want the div.facts to occupy the left-40% , and the div.marquee-container
to occupy the right-60% of the div.did-you-know (their immediate parent). I
expect the div.marquee-container to be positioned relative to its parent, and
its width to be 60% of its parent, but its positioning and width are relative
to div.slider-inner, which is 2 levels above its parent.
How do I set the position and width of div.marquee-container relative to
its parent, and not the div three levels above it?
add position:relative to div.container
#locrizak's answer is correct, I needed to add 'position:relative' to the div.container, but I needed to add it to the div.did-you-know as well. In other words, I needed to set all of the containing div's to position:relative in order for the elements in question to be positioned relative to the immediate parent.
I found this was also answered in the MDN page for css position under the 'absolute' definition:
[The browser will] position [the element] at a specified position relative to its closest positioned ancestor or to the containing block
However the W3C reference was not as helpful.