

html
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自动获取屏幕尺寸信息的html文件</title>
<style>
#testDiv {
width: 100%;
height: 100vh; /* 100% of the viewport height */
background-color: lightblue;
text-align: center;
font-size: 2em;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div id="testDiv">
宽度: <span id="width"></span>px, 高度: <span id="height"></span>px
</div>
<script>
function updateSize() {
const div = document.getElementById('testDiv');
const width = div.offsetWidth;
const height = div.offsetHeight;
document.getElementById('width').textContent = width;
document.getElementById('height').textContent = height;
}
window.onload = updateSize;
window.onresize = updateSize;
</script>
</body>
</html>