简体   繁体   English

CSS中彩色背景上的透明文本

[英]Transparent text on colored background in CSS

Is there possible to show some text in a div/span/etc with colored background AND transparent characters with CSS ? 是否可以在div / span / etc中使用CSS带有彩色背景和透明字符的文本? I attached an image to simply show what I want to do. 我附加了一张图像,以简单地显示我想做什么。

my HTML code: 我的HTML代码:

<div id="container">
    <div id="textbox">this text is transparent</div>
</div>

my CSS code: 我的CSS代码:

#container {
    display: block;
    width: 400px;
    height: 200px;
    background-image: url('...path/to/myimage.jpg');
}
#textbox {
    background: #000;
    color: transparent; /* ??? */
}

通过CSS在图像上显示透明文本

You can use SVG, it needs some extra to tuning: width of svg in order to see all text, set a background pattern same as background. 您可以使用SVG,它需要进行一些调整:svg的宽度才能查看所有文本,并设置与背景相同的背景图案。

SVG text do not wrap yet (svg 2.0 should). SVG文本还没有换行(svg 2.0应该)。

the only advantage is that it is supported by Firefox when background-clip:text; 唯一的好处是,当background-clip:text;受到Firefox的支持。 does not. 才不是。

DEMO DEMO

HTML + SVG inline content: HTML + SVG内联内容:

</div>
  <svg height="5em" width="32em">
    <defs>
      <pattern id="textpattern" patternUnits="userSpaceOnUse" width="1000" height="1000" >
        <image xlink:href="http://lorempixel.com/600/300/nature/9" width="600" height="300"  x="-3em" y="-3em"/>
      </pattern>
    </defs>
    <text  y="1.2em" x="0.5em" >​this text is transparent</text>
  </svg>
</div>

Associated CSS to demo : 关联的CSS进行演示:

text {
  font-size:3em;
  fill:url(#textpattern) ;
}
svg {
  background:rgba(0,0,0,0.9);
  margin:2em 0 0 2em;
}
#container {
  padding:1em;
  background:url(http://lorempixel.com/600/300/nature/9) no-repeat;
}

One option is to use -webkit-background-clip . 一种选择是使用-webkit-background-clip

CSS-tricks article "Show Image Under Text" gives you some good options. CSS技巧文章“在文本下显示图像”为您提供了一些不错的选择。

Check this out: http://www.w3schools.com/cssref/css3_pr_background-clip.asp (and as @Josh mentioned, http://css-tricks.com/image-under-text/ ) 检查一下: http : //www.w3schools.com/cssref/css3_pr_background-clip.asp (以及@Josh提到的http://css-tricks.com/image-under-text/

Looks like that it's well supported (with the latest browsers at-least) 看起来它受到了很好的支持(至少有最新的浏览器)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM