简体   繁体   English

CSS 文字阴影覆盖背景线性渐变

[英]CSS Text Shadow Overrides the Background Linear Gradient

I am working on a web app which will allow the user to generate a logo, and have discovered an issue in certain scenarios.我正在开发一个 web 应用程序,该应用程序将允许用户生成徽标,并在某些情况下发现了问题。 When the text color is actually a gradient, I am using a technique to render this gradient where the color is set to transparent and the background is set to the gradient, as the color cannot accept the gradient value directly.当文本颜色实际上是渐变时,我使用一种技术来渲染这种渐变,其中颜色设置为透明,背景设置为渐变,因为颜色不能直接接受渐变值。

However, when I also use the text-shadow property, such as to create a 3D shadow effect, the shadow colors block / override the background color.但是,当我也使用 text-shadow 属性,比如创建 3D 阴影效果时,阴影 colors 块/覆盖背景颜色。 The CSS below is taken from an example where this occurs.下面的 CSS 取自发生这种情况的示例。 The text shadow has different shades of gray, and the background itself has that linear gradient of a blue to orange color, but the end result does not appear to have those colors.文本阴影有不同的灰色阴影,背景本身具有从蓝色到橙色的线性渐变,但最终结果似乎没有那些 colors。 Is there a different combination of CSS properties, or perhaps a way to provide the gradient to the color property instead of background, that will prevent this issue?是否有 CSS 属性的不同组合,或者可能是一种为颜色属性而不是背景提供渐变的方法,可以防止这个问题?

#logo {
    color: transparent;
    background: linear-gradient(#3294cd, #a85c34);
    background-clip: text;
    -webkit-background-clip: text;
    font-style: italic;
    font-weight: bold;
    font-family: Aladin;
    filter:  brightness(100%) contrast(140%) saturate(140%);
    text-shadow: 0px 0px 0 #bbb, -1.4285714285714284px 0.5714285714285714px 0 #999, -2.8571428571428568px 1.1428571428571428px 0 #888, -4.285714285714286px 1.7142857142857142px 0 #777, -5.7142857142857135px 2.2857142857142856px 0 #666, -7.142857142857143px 2.857142857142857px 0 #555, -8.571428571428571px 3.4285714285714284px 0 #444, -10px 4px 0 #333;
}

The output of the CSS above is shown below:上图CSS的output如下图所示:

上面 CSS 代码的输出

The way text-shadow seems to work is by creating a 'copy' of the text in the relevant color and the relevant distance/position from the actual text.文本阴影的工作方式似乎是通过以相关颜色和与实际文本的相关距离/位置创建文本的“副本”。 So if your text is basically transparent you just see the shadow.因此,如果您的文本基本上是透明的,您只会看到阴影。

The 'trick' which allows you to get a background cutout by the text doesn't stop the background still being, well, a background ie it is 'beneath' everything else, including the shadow.允许您通过文本获得背景剪切的“技巧”并不能阻止背景仍然是背景,即它“低于”其他所有内容,包括阴影。

The only workaround I can think of is to have two copies of the text.我能想到的唯一解决方法是拥有两份文本。 Put the first into an absolutely positioned element behind the second.将第一个放入第二个后面的绝对定位元素中。 The first will have the shadow, the second will have the 'cutout' background.第一个将有阴影,第二个将有“抠图”背景。

 #dummyLogo { position: absolute; color: transparent; font-style: italic; font-weight: bold; font-family: Aladin; filter: brightness(100%) contrast(140%) saturate(140%); text-shadow: 0px 0px 0 #bbb, -1.4285714285714284px 0.5714285714285714px 0 #999, -2.8571428571428568px 1.1428571428571428px 0 #888, -4.285714285714286px 1.7142857142857142px 0 #777, -5.7142857142857135px 2.2857142857142856px 0 #666, -7.142857142857143px 2.857142857142857px 0 #555, -8.571428571428571px 3.4285714285714284px 0 #444, -10px 4px 0 #333; font-size: 72px; z-index: -1; } #logo { color: transparent; background: linear-gradient(#3294cd, #a85c34); background-clip: text; -webkit-background-clip: text; font-style: italic; font-weight: bold; font-family: Aladin; filter: brightness(100%) contrast(140%) saturate(140%); font-size: 72px; }
 <div id="dummyLogo">HELLO</div> <div id="logo">HELLO</div>

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

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