Center image on top of div

I have a div with a bunch of things and I want to make an image centered on it. My basic layout looks something like this:

<div> <img style="display:block; position: absolute; top:50%; left:50%; margin-top:-33px; margin-left:-33px;" src="Images/ajax-loader_001.gif" /> <!-- a bunch of tables and stuff that doesn't have a conveniently fixed size --> </div> 

The above will center the image on the screen (66px x 66px BTW image), but I want it to be centered relative to the parent div and β€œfloating” above it. Is there a purely css way to do this? You must work in IE8 and the latest versions of Firefox.

Edit: just to clarify - I'm looking for the center both horizontally and vertically and above the parent.

+4
source share
3 answers

Just need to put this in the parent div:

 position: relative; 
+5
source

You can use:

 <div> <img style="display:block; position: relative; margin:auto" src="Images/ajax-loader_001.gif" /> <!-- a bunch of tables and stuff that doesn't have a conveniently fixed size --> </div> 

This will work for sure.

+2
source

You must remove all absolute positioning elements and instead do it img {display: block; margin: 0 auto; }

0
source

Source: https://habr.com/ru/post/1401458/


All Articles