对于大多数浏览器,您可以使用 position:fixed
img.centered {
position:fixed;
left: 50%;
top: 50%;
/*
if, for instance, the image is 64x64 pixels,
then "move" it half its width/height to the
top/left by using negative margins
*/
margin-left: -32px;
margin-top: -32px;
}
Run Code Online (Sandbox Code Playgroud)
例如,如果图像是40x30像素,则需要进行设置margin-left:-20px; margin-top:-15px.
这是一个jsfiddle示例:http://jsfiddle.net/WnSnj/1/
请注意,position:fixed在所有浏览器中的工作方式并不完全相同(尽管在所有浏览器中都可以).请参阅:http://www.quirksmode.org/css/position.html