开发者

How can I center an image of an unknown width?

Using CSS how can I center an image of unknown dime开发者_运维技巧nsions?


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%;}


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.


You can use CSS like this:

img { display: block; margin: 0 auto; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜