【问题标题】:CSS mix-blend-mode not working if the background image is placed in another tag如果背景图像放在另一个标签中,CSS mix-blend-mode 不起作用
【发布时间】:2021-04-23 16:58:00
【问题描述】:

我有一个带有headerbanner 的简单页面布局。

我想让header 透明并位于banner 图像的顶部(稍后我可能会将其转换为滑块,为简单起见,我使用了单个图像)。

header 里面的menu 因为背景图片的原因看不清楚,所以我决定使用mix-blend-mode: difference,我应该这样做。

但是文本没有改变颜色。

正如您在下面的代码 sn-p 中看到的,我已将 mix-blend-mode 应用于 <a>这只有在我给 li 一个背景颜色时才有效

我认为问题在于图像元素与mix-blend-mode 元素位于不同的标签中。我无法弄清楚问题出在哪里。任何帮助将不胜感激。

.header {
  position: absolute;
  width: 100%;
  top: 50px;
  left: 0;
  z-index: 9;
}

.header .header__bottom ul {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: right;
  position: relative;
}

.header .header__bottom ul li {
  display: inline-block;
}

.header .header__bottom ul li a {
  text-decoration: none;
  display: block;
  padding: 0 20px;
  color: #fff;
  mix-blend-mode: difference;
}

.banner img {
  width: 100%;
}
<section class="header">
  <div class="header__bottom">
    <div class="logo">
      <img src="https://www.imltravel.com/wp-content/uploads/2017/03/IML-LOGO-VECTOR-WEBSITE-SMALL-e1489572911433.png">
    </div>
    <div class="header__menu">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Tour Packages</a></li>
        <li><a href="#">Visa</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Team</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </div>
</section>

<section class="banner">
  <img src="https://images.unsplash.com/photo-1503220317375-aaad61436b1b?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8OHx8fGVufDB8fHw%3D&w=1000&q=80">
</section>

【问题讨论】:

  • 保留您的初始代码并仅添加徽标,否则您将使我的回答无关紧要
  • 哦好吧...我会改的。

标签: html css mix-blend-mode


【解决方案1】:

.header 正在创建一个堆栈上下文,因为它定义了 z-index

要么删除 z-index:

.header {
  position: absolute;
  width: 100%;
  top: 50px;
  left: 0;
}

.header .header__bottom ul {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: right;
  position: relative;
}

.header .header__bottom ul li {
  display: inline-block;
}

.header .header__bottom ul li a {
  text-decoration: none;
  display: block;
  padding: 0 20px;
  color: #fff;
  mix-blend-mode: difference;
}

.banner img {
  width: 100%;
}

body {
 margin:0
}
<section class="header">
  <div class="logo">
      <img src="https://www.imltravel.com/wp-content/uploads/2017/03/IML-LOGO-VECTOR-WEBSITE-SMALL-e1489572911433.png">
    </div>
  <div class="header__bottom">
    <div class="header__menu">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Tour Packages</a></li>
        <li><a href="#">Visa</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Team</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </div>
</section>

<section class="banner">
  <img src="https://images.unsplash.com/photo-1503220317375-aaad61436b1b?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8OHx8fGVufDB8fHw%3D&w=1000&q=80">
</section>

或者将 mix-blend-mode 移到页眉(但这也会影响 logo):

.header {
  position: absolute;
  width: 100%;
  top: 50px;
  left: 0;
  z-index:9;
  mix-blend-mode: difference;
}

.header .header__bottom ul {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: right;
  position: relative;
}

.header .header__bottom ul li {
  display: inline-block;
}

.header .header__bottom ul li a {
  text-decoration: none;
  display: block;
  padding: 0 20px;
  color: #fff;
}

.banner img {
  width: 100%;
}
<section class="header">
  <div class="logo">
      <img src="https://www.imltravel.com/wp-content/uploads/2017/03/IML-LOGO-VECTOR-WEBSITE-SMALL-e1489572911433.png">
    </div>
  <div class="header__bottom">
    <div class="header__menu">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Tour Packages</a></li>
        <li><a href="#">Visa</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Team</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </div>
</section>

<section class="banner">
  <img src="https://images.unsplash.com/photo-1503220317375-aaad61436b1b?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8OHx8fGVufDB8fHw%3D&w=1000&q=80">
</section>

应用了混合的元素必须与该元素所属的堆叠上下文的所有底层内容混合。 ref

【讨论】:

  • 将内容移到页眉对我有帮助。 z-index 不在我的实际代码中(我正在处理的代码)。这真的很有帮助!但它也适用于看起来很糟糕的标志。我可以从标题下的混合混合模式中排除元素吗?
  • 现在logo好像全部倒过来了。
  • 我在上面添加了一个额外的代码sn-p。谢谢!
  • @DebsmitaPaul 如果您考虑我的第一个没有 z-index 的代码,它会正常工作
猜你喜欢
  • 1970-01-01
  • 2020-06-09
  • 2022-11-21
  • 2021-04-30
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多