CSS 在页面中央显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1224399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:54:32  来源:igfitidea点击:

Display the image in the center of the page

css

提问by Zeeshan Rang

I have a image -- a loading image.

我有一个图像——一个加载图像。

I want that image to be displayed in the center on the page. How can I do that?

我希望该图像显示在页面的中心。我怎样才能做到这一点?

The code I wrote is :

我写的代码是:

img.loading
    {
        position:absolute;
        left:0px;
        top:0px;
        z-index:1;
    }

How can i make this image always be displayed in the center of the page?

如何使此图像始终显示在页面中央?

回答by CD..

Found this: How to Center an Image Perfectly in CSS, might help.

发现这个:如何在 CSS 中完美地居中图像,可能会有所帮助。

#img
{
    position:absolute;
    width:592px; /*image width */
    height:512px; /*image height */
    left:50%; 
    top:50%;
    margin-left:-296px; /*image width/2 */
    margin-top:-256px; /*image height/2 */
}

回答by Chase Seibert

See W3C

W3C

IMG.displayed {
    display: block;
    margin-left: auto;
    margin-right: auto 
}

<IMG class="displayed" src="..." alt="...">

回答by noelyahan

Or you can use background: url(center_image) no-repeat center;

或者你可以使用 background: url(center_image) no-repeat center;



working example:http://jsfiddle.net/DT4hL/

工作示例:http : //jsfiddle.net/DT4hL/