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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29463586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:47:23  来源:igfitidea点击:

Why is my container background image so zoomed in?

htmlcsstwitter-bootstrap-3

提问by newman

I am having an issue with my background image of a container being zoomed in on the browser. The picture is 1200pxwide and my container is also 1200pxwide. I have it set as the background-image for my .container selector. When I load it in the browser it is only showing a small portion of the image because it is zoomed in so far. Why is this happening?

我在浏览器上放大容器的背景图像时遇到问题。图片很1200px宽,我的容器也1200px很宽。我将它设置为我的 .container 选择器的背景图像。当我在浏览器中加载它时,它只显示了图像的一小部分,因为到目前为止它被放大了。为什么会这样?

HTML:

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:

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

Am I missing anything?

我错过了什么吗?

回答by AndrewL64

You need to specify a few more properties for your background-image like this:

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

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

回答by ganji

This works fine!

这工作正常!

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;