Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML positioning element without blank space

Tags:

html

css

I have two elements (images)

imgA is 2000px high imgB is 1000px high

I place them one after the other and then move imgB up (I use relative positioning) to overlap the imgA.

Thus, the window in the browser should be 2000px high.

However, it seems, that when placing imgA and then imgB, the browser allocates the place for both of them i.e. 3000px hig, and after I move imgB up to overlap imgA, I have a blank space =1000px left at the bottom of the page.

How can this blank space be prevented?

Thanks

like image 275
Yura Avatar asked Oct 28 '25 09:10

Yura


2 Answers

Ignore this

This is because the images are block level elements. To stop the browser from allocating space you can just add: display:inline-block to the second image. This will bring the image out of block structure and so the browser will not allocate it whitespace.

Also have a look at Relatively Absolute positioning, it is very handy for the sort of thing you are doing.


EDITED

As commented below, this does not work. Use instead the Relatively Absolute positioning.

Here is a jsFiddle that shows the code needed to position an image over another

like image 199
Michael Allen Avatar answered Oct 31 '25 00:10

Michael Allen


Use display: block on your images, then use position: absolute instead of position: relative to position imgB over imgA.

Don't forget to assign the parent element to anything other than position: static to make the positioning of your image relative to the parent element.

like image 37
Han Dijk Avatar answered Oct 30 '25 23:10

Han Dijk