Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Margin: 0 Auto; is not working on <img> tag

Tags:

html

css

margin

My markup is simple, and I'm using inline CSS. My goal is to get the image centered using margin: 0 auto; in the style HTML attribute. Here's code that I have tried:

<div style="width: 100%;">
<img src="http://dummyimage.com/200x300/000000/fff" style="margin: 0 auto;"/>
</div>

Why is the CSS not centering my <img>?

like image 216
Drazzah Avatar asked Oct 21 '14 01:10

Drazzah


Video Answer


2 Answers

You must use display: block to make margin: 0px auto have any effect.

<div style="width: 100%;">
<img src="http://placekitten.com/g/200/300" style="display: block; margin: 0px auto;">
</div>
like image 70
Fahad Hasan Avatar answered Sep 19 '22 00:09

Fahad Hasan


Your style attribute syntax is wrong on the div. Also, change the style on the img tag like so:

<div style="width:100%">
<img src="https://4c206b86fe5a0219d31bb1ae55c8bbdc3f028879-www.googledrive.com/host/0BzEiAml5ygAeU3N6QTRUUW53Vjg/" width="50%" height="auto" style="margin:0 auto; display:block">
</div>
like image 31
Talmid Avatar answered Sep 20 '22 00:09

Talmid