【问题标题】:CSS: how to have transparent background with the blend mode only affecting/being clipped to the (text) content?CSS:如何使混合模式具有透明背景,仅影响/被剪辑到(文本)内容?
【发布时间】:2020-12-30 14:28:30
【问题描述】:

这是我的代码。我认为我想要实现的目标相当明显 - 我想删除文本后面的黑色背景,但是,当我简单地删除黑色背景 css 时,渐变层只会显示,因为它没有被剪辑到文本中。

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background:url('https://wallpapersite.com/images/pages/pic_w/16658.jpg') no-repeat center center fixed;
    background-size: cover;
}

.neon {
    position: relative;
    overflow: hidden;
    filter: brightness(200%);
}

.text {
    background-color: black;
    color: white;
    font-size: 100px;
    font-weight: bold;
    font-family: sans-serif;
    text-transform: uppercase;
    position: relative;
    user-select: none;
}

.text::before {
    content: attr(data-text);
    position: absolute;
    color: white;
    filter: blur(0.02em);
    mix-blend-mode: difference;
}

.gradient {
    position: absolute;
    background: linear-gradient(45deg, #00f7ff, #f545d7, #0085ff, #6945f5, #005aff);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    mix-blend-mode: multiply;
}

.spotlight {
    position: absolute;
    top: -100%;
    left: -100%;
    right: 0;
    bottom: 0;
    background: 
        radial-gradient(
            circle,
            white,
            transparent 25%
        ) center / 25% 25%,
        radial-gradient(
            circle,
            white,
            black 25%
        ) center / 12.5% 12.5%;
    animation: light 5s linear infinite;
    mix-blend-mode: color-dodge;
}

@keyframes light {
    to {
        transform: translate(50%, 50%);
    }
}
<div class="neon">
  <span class="text" data-text="NIGHT SKY">NIGHT SKY</span>
  <span class="gradient"></span>
  <span class="spotlight"></span>
</div>

无论如何,我已经尝试了一些事情,比如弄乱一些mix-blend-modes,添加一些剪切路径等,但无法接近工作。我认为它应该(?)是可能的,但如果不是,我明天会尝试其他一些方法(比如将文本转换为 png 并使用background-clip 属性)

【问题讨论】:

    标签: html css background linear-gradients mix-blend-mode


    【解决方案1】:

    另一个mix-blend-mode 将修复它。在这种情况下使用lighten 删除您不想要的黑色部分:

    html {
      background:#fff; /* this is needed to make sure the blending works fine (any color will do the trick) */
    }
    
    body {
      margin: 0;
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      background: url('https://wallpapersite.com/images/pages/pic_w/16658.jpg') no-repeat center center fixed;
      background-size: cover;
    }
    
    .neon {
      position: relative;
      overflow: hidden;
      filter: brightness(200%);
      mix-blend-mode:lighten; /* here */
    }
    
    .text {
      background-color: black;
      color: white;
      font-size: 100px;
      font-weight: bold;
      font-family: sans-serif;
      text-transform: uppercase;
      position: relative;
      user-select: none;
    }
    
    .text::before {
      content: attr(data-text);
      position: absolute;
      color: white;
      filter: blur(0.02em);
      mix-blend-mode: difference;
    }
    
    .gradient {
      position: absolute;
      background: linear-gradient(45deg, #00f7ff, #f545d7, #0085ff, #6945f5, #005aff);
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      mix-blend-mode: multiply;
    }
    
    .spotlight {
      position: absolute;
      top: -100%;
      left: -100%;
      right: 0;
      bottom: 0;
      background: radial-gradient( circle, white, transparent 25%) center / 25% 25%, radial-gradient( circle, white, black 25%) center / 12.5% 12.5%;
      animation: light 5s linear infinite;
      mix-blend-mode: color-dodge;
    }
    
    @keyframes light {
      to {
        transform: translate(50%, 50%);
      }
    }
    <div class="neon">
      <span class="text" data-text="NIGHT SKY">NIGHT SKY</span>
      <span class="gradient"></span>
      <span class="spotlight"></span>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2018-10-19
      • 2021-10-14
      • 2010-12-16
      • 2013-09-15
      • 2017-12-13
      • 2015-03-31
      相关资源
      最近更新 更多