html - css 背景图像显示在我的其他代码前面

标签 html css

我的背景图片一直出现在所有内容的前面。当然我只是希望它成为背景...

我已经尝试在我的 HTML 文件和 CSS 文件中设置背景图片。两者都在我的文本和导航栏的前面。

<img src="img/homepage.jpg" id="bg" alt="oops">

*,
*::before,
*::after {
box-sizing: border-box;
}

:root{
    --bg-color: #D8D8D8;
}

body {
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
width: 100vw;
height: 100vh;
color: #000;
font-family: 'Maitree', serif;
}
#bg {
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 100%; 
}

我想要我的照片作为背景。

最佳答案

你很幸运,css 有你需要的东西,background-image 属性。您选择要具有背景的元素,然后使用 css 设置图像。

如果您希望它位于整个body 的后面,那么您可以尝试:

body {
  background-image: url('img/homepage.jpg'); // make sure this path is relative
  background-size: cover; // cover is a good option here, but you might want something else
}

查看 https://developer.mozilla.org/en-US/docs/Web/CSS/background因为您可以对背景做很多事情,包括定位、更改大小、重复等等。

关于html - css 背景图像显示在我的其他代码前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119466/

相关文章:

html - anchor 标签高度值不会改变

javascript - 错误 : Assertion Failed: The URL '/' did not match any routes in your application

javascript - 使用 Javascript 从 DOM 收集详细信息以添加到数组

html - Bootstrap 背景图像无法在 iPhone 上调整大小

javascript - 从相应的单选按钮获取输入文本

javascript - 使用javascript函数检索隐藏文本值对象HTMLinputElement错误

jquery - 通过宽度和高度管理样式的媒体查询模式

html - 在 CSS 中应用文本描边和文本阴影?

jquery - 将导航栏固定在 Bootstrap 的特定位置

php - 如何删除父主题生成的样式?