[英]Background-size: cover; doesnt cover screen
html 这里是新手,我正在尝试使用background-size: cover;
使图像覆盖整个屏幕,我仍然有这个差距。 我怎样才能解决这个问题? 我觉得这与这部分有关:
body {
background-image: url("hexagon.gif");
background-size: cover;
我试图用 .background-image 替换 body 这个词,但随后整个页面变成空白。 我该如何解决? 谢谢!
<!DOCTYPE html>
<html>
<head>
<meta name= viewport content= width="device-width initial-scale=1.0">
</head>
<body>
<div class="background-image">
<style>
* {
margin: 0;
padding: 0;
}
body {
background-image: url("hexagon.gif");
background-size: cover;
background-repeat: no-repeat;
height: 100hv;
background-color: #cccccc;
}
</style>
</div>
</body>
</html>
cover
您在代码中遗漏的是,
height:100hv
语法错误,改成height:100vh
width:100vw
<!DOCTYPE html>
<html>
<head>
<meta name= viewport content= width="device-width initial-scale=1.0">
</head>
<body>
<div class="background-image">
<style>
* {
margin: 0;
padding: 0;
}
body {
background-image: url("hexagon.gif");
background-size: cover;
background-repeat: no-repeat;
height: 100vh;/*CORRECTED SYNTAX ERROR*/
width: 100vw;/*ADDED CODE*/
background-color: #cccccc;
}
</style>
</div>
</body>
</html>
* { margin: 0; padding: 0; } body { background-image: url("https://picsum.photos/1800/900");/*Example*/ background-size: cover; background-repeat: no-repeat; height: 100vh;/*CORRECTED SYNTAX ERROR*/ width: 100vw;/*ADDED CODE*/ background-color: #cccccc; }
使用background-size:100vw 100vh;
,
或background-size:100% 100%;
如果高度和宽度都设置为height:100vh;width:100vw;
<!DOCTYPE html>
<html>
<head>
<meta name= viewport content= width="device-width initial-scale=1.0">
</head>
<body>
<div class="background-image">
<style>
* {
margin: 0;
padding: 0;
}
body {
background-image: url("hexagon.gif");
background-size: 100vh 100vh;
background-repeat: no-repeat;
background-color: #cccccc;
/*OR USE THE BELOW*/
/*
background-image: url("hexagon.gif");
background-size: 100% 100%;
background-repeat: no-repeat;
height: 100vh;
width: 100vw;
background-color: #cccccc;
*/
}
</style>
</div>
</body>
</html>
* { margin: 0; padding: 0; } body { background-image: url("https://picsum.photos/1800/900");/*Example*/ background-size:100vw 100vh; background-repeat: no-repeat; background-color: #cccccc; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.