简体   繁体   English

使用边框使文本透明

[英]Make Text transparent with border

I'm trying to display text (a number), which is displayed on a background image.我正在尝试显示显示在背景图像上的文本(数字)。

What I currently got is the following:我目前得到的是以下内容:

 body { background-image: url("https://i.picsum.photos/id/10/800/800.jpg"); background-repeat: no-repeat; background-position: center; width: 400px; height: 400px; } #age { /* 1 pixel black shadow to left, top, right and bottom */ text-shadow: 2px 0 0 #fff, -2px 0 0 #fff, 0 2px 0 #fff, 0 -2px 0 #fff, 1px 1px #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff; font-family: sans; color: transparent; font-size: 40vw; }
 <h1 id="age">27</h1>

Basically, it works more or less as I want.基本上,它或多或少地按我的意愿工作。 However, the number itself is always filled with white color.但是,数字本身总是用白色填充。 What I would like though, is to have that number displayed huge there, have it filled with no color (transparent) and only use it with a border around the number itself.不过,我想要的是让那个数字在那里显示得很大,让它没有颜色(透明)填充,并且只在数字本身周围使用边框。 Later on, I want to upgrade to a counter, that visibly counts to the defined number first.稍后,我想升级到一个计数器,它首先明显地计数到定义的数字。 However, the first step for me would be to display the number without the white fill.但是,对我来说,第一步是显示没有白色填充的数字。

Do you know any possible way to do this and only display a white border and have the background shine through for the rest?您是否知道任何可能的方法来做到这一点,并且只显示白色边框并让 rest 的背景发光?

There is an experimental -webkit-text-stroke CSS property, that does what you need (see this answer ):有一个实验性的-webkit-text-stroke CSS 属性,可以满足您的需要(请参阅此答案):

 #age-container { background-image: url("https://images.pexels.com/photos/68568/primula-catchfly-blossom-bloom-pink-68568.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); background-repeat: no-repeat; background-position: center; background-size: cover; width: 400px; height: 400px; display: flex; } #age { margin: auto; -webkit-text-stroke: 10px white; font-family: sans-serif; color: transparent; font-size: 250px; }
 <div id="age-container"> <h1 id="age">27</h1> </div>

Another option would be to use a font that already looks the way you need it to (like these ).另一种选择是使用已经看起来像你需要的字体(比如这些)。

The issue in 2020 with: 2020年的问题是:

  • text-stroke
  • text-stroke-width
  • text-stroke-color
  • text-fill-color

is that - despite having been around for over half a decade - these are still experimental properties and have never yet been added to any offical spec (either W3C or WHAT-WG ).就是——尽管已经存在了五年多——这些仍然是实验性的属性,还没有被添加到任何官方规范( W3CWHAT-WG )中。

That's why, even though text-stroke now has wide support from browsers, all browsers (including Firefox) only support text-stroke with the -webkit prefix:这就是为什么,尽管text-stroke现在已经得到了浏览器的广泛支持,但所有浏览器(包括 Firefox)支持带有-webkit前缀的text-stroke

-webkit-text-stroke

See: https://caniuse.com/#feat=text-stroke见: https://caniuse.com/#feat=text-stroke


Using the -webkit- browser vendor prefix may not be an issue for you.使用-webkit-浏览器供应商前缀对您来说可能不是问题。

But if it is, there is another way to visually display a text-stroke-outlined transparent number within:但如果是,还有另一种方法可以在其中直观地显示文本笔划轮廓的透明数字:

<h1 id="age">27</h1>

and that's to apply an SVG image background to the <h1> element.这就是将SVG 图像背景应用于<h1>元素。

Working Example:工作示例:

 body { display: flex; justify-content: center; align-items: flex-start; } #age { position: relative; font-family: sans-serif; color: rgba(0, 0, 0, 0); background-image: url("https://images.pexels.com/photos/1749/fire-orange-emergency-burning.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"); } #age[data-age="27"]::after { content: ''; position: absolute; top: 0; left: 0; background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 400 400'><text x='204' y='320' text-anchor='middle' stroke='rgb(255, 255, 255)' stroke-width='6' fill='rgba(0, 0, 0, 0)' style='font: 320px sans-serif; font-weight: 700;'>27</text></svg>"); } #age, #age[data-age="27"]::after { width: 400px; height: 400px; background-repeat: no-repeat; background-position: center; }
 <h1 id="age" data-age="27">27</h1>

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

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