css - 将混合模式与 2 个重叠的 div 混合,但需要排除文本

标签 css mix-blend-mode

我正在 flex 2 个元素,它们大约重叠 25%。 使用 mix-blend-mode,它很好地混合了背景,也混合了文本。

JS fiddle 就在这里:https://jsfiddle.net/simohell/q9breg60/

我尝试使用::before 来应用 mix-blend 模式,但我无法弄明白。

.wrapper {
  display: flex;
}

.text {
  margin-top: 30px;
  flex: 0 0 50%;
  padding: 30px;
  background-color: rgba(17, 38, 59, .95);
  mix-blend-mode: multiply;
}

h1,
p {
  color: #fff;
}

.image {
  flex: 0 0 75%;
  margin-left: -25%;
  background: url("https://images.unsplash.com/photo-1456298964505-ef9e1a638209?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2534&q=80");
  background-size: cover;
  mix-blend-mode: multiply;
}
<div class="wrapper">
  <div class="text">
    <h1>Welcome to this snippet</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ea dolore quaerat cupiditate accusantium eligendi vitae quas omnis obcaecati unde deserunt. Quibusdam aspernatur magni accusamus debitis distinctio iusto aliquam natus magnam?</p>
  </div>
  <div class="image">&nbsp;</div>
</div>

最佳答案

将文本的背景移至主包装,然后增加其 z-index 以允许图像仅与背景混合。主要技巧是调整渐变的大小以仅模拟文本的相同背景:

.wrapper {
  display: flex;
  background:
    linear-gradient(rgba(17, 38, 59, .95),rgba(17, 38, 59, .95)) 
    bottom left/ 
    50% /*widh:50% */
    calc(100% - 30px) /*height:100% - top margin */
    no-repeat;
}

.text {
  margin-top: 30px;
  flex: 0 0 50%;
  padding: 30px;
  z-index:2;
}

h1,p {
  color: #fff;
}

.image {
  flex: 0 0 75%;
  margin-left: -25%;
  background: url("https://images.unsplash.com/photo-1456298964505-ef9e1a638209?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2534&q=80");
  background-size: cover;
  mix-blend-mode: multiply;
}

body {
 margin:0;
}

* {
  box-sizing:border-box;
}
<div class="wrapper">
  <div class="text">
    <h1>Welcome to this snippet</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ea dolore quaerat cupiditate accusantium eligendi vitae quas omnis obcaecati unde deserunt. Quibusdam aspernatur magni accusamus debitis distinctio iusto aliquam natus magnam?</p>
  </div>
  <div class="image">&nbsp;</div>
</div>

您还可以将图像移动到主包装器的背景并考虑 background-blend-mode。您还将获得简化的代码:

.wrapper {
  overflow:auto;
  background:
  url("https://images.unsplash.com/photo-1456298964505-ef9e1a638209?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2534&q=80") 
    center/cover content-box,  
  linear-gradient(rgba(17, 38, 59, .95),rgba(17, 38, 59, .95)) 
    bottom left/ 
    50% /*widh:50% */
    calc(100% - 30px) /*height:100% - top margin */
    no-repeat;
  padding-left:25%;
  background-blend-mode: multiply;
}
.text {
  margin-top: 30px;
  width:66.6%; /* we added 25% of padding so we need 66.6% from 75% to get 50%*/
  margin-left:-33.4%; /* we shift back by the added margin 25% (33.4% of 75%)*/
  padding: 30px;
}
h1,p {
  color: #fff;
}

body {
 margin:0;
}

* {
  box-sizing:border-box;
}
<div class="wrapper">
  <div class="text">
    <h1>Welcome to this snippet</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ea dolore quaerat cupiditate accusantium eligendi vitae quas omnis obcaecati unde deserunt. Quibusdam aspernatur magni accusamus debitis distinctio iusto aliquam natus magnam?</p>
  </div>
</div>

关于css - 将混合模式与 2 个重叠的 div 混合,但需要排除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184246/

相关文章:

android - 将 mix-blend-mode 设置为除 "normal"之外的任何值都会隐藏 Android 9 上 Chrome 中的元素

css - 混合混合模式并不总是有效

css - 混合混合模式在应用于一个元素但不适用于另一元素时起作用

javascript - 有没有办法用javascript计算svg元素的混合模式(css)?

javascript - 循环播放图像幻灯片 (html/js)

javascript - 如何将外部 CSS 文件分配给 javascript 构造的 SVG 对象?

javascript - 页面重叠在页脚 div 上

html - 希望子菜单项在文本长于宽度时换行

css - 延迟时的动画 CSS 下划线

html - Chrome/Safari 最近是否更改了 mix-blend-mode 的处理方式