我在浏览器中放大容器的背景图片时遇到问题。图片是 1200px
宽,我的容器也是 1200px
宽。我将它设置为我的 .container 选择器的背景图像。当我在浏览器中加载它时,它只显示图像的一小部分,因为到目前为止它已经放大了。为什么会这样?
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
<main role="main">
<article role="article">
<section>
<header>
<div class="container">
</div>
</header>
</section>
</article>
</main>
</body>
</html>
CSS:
@font-face {
font-family: "Brandon Grotesque";
src: url("fonts/Brandon_Grotesque/Brandon_reg.otf")
format("opentype");
}
html, body {
padding:0;
margin: 0;
background-color:#222222;
}
body {
font-family:"Brandon Grotesque";
width: 1200px;
margin: 0 auto;
}
.container {
width: 1200px;
height:600px;
background-image:url("img/background.jpg");
text-align:center;
margin:auto;
padding:0;
}
我错过了什么吗?
最佳答案
您需要像这样为背景图像指定更多属性:
.container {
width: 1200px;
height:600px;
background-image: url("img/background.jpg");
background-size: cover; /* or contain depending on what you want */
background-position: center center;
background-repeat: no-repeat;
text-align:center;
margin:auto;
padding:0;
}
关于background
属性的详细解释可以引用官方MDN Web Docs .
关于html - 为什么我的容器背景图像被放大了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463586/