您可以使用 jquery 并在窗口调整大小时计算填充和所有内容。
这是一个例子:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<style>
#alwaysCentered{
background: #D4D4D4;
width: 175px;
text-align: center;
}
</style>
</head>
<body>
<div id="alwaysCentered">I will always be centered</div>
<script>
alwaysCenteredMarginLeft = ($(window).width() - $('#alwaysCentered').width()) / 2
alwaysCenteredMarginTop = ($(window).height() - $('#alwaysCentered').height()) / 2;
$('#alwaysCentered').css({
'margin-left' : alwaysCenteredMarginLeft,
'margin-top' : alwaysCenteredMarginTop
});
window.onresize = function(event) {
alwaysCenteredMarginLeft = ($(window).width() - $('#alwaysCentered').width()) / 2
alwaysCenteredMarginTop = ($(window).height() - $('#alwaysCentered').height()) / 2;
$('#alwaysCentered').css({
'margin-left' : alwaysCenteredMarginLeft,
'margin-top' : alwaysCenteredMarginTop
});
}
</script>
</body>
</html>