为什么我的容器背景图像如此放大?

时间:2015-04-05 23:52:03

标签: html css html5 css3 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;
}

我错过了什么吗?

2 个答案:

答案 0 :(得分:8)

您需要为背景图片指定更多属性,如下所示:

.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;
}

答案 1 :(得分:1)

这很好用!

background:rgb(37, 16, 13) url("images/bc02.jpg");
background-repeat: no-repeat;
width: 100%;
height:100%;
background-position: center center;
text-align:center;
margin:auto;
padding:0;