html - CSS:背景图像大于整个页面

标签 html css reactjs google-chrome

编辑:不是链接问题的重复,因为我说我在另一个问题中尝试了接受的解决方案,但它们没有解决我的问题

我正在尝试为包含部分提供背景图像,但该图像比屏幕大大约 15%。我尝试将背景大小设置为 100%、覆盖和包含,但它始终比页面大。我查看了 DOM 中每个元素的大小,它们的大小都相同,没有一个更大,所以我很困惑是什么导致了更大的背景图像。这是我的代码: html

<section className="Intro">
    <img />
    <h1>...</h1>
    <h2>...</h2>
    <h3>...</h3>
    <p>...</p>
</section>

CSS

.Intro {
  background-image: url('../mobile-bg.jpg');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  color: white;
  height: 100vh;
}

最佳答案

您的代码是正确的。但是有些浏览器不支持某些 css 规则。然后我们必须使用他们的 css 规则。试试这个。它会起作用

.Intro {
  background-image: url('../mobile-bg.jpg');
  background-repeat: no-repeat;
  background-position: center center;
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
  background-size: cover;
  color: white;
  height: 100vh;
}

关于html - CSS:背景图像大于整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319730/

相关文章:

html - float 内的 z-index 不会溢出到兄弟节点

javascript - 在 Firefox 扩展中使用 indexedDB

php - 由于模板提供的脚本,CSS 无法加载

javascript - 验证 JavaScript 中的电子邮件输入

javascript - 为什么我要在脚本标签上放置一个 ID?

html - HTML5 canvas 元素可以使用哪些脚本语言?

javascript - Bootstrap 表单动态添加删除带有字段的表单

jquery - 动态分配CSS类到网站菜单栏struts2

javascript - 如何在React Native其他函数中使用useState

javascript - react Hook 中的事件监听器 onMouseMove : event object is undefined or clientX/Y are null