Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center an image of an unknown width?

Tags:

css

Using CSS how can I center an image of unknown dimensions?

like image 389
JimBo Avatar asked Jun 06 '10 19:06

JimBo


3 Answers

Horizontally

img {display:block; margin: 0 auto}

or

.container {text-align:center}

Vertically

/* container must contain some text as well */
.container, img {line-height:100px; vertical-align:middle}

or

.container {display:table-cell; vertical-align:middle}

If image is only decorative:

.container {background: url(…) 50%;}
like image 55
Kornel Avatar answered Dec 19 '22 07:12

Kornel


Seeing as images are native inline-block elements (I think - at least not block),

text-align: center 

for the surrounding container will do the job.

like image 20
Pekka Avatar answered Dec 19 '22 07:12

Pekka


You can use CSS like this:

img { display: block; margin: 0 auto; }
like image 26
Martti Laine Avatar answered Dec 19 '22 07:12

Martti Laine