【问题标题】:Why is my container background image so zoomed in?为什么我的容器背景图片这么放大?
【发布时间】:2022-01-07 15:01:47
【问题描述】:

我在浏览器上放大容器的背景图像时遇到问题。图片是1200pxwide,我的容器也是1200pxwide。我将它设置为我的 .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;
}

我错过了什么吗?

【问题讨论】:

  • 哦,这是另一部分。它在 Chrome 中正确加载,但在 Safari 中不正确!我应该事先对此进行测试.....有我需要为他们安装的 webkit 吗?再次感谢!
  • bootstrap.min.css 中有什么东西覆盖了你的 style.css?您可以编辑该引导 css 文件(可能不想)或在 style.css 中添加更多属性以覆盖它们。在 style.css 属性中使用 !important,如果这不起作用,请添加内联样式,&lt;div class="container" style="background-image: url(img/background.jpg)"&gt; 我看到有人回答了您想要摆弄的一些 css 属性将/可能是什么。

标签: html css twitter-bootstrap-3


【解决方案1】:

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

.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

【讨论】:

  • 我知道这是一篇旧帖子。但是,我不确定这是如何工作的!您可以在答案中添加说明吗?
  • @DmanCannon 添加了指向官方 MDN Web 文档的链接。
【解决方案2】:

这很好用!

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;

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多