首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么相对定位消除不透明度?

问为什么相对定位消除不透明度?
EN

Stack Overflow用户
提问于 2017-03-07 01:43:52
回答 2查看 1.5K关注 0票数 1

我做了一个简单的网站,其中有一个图像在背景中有0.3的不透明度,并有一个背景颜色。最重要的是一些文字。下面是HTML:

如果我使位置相对,文本不受图像的不透明度的影响。如果我让它不是相对的,它会受到不透明的影响。我只是想知道为什么相对定位会产生这样的效果。

​

代码语言:javascript
复制
.bg-1 {
  width: 100%;
  height: 80vh;
  background: url(img/keyboard.jpg) no-repeat center center;
  background-position: top;
  background-size: cover;
  opacity: .3;
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}

#welcome {
  padding-top: 15px;
  color: #66FCF1;
  z-index: 1;
  position: relative;
  /*this is the point of concern*/
}

.first {
  width: 100%;
  height: 80vh;
  background-color: black;
}
代码语言:javascript
复制
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container-fluid first text-center">
  <div id="welcome">
    <p class="welcomeSize">Welcome!</p>
  </div>
  <div class="bg-1">
    <!--the bg image with opacity-->
  </div>
</div>

​

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-07 01:50:36

你看到的是z-index。即使您已经有了一个z-index on #welcome,但z-index属性在向带有z-index的元素中添加非static position之前不会生效。因此,添加position: relative会激活#welcome上的z-index,将#welcome放在.bg-1之上而不是后面。

票数 4
EN

Stack Overflow用户

发布于 2017-03-07 01:51:09

默认情况下,元素按html中指定的顺序加载。在本例中,您的.bg-1元素在#welcome元素之后加载(因此,也就是在此基础上)。

同样, is only applied to positioned elements (即。fixed、relative和absolute)。应用position:relative时,您将触发要应用的z-index,并“提升”背景图像上方的#welcome元素。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42638715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档