CSS background-size:contain不占全部宽度。它确实适当地覆盖了所有高度,但在屏幕右侧留下了一个非常大的(500 px或更宽)白色部分。我试过任何办法,但都不能使它奏效。我留下了一个简单的代码,我去掉了所有不必要的部分。到目前为止,我已经尝试了Chrome和Firefox,得到了相同的输出。我想使用background-size:contain,因为当窗口大小变小时,它会重新缩放,而background-size:cover不会这样做。
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<meta charset="UTF-8">
<style>
html, body
{
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}
body
{
background-image: url('violin.jpeg');
background-repeat: no-repeat;
background-size: contain;
}
</style>
<body>
<h1>Heading</h1>
<p>Paragraph.</p>
</body>
</html>
您可以尝试像这样的背景大小:封面
background-size: cover;
background-repeat: no-repeat;
background-position: center;