What is the better way to center a image in a div?

Hello friends:

After looking for some example, I got this one:

<html>
	<head>
	</head>
	
	<style>
		body  {background-color:white}
		div {background-color:cyan; border: 1px solid black; height:200px; width:200px; display:table-cell; vertical-align:middle; text-align:center}
		
	</style>
	
	<body>
		<div>
			<img src="c:\aguia.jpg" width="50%" height="50%">
		</div>	
	</body>


</html>

Open in new window


is the better way to center ( vertical and horizontal) the image in my div?

Regards
Alex
LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eddie ShipmanAll-around developerCommented:
From: https://www.w3.org/Style/Examples/007/center.en.html

div img {
    display: block;
    margin-left: auto;
    margin-right: auto
}

Open in new window

Snarf0001Commented:
Personally I prefer using flex for vertical alignment.  I've found that having display:table-cell causes unexpected issues with placement on the containing element.  Of course I can't specifically remember any right now.... but I've had problems.
Nothing with flex:

<html>
<head>
    <style>
        body { background-color: white }
        div { background-color: cyan; border: 1px solid black; height: 200px; width: 200px; display: flex; align-items: center; justify-content: center; }
    </style>
</head>
<body>
    <div>
        <img src="c:\aguia.jpg" width="50%" height="50%">
    </div>
</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hidrauAuthor Commented:
thanks a lot for help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.