以下假设图像的宽度和高度是已知的:
#mydiv {
height: 400px;
position: relative;
background-color: gray; /* for demonstration */
}
.ajax-loader {
position: absolute;
left: 50%;
top: 50%;
margin-left: -32px; /* -1 * image width / 2 */
margin-top: -32px; /* -1 * image height / 2 */
}
更新:在现代浏览器margin:auto将产生所需的结果,知道图像的宽度/高度:
#mydiv {
height: 400px;
position: relative;
background-color: gray; /* for demonstration */
}
.ajax-loader {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto; /* presto! */
}