如何用HTML图像填充文本?

时间:2019-07-18 19:29:35

标签: html css

假设我有一个这样的图像:

我想用该图像填充一个<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白色覆盖图像并用透明字体覆盖吗?有人知道如何做到这一点吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以尝试使用-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

相关问题