使用html body设置宽高100%
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
}
div{
width: 100%;
height: 100%;
background-color: lightskyblue;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img src="./2.jpg" alt="">
</div>
</body>
</html>
使用height: calc(100vh)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 100%;
height: calc(100vh);
background-color: lightskyblue;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img src="./1.jpg" alt="">
</div>
</body>
</html>
使用绝对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
position: absolute;
width: 100%;
height: 100%;
background-color: lightskyblue;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img src="./3.jpg" alt="">
</div>
</body>
</html>
使用js document.body.clientHeight
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img src="./3.jpg" alt="">
</div>
<script>
let div = document.querySelector('div')
div.style.width = document.body.clientWidth+'px'
div.style.height = document.body.clientHeight+'px'
</script>
</body>
</html>
使用jquery $(window).height()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img src="./4.jpg" alt="">
</div>
<script>
$('div').width($(window).width())
$('div').height($(window).height())
</script>
</body>
</html>