需要帮助获取背景,以比剪辑提供更复杂的方式显示一些文本

问题描述 投票:0回答:1

请参阅附图,了解我要实现的目标。

基本上我需要背景图像来通过文本,除了文本与褪色面板重叠。这需要文本成为与面板本身相同的颜色/不可操作性。

这让我很头疼,但我想看看有多远/哪些解决方案可以实现我的目标。

谢谢,哈利。

enter image description here


编辑1:如果你想测试任何https://codepen.io/itsharryfrancis/pen/XVagep,这是一个codepen

我已经尝试过使用它,但确实如此

-webkit-background-clip: text;
-webkit-text-fill-color: transparent;

编辑2:我将在这里留下一个codepen,这是我迄今为止可能想要在未来看到它的任何人的最新版本。

https://codepen.io/itsharryfrancis/pen/goxVQP?editors=0100

html css clipping
1个回答
1
投票

这是实现所需外观的工作示例,但它使用相对较新的clip-path属性,即IE和Edge中的not supported。这个问题可以通过使用SVG剪辑来解决(或作为回退),但我希望这对于开始就足够了。

.example {
    width: 600px;
}

.example {
    background: url('http://dummy-images.com/nature/dummy-1024x576-Waterfalls.jpg');
    background-position: center center;
    background-size: cover;
    position: relative;
}

.text {
    width: 100%;
    font-family: Helvetica, Arial, sans-serif;
    font-weight: 300;
    font-size: 100px;
    line-height: 1.0;
    text-transform: uppercase;
}

.part1, .part2 {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.part1:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 400px;
    background-color: rgba(255, 255, 255, 0.5);
    z-index: 1;
}

.part1 .text {
    position: relative;
    z-index: 2;
    background: url('http://dummy-images.com/nature/dummy-1024x576-Waterfalls.jpg');
    background-position: center center;
    background-size: 100%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.part2 {
    color: rgba(255, 255, 255, 0.5);
    z-index: 3;
    clip-path: polygon(400px 0px, 100% 0px, 100% 100%, 400px 100%);
}
<div class="example">
  <div class="part1"><span class="text">Stack<br>Overflow</span></div>
  <div class="part2"><span class="text">Stack<br>Overflow</span></div>
 </div>
 

更新:我意识到初始版本在文本下的背景错位。更新版本修复了这个问题,但价格很低:.text元素需要有width: 100%,因为它允许对齐背景。如果文本应该另外对齐,则需要在一个案例中添加一些padding

© www.soinside.com 2019 - 2024. All rights reserved.