CSS(层叠样式表)主要用于描述HTML文档的外观和格式。获取网页高度通常是指获取浏览器窗口或整个文档的高度。
在CSS中,通常使用JavaScript来获取网页高度。
window.innerHeight
document.documentElement.scrollHeight
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>获取网页高度</title>
<style>
body {
height: 2000px; /* 设置一个较大的高度以便观察效果 */
}
.fixed-navbar {
position: fixed;
top: 0;
width: 100%;
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="fixed-navbar">
固定导航栏
</div>
<script>
window.addEventListener('scroll', function() {
var windowHeight = window.innerHeight;
var documentHeight = document.documentElement.scrollHeight;
console.log('浏览器窗口高度:', windowHeight);
console.log('文档高度:', documentHeight);
});
</script>
</body>
</html>
通过上述方法,你可以获取网页的高度,并根据需要进行相应的布局和交互设计。
领取专属 10元无门槛券
手把手带您无忧上云