【问题标题】:How to fix an issue with background-image opacity causing text to be unreadable? [duplicate]如何解决背景图像不透明度导致文本不可读的问题? [复制]
【发布时间】:2021-03-16 21:03:22
【问题描述】:

我的背景有问题。我想要和截图 1 一样,但我和截图 2 一样。

问题出在哪里?

html,
body,
.bg-for-1 {
  height: 100%;
}

.bg-for-1 {
  background: black;
}

.bg-for-1 .first {
  position: absolute;
  opacity: 0.2;
  width: 100%;
  height: 100%;
  background-image: url(http://digitalresult.com/wp-content/uploads/2016/06/beautiful-reflection-at-lake-pier-wallpaper-32.jpeg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.bg-for-1 .first h3 {
  color: white;
  position: relative;
  text-align: center;
}
<div class="bg-for-1"><div class="first"><h3>Hello</h3></div></div>

【问题讨论】:

  • 缺少您的 html 以清楚地证明您的问题 ;) 您可以更新您的 sn-p ;) 但是,看起来像:.first h3 使 h3 成为 .first 的子级,其中设置了不透明度,所以它也是半透明的..
  • 你好

标签: css background-image opacity


【解决方案1】:

由于您需要用黑色使图像变暗,因此您可以使用半透明渐变在图像上设置深色叠加层,这不会对内容产生影响:

How to add a color overlay to a background image? 相关的示例 + 2 个其他方法

html,
body,
.bg-for-1 {
  height: 100%;
}

.bg-for-1 {
color:white;}

.bg-for-1 .first {
  display: flex;
  flex-flow:column;
  justify-content:center;
  align-items: center;

  width: 100%;
  height: 100%;
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(https://picsum.photos/id/1015/6000/4000);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.bg-for-1 .first h3 {
  color: white;
  position: relative;
  text-align: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="bg-for-1">
  <div class="first">
    <h1>Nice Portfolio!</h1>
    <h3>Find something for you!</h3>
    <button class="btn btn-danger mt-3">Press</button>
  </div>
</div>

<div class="second">
  <h3>Lot of amazing projects for you and your team!</h3>
  <h5>Just simply and nice projects for you and your team , check what we recomandate for you, we work fast and clear.</h5>
  <button type="button" class="btn btn-dark mt-5">Press And Check!</button>
</div>

<div>new div goes on previous</div>

【讨论】:

  • @ArtursGolis 如果你删除 absolute ,那么你可以在下面添加内容
【解决方案2】:

在 css 中将 contain 用于 background-size

.bg-for-1 .first {
  ...
  background-image: url(http://digitalresult.com/wp-content/uploads/2016/06/beautiful-reflection-at-lake-pier-wallpaper-32.jpeg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  ...
}

【讨论】:

  • 这是关于不透明度 ;) 不是我相信的 bg 大小。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2019-08-02
  • 2016-09-06
  • 1970-01-01
  • 2011-10-16
  • 2011-11-10
  • 2020-01-30
  • 2015-12-10
相关资源
最近更新 更多