只需将<html>
和<body>
的height
和width
设置为100%
,overflow
设置为hidden
,并在元素的left
,top
,width
和height
中使用百分比即可。
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Proportional resizing</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
overflow: hidden;
}
div {
position: absolute;
left: 30%;
top: 20%;
width: 40%;
height: 30%;
background-color: #ddd;
}
</style>
</head>
<body>
<div>divthing</div>
</body>
</html>
<body>
。
更新:回答另一个问题:目前几乎不支持背景图片的缩放。当 CSS 3 background-size
属性普及时(请参见此表),事情会更容易。现在,请查看这个答案(是的,这意味着:更多标记)。
div
的大小吗? - Marcel Korpel