【问题标题】:Clippingmask from text and its parent div来自文本及其父 div 的剪贴蒙版
【发布时间】:2021-11-21 19:01:13
【问题描述】:

我看到有几个人问过这个问题,但它总是被关闭并重定向到一个类似的问题(这是不同的)。

问题是:如何从文本和文本元素的最近的父div制作剪贴蒙版?不要混淆如何根据文本和文本背景制作剪贴蒙版。

非常感谢任何帮助。如果您可以在父 div 中制作淘汰文本,这将带来惊人的多功能性!

谢谢!

【问题讨论】:

  • 你能举一个你已经尝试过的例子吗?另外,您能否更清楚地解释一下您要做什么? (从父母内容或其他内容中剪下文本的形状?)
  • 嘿@Philip 感谢您的回复。我想在背景 div 中创建剔除文本,以便背景 div 下方的主体或图层可以发光。流行的是使用 -webkit-text-fill-color: transparent;和 webkit-background-clip:文本;在文本背景中创建剔除文本。我不想要那个,我想要父 div(背景)中的剔除文本
  • 所以本质上是background-clip: text 的倒数?看起来像这样:stackoverflow.com/questions/15507401/…
  • 由于background-clip: text 不存在真正的逆,trick they use here 在文本上使用相同的背景并使用background-clip: text。然后在中间放点别的东西。文本(与使用的背景颜色相同)必须恰到好处地定位,使其看起来看起来像透明,但实际上并非如此。

标签: css clip clip-path


【解决方案1】:

background-clip: text 属性将除文本之外的所有内容从背景中删除,使文本着色/填充为背景颜色/图像。 (当然,如果您将文本颜色设置为transparent。)

你想要的恰恰相反。但由于background-clip: text 没有真正的逆,你可以使用the trick they use here

方法 1:CSS 技巧使其看起来像剪切文本

它的作用是让您认为文本是透明的,方法是使用相同的背景填充/着色文本并在其间放置一些其他内容以进行区分。

在你的屏幕上你会看到例如:

  1. 红色背景。 (使用 ::after 元素创建。)
  2. 较小的黑色元素(使其周围的红色背景元素可见)。 (使用 ::before 元素创建。)
  3. 顶部有红色文字。 (原始元素。)

现在它似乎文本是透明的,因为它与红色背景元素具有相同的填充。所以诀窍是让文本和背景具有相同的填充和大小,并以相同的方式定位填充。

工作示例:

/*
  Contains only the text that is
  filled/colored with the background.
*/
.cut-out-text {
    position: relative;
    font-size: 100px;
    font-weight: bold;
    padding: 50px;
    background: linear-gradient(to right, red, blue);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
    -webkit-text-fill-color: transparent;
}

/*
  Layer in between. (Black in this case.)
    This layer is sized just a bit smaller than the
    container to leave the `::after` visible around.
*/
.cut-out-text::before {
    content: '';
    display: inline-block;
    position: absolute;
    z-index: -1;
    top: 10px;
    right: 10px;
    bottom: 10px;
    left: 10px;
    background: #000;
}

/*
  Layer that repeats the original background
    and appears to be the real background,
  making it look like the text is cut out.
*/
.cut-out-text::after {
    content: '';
    display: inline-block;
    position: absolute;
    z-index: -2;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: inherit;
    background-image: inherit;
}
<div class="cut-out-text">Hello World</div>

方法 2:使用 SVG

如果背景不像颜色或线性渐变那样简单,您可以使用 SVG 图像覆盖背景。

例如,这个 SVG 的内容是带有遮罩或剪切路径的全尺寸 <rect>

此蒙版或剪切路径将包含应显示rect 部分的黑白内容。

在此掩码中,您可以放置​​文本以将其剪掉。 See examples here.

提示:如果您不想要一个全尺寸的黑色 <rect> 并将文本剪切为重叠内容,您还可以使用带有常规 HTML 的 <foreignObject>(其中可以使用 CSS 类等进行样式设置)。 (See this JSFiddle example.)

#some-header-background {
    width: 100%;
    height: 100px;
    background: linear-gradient(to right, red, blue);
}

#overlapping-content-with-text-cut-out {
    width: 100%;
    height: 100%;
}

text.cut-out-text {
    font: bold 60px sans-serif;
}
<div id="some-header-background">
  
  <svg id="overlapping-content-with-text-cut-out" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <mask id="cut-out-text">
        <rect x="0" y="0" width="100%" height="100%" fill="white" />
        <text class="cut-out-text"
          fill="black"
          x="0%" y="100%"
        >Hello World</text>
        <!--
           More about styling and position text in SVG:
           https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text
           https://developer.mozilla.org/en-US/docs/Web/SVG/Element/tspan
        -->
      </mask>
    </defs>

    <rect x="0" y="0" width="100%" height="100%" mask="url(#cut-out-text)" />
    <!-- OR use HTML for overlapping content: -->
    <!--
    <foreignObject x="0" y="0" width="100%" height="100%" mask="url(#cut-out-text)">
      <div class="overlapping-content">Overlapping</div>
    </foreignObject>
    -->
  </svg>
  
</div>

其他方法请查看this question

【讨论】:

  • 计划是在背景中有一个动画,可以通过文本看到。因此,您的解决方案是不可能的,因为没有实际的透明度。据您所知,有没有一种方法可以在不使用技巧的情况下创建带有文本及其父 div 的剪贴蒙版?
  • 正如我所写,background-clip: text 在 CSS 中没有直接的逆。但是您可以使用 SVG 图像来覆盖背景动画。例如,这个 SVG 的内容是带有掩码的全尺寸 &lt;rect&gt;。这个&lt;mask&gt; 将包含一个黑白内容,其中将显示rect 的部分内容。在这个掩码中,您可以放置​​文本以将其剪掉。或者使用 SVG 的剪辑。 See examples here.
  • 我已将 SVG 方法添加到答案中。
猜你喜欢
  • 2017-03-09
  • 2011-06-23
  • 1970-01-01
  • 2012-07-07
  • 2013-08-16
  • 2011-10-20
  • 2016-07-07
  • 1970-01-01
  • 2011-06-27
相关资源
最近更新 更多