Vertical and horizontal align of image [duplicate]

狂风中的少年 提交于 2020-01-06 02:21:47

问题


I have a placeholder (under construction) site which holds only one image with the logo. I horizontically aligned with margin: 0 auto. But how do I know additionally horizontal align the image. Or with one sentence: How do I fully centrate an image.

Look here to see live: fiddle

Regards, Bodo

EDIT: For 1:2 (height to width) images use:

#wrapper img
{
    position: absolute;
    top: 50%;
    left: 50%;
    width: 799px;
    height: 404px;
    margin: -202px -399px 0;
    border: 1px solid black;
}

like in the linked thread :)

thank you for the help


回答1:


Urm.. Have you tried ?

<div class='placeholder'>
   <img src='../images/myimage'>
</div>

And the css for that markup

.placeholder
{
position : absolute;
top : 50%;
left : 50%;
width : 200px; /* Width of the image in question */
height : 200px;/* height of the image in question */
margin : -100px 0px 0px -100px;
}

This should bring your image to the very center of the page.




回答2:


This will do it

img#logo {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100px;
    height: 100px;
    margin-left: -50px;
    margin-top: -50px;
}

http://jsfiddle.net/DxByM/




回答3:


Have a div to hold the image with the following rules:

margin-left: auto;
margin-right: auto;
margin-top:200px;  <!-- Will depend on your image -->
width:[your-image-width]px;

Will center your image



来源:https://stackoverflow.com/questions/10515252/vertical-and-horizontal-align-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!