【问题标题】:How can I fill a text with an image in HTML? [duplicate]如何用 HTML 中的图像填充文本? [复制]
【发布时间】:2019-11-27 19:12:41
【问题描述】:

假设我有这样的图像:

我想用该图像填充<h1> 标记,使其看起来像这样:

我想我可以做这样的事情来开始......

div {
  width: 100%;
  height: 100%;
  background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
  background-size: cover;
}

h1 {
  color: white;
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
<div>
  <h1>404</h1>
</div>

我应该有另一个“绝对”的 div 白色覆盖图像和透明字体吗?有谁知道如何做到这一点?

【问题讨论】:

  • 查看背景剪辑:文本;

标签: html css


【解决方案1】:

background-clip 可能是一个选项

https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip

background-clip CSS 属性设置元素的背景是否延伸到其边框框、填充框或内容框下方。

div {
  width: 100%;
  height: 100%;
}

h1 {
  color: transparent;
  font-size: 28vmax;
  background-size: cover;
  background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
  background-clip: text;
}
<div>
  <h1>404</h1>
</div>

mix-blend-mode:

https://css-tricks.com/almanac/properties/m/mix-blend-mode/

mix-blend-mode 属性定义元素的内容应如何与其背景混合。这意味着任何图像或文本、边框或标题都会受到此属性的影响。

还有

https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode

mix-blend-mode CSS 属性设置元素的内容应如何与元素的父级内容和元素的背景混合。

* {
  margin: 0;
}

div {
  width: 100%;
  height: 100vh;
  background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
  background-size: cover;
}

h1 {
  font-size: 15vmax;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  color: black;
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
  background: white;
  mix-blend-mode: screen;
}
<div>
  <h1>404</h1>
</div>

【讨论】:

    【解决方案2】:

    您可以尝试使用-webkit-background-clip: text;

    这称为敲除文本,但您应该能够获得所需的结果。

    这里有几个示例,但在 Photoshop 中执行此操作然后插入图像可能更容易。 (只是一种选择)

    /* 
      Based from this article from Divya Manian - 
      http://nimbupani.com/using-background-clip-for-text-with-css-fallback.html
    */
    
    * {
        margin: 0;
        padding: 0;
    }
    
    *,
    :before,
    :after {
        -webkit-box-sizing: border-box;
           -moz-box-sizing: border-box;
                box-sizing: border-box;
    }
    
    html,
    body {
        min-height: 100%;
    }
    
    body {
        font-family: 'Oswald', sans-serif;
        color: #fff;
        background-color: #000;
    }
    
    .wrapper {
        text-align: center;
    }
    
    .title {
        font-size: 2em;
        position: relative;
        margin: 0 auto 1em;
        padding: 1em 1em .25em 1em;
        text-align: center;
        text-transform: uppercase;
    }
    .title:after {
        position: absolute;
        top: 100%;
        left: 50%;
        width: 240px;
        height: 4px;
        margin-left: -120px;
        content: '';
        background-color: #fff;
    }
    
    /* Clip text element */
    .clip-text {
        font-size: 6em;
        font-weight: bold;
        line-height: 1;
        position: relative;
        display: inline-block;
        margin: .25em;
        padding: .5em .75em;
        text-align: center;
        /* Color fallback */
        color: #fff;
        -webkit-background-clip: text;
    
        -webkit-text-fill-color: transparent;
    }
    
    .clip-text:before,
    .clip-text:after {
        position: absolute;
        content: '';
    }
    
    /* Background */
    .clip-text:before {
        z-index: -2;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-image: inherit;
    }
    
    /* Text Background (black zone) */
    .clip-text:after {
        position: absolute;
        z-index: -1;
        top: .125em;
        right: .125em;
        bottom: .125em;
        left: .125em;
        background-color: #000;
    }
    
    /* Change the background position to display letter when the black zone isn't here */
    .clip-text--no-textzone:before {
        background-position: -.75em 0;
    }
    
    .clip-text--no-textzone:after {
        content: none;
    }
    
    /* Use Background-size cover for photo background and no-repeat background */
    .clip-text--cover,
    .clip-text--cover:before {
        background-repeat: no-repeat;
        -webkit-background-size: cover;
                background-size: cover;
      background-position: 50% 50%;
    }
    
    /* Background image from http://thepatternlibrary.com/ and http://lorempixel.com */
    .clip-text_one {
        background-image: url(https://picsum.photos/480/200?random);
    }
    
    .clip-text_two {
        background-image: url(https://picsum.photos/480/200?grayscale);
    }
    
    .clip-text_tree {
        background-image: url(https://picsum.photos/480/200?grayscale&random=2);
    }
    
    .clip-text_four {
        background-image: url(https://picsum.photos/480/200?grayscale&blur=3);
    }
    
    .clip-text_five {
        background-image: url(https://picsum.photos/480/200?grayscale);
    }
    
    .clip-text_six {
        background-image: url(https://picsum.photos/480/200?random=3);
    }
    
    .clip-text_seven {
        background-image: url(https://picsum.photos/480/200?random=4);
    }
    
    .clip-text_eight {
        background-image: url(https://picsum.photos/480/200?random=6);
    }
    
    .clip-text_nine {
        background-image: url(https://picsum.photos/480/200?random=5);
    }
    
    .clip-text_ten {
        background-image: url(https://picsum.photos/480/200?random=7);
    }
    
    .clip-text_eleven {
        background-image: url(https://picsum.photos/480/200?random=8);
        background-size: cover;
    }
    
    .clip-text_twelve {
        background-image: url(https://picsum.photos/480/200?random=9);
    }
    
    .clip-text_thirteen {
        background-image: url(https://picsum.photos/480/200?random=10);
    }
    
    .clip-text_fourteen {
        background-image: url(https://picsum.photos/480/200?random=11);
    }
    
    .clip-text_fifteen {
        background-image: url(https://picsum.photos/480/200?random=12);
    }
    <div class="wrapper">
          <p class="title">Play with background-clip text</p>
          <div class="clip-text clip-text_one">TEST</div>
      <div class="clip-text clip-text_fifteen clip-text--no-textzone">TEST</div>
          <div class="clip-text clip-text_twelve clip-text--cover">TEST</div>
      <div class="clip-text clip-text_tree clip-text--no-textzone">TEST</div>
          <div class="clip-text clip-text_two">TEST</div>
          <div class="clip-text clip-text_fourteen clip-text--cover">TEST</div>
          <div class="clip-text clip-text_tree">TEST</div>
          <div class="clip-text clip-text_eleven clip-text--cover">TEST</div>
          <div class="clip-text clip-text_four">TEST</div>
          <div class="clip-text clip-text_five">TEST</div>
          <div class="clip-text clip-text_six">TEST</div>
          <div class="clip-text clip-text_seven">TEST</div>
          <div class="clip-text clip-text_eight">TEST</div>
          
          <div class="clip-text clip-text_nine">TEST</div>
          <div class="clip-text clip-text_ten">TEST</div>
          <div class="clip-text clip-text_thirteen clip-text--cover">TEST</div>
      </div>

    如果您有任何问题,请告诉我!实现相当简单,我确保包含可供您查看的选项,这是一个链接以获取更多信息:

    https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2018-11-22
      • 2018-09-24
      • 2023-04-01
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多