【问题标题】:Text Highlights as CSS masks?文本高亮作为 CSS 掩码?
【发布时间】:2020-06-02 01:09:41
【问题描述】:

我想知道是否可以重现这种文字效果:

它应该看起来好像文本突出显示降低了图像的不透明度。我猜你需要的是背景层的副本,在文本高亮的形状/位置中被掩盖。但是有没有办法真正让这些蒙版根据文本行自动调整大小/重新定位?或者有什么其他方法可以达到效果?

这可能更好地解释了我所追求的:

【问题讨论】:

  • “这是我正在开发的网站...” -* 您可能希望删除这部分以避免被指责为垃圾邮件
  • 哦,不知道。对不起。
  • 一切都很好

标签: css text mask masking


【解决方案1】:

您可以使用 HTML 的 mark 标记,使用带有不透明度的背景颜色:

/*custom styling of higlihter*/
mark{
background: rgba(255, 255, 255, 0.38);
    color: black;
}
.wrap{
background-image: url("https://image.shutterstock.com/image-illustration/color-splash-series-background-design-260nw-587409425.jpg");
height: 230px;
width: 230px;
}
<div class="wrap">
Do <mark>not forget to buy milk today</mark>
<div>

注意:Internet Explorer 8 及更早版本不支持标记标签。

另一种解决方案,在带有gradient&lt;p&gt; 标签上使用backgroundcolor

.wrap{
 background: grey;
 color: white;
 display: inline-block;
 background:url(https://thumbs.dreamstime.com/b/halloween-background-full-moon-creepy-house-flying-bats-halloween-background-full-moon-creepy-house-125024932.jpg);
 background-size: cover;
 height: 200px;
 width: 200px;
}
p{
font-size:20px;
    background-image: linear-gradient(to right, #ffffff00, #000000c9 , #ffffff00);
    text-align: center;
}
&lt;div class="wrap"&gt;&lt;p&gt;Don't play with&lt;p&gt;&lt;/div&gt;

您可以相应地更改颜色。

Reference.

【讨论】:

  • 感谢您的提示,但这还不足以解决我的问题。我在原始帖子中添加了另一张图片以获得更好的解释。问题是棋盘背景(实际上是我的背景,不是 alpha 通道)上的亮点应该是“不可见的”,文本和背景之间没有任何内容。
  • @KT9000 您可以使用具有透明度的背景颜色来实现这一点;我会用后面的图片更新我的代码以便更好地理解
  • @KT9000 使用渐变色和背景色更新了我的答案,希望对您有所帮助,如果您仍然遇到问题,请告诉我,您可以在第二个解决方案中更改不透明度和覆盖该区域的颜色百分比
  • 我已经更新了我的模型,不幸的是渐变不是一个选项,因为有多个透明图像在文本后面移动。另外,我不能对高光使用纯色,因为我希望它们显示背景图像/图案。
  • 感谢您的所有努力!看起来 CSS 中有完美的解决方案,所以我想的太复杂了,而不是仅仅查找所有可用的背景属性。
【解决方案2】:

您可能正在寻找 css 属性 background-attachment: fixed。这确实需要注意,背景将不再随页面滚动并保持静态,但这样可以保证元素背景和容器背景之间的重叠保持不变。有一个通过 javascript 解决滚动问题的方法,只需少量开销,具体取决于浏览器渲染/重新定位的图形的重量。

然后,您只需将相同的背景应用于包含元素的背景(在我的情况下为 .wrap)和包含元素的文本(在我的情况下为 wrap),您将获得所需的效果,如第二张图片所示。

然后将标记放在段落元素中并重复文本两次。一次进入段落,一次进入标记。
然后将段落设置为相对位置,并将标记设置为绝对位置,这样它们就可以完美地重叠。这是为了抵消换行是透明的并且不能正确显示文本,因为文本也会变得透明。

.wrap, .wrap mark {
   background-image: url('https://i.imgur.com/hAodNjT.jpg');
   background-attachment: fixed;
}
.wrap p {
  position: relative;
}
.wrap mark {
   position: absolute;
   top: 0px;
   left: 0px;
   opacity: 0.4;
}
img {
   width: 300px;
   height: auto;
}
.wrap {
   padding-top:160px;
   position: relative;
   width: 400px;
   height: 400px;
}
.wrap img {
   position:absolute;
   top:60px;
   
   z-index:0;
}
.wrap p {
    position:relative;
    z-index: 1;
}
<div class="wrap">
  <img src="https://i.imgur.com/cULI8TG.png">
  <p>some text that runs over the image<mark>some text that runs over the image</mark></p>
  <p>some other text that runs over the image<mark>some other text that runs over the image</mark></p>
</div>

带有背景滚动修复,滚动时确实会引入更多开销

var $affected = $('.wrap, .wrap mark');
let handler = (e) => {
    $affected.css({'background-position' : '-'+window.scrollX+'px -'+window.scrollY+'px'});
}
$(window).on('resize scroll', handler);
.wrap, .wrap mark {
   background-image: url('https://i.imgur.com/hAodNjT.jpg');
   background-attachment: fixed;
}
.wrap p {
  position: relative;
}
.wrap mark {
   position: absolute;
   top: 0px;
   left: 0px;
   opacity: 0.4;
}
img {
   width: 300px;
   height: auto;
}
.wrap {
   padding-top:160px;
   position: relative;
   width: 400px;
   height: 400px;
}
.wrap img {
   position:absolute;
   top:60px;
   
   z-index:0;
}
.wrap p {
    position:relative;
    z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
  <img src="https://i.imgur.com/cULI8TG.png">
  <p>some text that runs over the image<mark>some text that runs over the image</mark></p>
  <p>some other text that runs over the image<mark>some other text that runs over the image</mark></p>
</div>

【讨论】:

  • 哇哦!这正是我想要的,而且比我想象的要容易得多。不过,我必须阅读如何在 Webflow 中嵌入自定义 CSS,希望它有效。非常感谢!
  • @KT9000 如果您需要背景滚动,我还添加了一个修复程序。
猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 2021-08-03
相关资源
最近更新 更多