html - 为什么我的容器背景图像被放大了?

标签 html css twitter-bootstrap-3

我在浏览器中放大容器的背景图片时遇到问题。图片是 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/

相关文章:

javascript - 如何指定一个样式不被JS中指定的样式覆盖

javascript - 两次更改相同的 HTML 标记

css - 我如何使用angularjs中的点击事件进入主页?

html - 如何在一个数据图标声明或 div 中包含多个字体图标

javascript - 防止透明div内的图像透明

javascript - 在图像上滑动

html - 垂直居中的div

html - Bootstrap - 如何将列高设置为多行?

html - 调整 Bootstrap 宽度,但保持容器类约束

html - Bootstrap 3 网格行跨度