【问题标题】:Cut-out text with CSS使用 CSS 剪切文本
【发布时间】:2014-04-30 20:02:01
【问题描述】:

我尝试在 CSS 中为按钮创建悬停效果。

基本上,文本应从其父元素中“剪掉”,使其对网站背景透明。
我会用渐变做条纹,但我的问题是为字体添加透明度。
我查看了背景剪辑,但这与我试图实现的目标相反,并且会使事情变得更加复杂。 有没有简单的方法来实现这个效果?我不介意使用 JS,但如果可能的话,不要使用 jQuery。

【问题讨论】:

标签: html css button styles


【解决方案1】:

在下面的示例中,我使用 CSS mix-blend-mode,混合模式为 difference

@import url(http://fonts.googleapis.com/css?family=Raleway:900,600,500,800,700);
* {
  box-sizing: border-box;
}
body {
  font-family: 'Raleway', sans-serif;
  background-color: white;
}
div {
  position: relative;
}
img {
  width:100%;
}
h1 {
  color: #000;
  background-color: #9c9c9c;
  padding: .5em 1em;
  position: absolute;
  bottom: 0;
  width: 100%;
  font-size: 10vw;
  font-weight: 900;
  text-transform: uppercase;
  mix-blend-mode: difference;
}
<div>
  <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9674/isolation-destination.jpg" alt="" />
  <h1>Let the sun shine!</h1>
</div>

View on CodePen
View my blog post on compositing and blending

【讨论】:

  • 仅链接到您自己的库(或实用程序)或其他人的库不是一个好的答案。事实上,它被认为不是答案。链接到它,解释它解决问题的原因,提供使用它的代码并声明是一个很好的答案。见:How can I link to an external resource in a community-friendly way?
  • 这是我第一次回答问题...感谢您的反馈,我会努力做得更好!
  • 这在 Linux Mint Firefox 中不起作用。不过,它适用于 Chrome。不过效果很棒。
  • caniuse.com/#search=mix-blend-mode - 没有 Edge 或 IE。部分 Safari
【解决方案2】:

尝试使用 Photoshop 创建仅具有轮廓且没有背景颜色的文本,然后将其保存为 .png 格式,然后将该文本放入 div 并用渐变装饰此 div。 希望这会有所帮助

检查在 Photoshop 中创建的以下图像.......

【讨论】:

  • 不要粗鲁,但现在是 2014 年,我永远不会再使用 Photoshop 进行此类操作。除此之外,这仍然不起作用,只会使文本透明,但不会将其从父母背景中删除。
  • 是的,我知道 Photoshop 确实很耗时,但它可以帮助您获得想要的结果。检查我的新编辑答案:)。希望这对您有所帮助..
【解决方案3】:

关注我的评论和链接,使用 svg ,你可以获得这种东西: http://codepen.io/gc-nomade/pen/Dqcio/

svg {
  position:absolute;
  background:repeating-linear-gradient(-45deg,
    transparent,
    transparent 5px,
    black 5px,
    black 10px
    );
  width:600px;
  height:300px;
  box-sizing:border-box;
  background-clip: content-box; 
  padding:60px 70px;
}
text {
  font-size:8em;
  fill:url(#textpattern);
  stroke: white;
  border:solid;
  }
div {
  position:relative;
  width:600px;
  margin:auto;
}

和标记:

    <div>
  <svg>
    <defs>
      <pattern id="textpattern" patternUnits="userSpaceOnUse" width="600" height="300" >
        <image xlink:href="http://lorempixel.com/600/300/nature/9" width="600" height="300"  x="-70px" y="-60px"/>
      </pattern>
    </defs>
    <text  y="120px" x="140px">test </text>
  </svg>
  <img src="http://lorempixel.com/600/300/nature/9" />
</div>

使用 CSS ,您甚至可以添加透明边框和半径以使其看起来更怪异http://codepen.io/gc-nomade/pen/wsfvg/

【讨论】:

  • 这适用于我的最新版本:Firefox、Opera、Safari、Internet Explorer、Chrome,在 w7 下
猜你喜欢
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2012-02-23
  • 1970-01-01
  • 2010-12-05
相关资源
最近更新 更多