css - 小心粘贴背景图片
问题描述
我想创建一个对文档中的其他 div 没有影响的背景图像。我在这里有这段代码,效果很好:
body,
html {
height: 100%;
margin: 0;
}
#bg {
/* The image used */
background-color: rgb(0, 0, 0);
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#heading {
position: absolute;
text-align: center;
vertical-align: central;
width: 40%;
left: 30%;
color: #F82473;
}
<div id="heading">Some stuff</div>
<div id="bg"></div>
但是,如果我切换我的 div 顺序,那么它的工作方式很奇怪:
body,
html {
height: 100%;
margin: 0;
}
#bg {
/* The image used */
background-color: rgb(0, 0, 0);
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#heading {
position: absolute;
text-align: center;
vertical-align: central;
width: 40%;
left: 30%;
color: #F82473;
}
<div id="bg"></div>
<div id="heading">Some stuff</div>
我应该如何在我的网站上粘贴背景图片,这样它就不会影响我文档中的任何其他 div?
解决方案
请使用 div 到 div 因为你有 2 个 div。一个用于背景,另一个用于标题,就像
<div class="bg">
<div id="heading">Some stuff</div>
// All the staff for background will goes here
</div>
它适用于您的情况,如果您在此之后使用其他 div,它们将没有背景图像。
推荐阅读
- visual-studio - Visual Studio 2019 构建顺序不起作用
- docker - Traefik:使用 docker 的配置不起作用
- python - 我想收集令牌的计数。看看最常见的标记是什么?,我写的代码不起作用,所以我评论了我的代码
- c++ - C ++设置和获取方法不产生值?
- flutter - Flutter Error FAILURE:构建失败并出现异常
- core-data - 警告:尝试在已经呈现(空)SwiftUI 的 * 上呈现 *
- php - 基于行数的透视数组
- swift - Swift 结合发布者与完成处理程序以及何时取消
- xcode - 工作区完整性,无法加载项目。升级到 MACOS Catalina 后
- javascript - 如何连接到 Firebase Firestore?