Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position element below relative positioned element without overlapping?

Tags:

css

position

If I put anything after element with relative+absolute positioned elements, it overlaps. It does not do this if I specify height, but I do not want to do this as content of relatively positioned element is dynamic. How to get rid of overlapping without specifying height?

Simple example:

<div style="position:relative">
    <div style="position:absolute;">
        blabla
    </div>
</div>
I WANT THIS BELOW
like image 695
gadelat Avatar asked Jul 27 '13 14:07

gadelat


People also ask

How do you position an element at the bottom of a container?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

How do you move an element with a relative position?

Relative Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

How do I stop HTML overlapping?

Answer 5523099de39efeabcb0002f2from each <img> , and they should stop overlapping each other.


2 Answers

absolute positioning takes the element out of the flow of the structure. It's presence is ignored for this reason, it's what it's purpose is. If you need an element positioned left or right then use float

When floating elements ensure you clear the bottom of the div so then the layout is preserved

<div style="clear:both;"></div>

DEMO http://jsfiddle.net/kevinPHPkevin/uHuSF/

Otherwise a different approach is required to accomplish your goal.

like image 94
Kevin Lynch Avatar answered Oct 06 '22 01:10

Kevin Lynch


Since you've applied position:relative to your very first element either you've to specify the height or provide padding-bottom to it.

For instance, in your provided code I've applied height of 20px to it.

Note: Using height is better than using padding-bottom for this kind of scenario

<div style="position: relative; height: 20px;">
    <div style="position:absolute;">blabla</div>
</div>
<div>I WANT THIS BELOW</div>
like image 21
Rishi Kulshreshtha Avatar answered Oct 06 '22 03:10

Rishi Kulshreshtha