在 HTML 中调整背景图像的大小



我是HTML新手,目前将以下代码存储在.css文件中。如果用户更改窗口大小,允许自动调整大小的最佳方法是什么?我尝试使用以下代码来允许此操作,但我无法获得预期的结果。

body {
background-image: url('Images/Space.jpg');
text-align: center;
}
text {
color:white;    
}
img {
width: 100%;
height: auto;
}

width 100%具有响应性,图像封面也是如此,但更有用,无需设置宽度或高度

* { margin: 0; padding: 0; }
		
body { 
	background: url(https://www.w3schools.com/css/img_flowers.jpg) no-repeat center center fixed; 
	-webkit-background-size: cover;
	-moz-background-size: cover;
	-o-background-size: cover;
	background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Resize the browser window to see the effect.</p>
<div></div>
</body>
</html>

尝试使用CSS background-size Property.

body {
background-image: url('Images/Space.jpg');
background-size: 100%;
text-align: center;
}

最新更新