使用 CSS 实现内部文本阴影
我目前正在使用 CSS3 并尝试实现这样的文本效果(黑色模糊的内部阴影):
但我无法找到在文本内创建文本阴影的方法。我想知道这是否仍然可能,因为 box-shadow 元素能够像这样在内部渲染阴影:
box-shadow: inset 0px -5px 10px 0px rgba(0, 0, 0, 0.5);
有什么想法吗?
I am currently playing around with CSS3 and trying to achieve a text effect like this (the black blurry inner shadow):
But I cannot find a way to create text shadows inside the text. I wonder whether it is still possible because the box-shadow element is able to render shadow inside like this:
box-shadow: inset 0px -5px 10px 0px rgba(0, 0, 0, 0.5);
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
这是我使用
:before
和:after
伪元素发现的一个小技巧:title 属性需要与内容相同。演示:http://dabblet.com/gist/1609945
Here's a little trick I discovered using the
:before
and:after
pseudo-elements:The title attribute needs to be the same as the content. Demo: http://dabblet.com/gist/1609945
您应该能够使用文本阴影来做到这一点,嗯,像这样:
这是一个示例: http://jsfiddle.net/ekDNq/< /a>
You should be able to do it using the text-shadow, erm somethink like this:
here's an example: http://jsfiddle.net/ekDNq/
一个优雅的示例,不需要定位包装器或标记中的重复内容:
在深色背景上看起来也不错:
和我的链接
和 ME2 链接
An elegant example without the need for a positioned wrapper or duplicative content in the markup:
Looks good on dark backgrounds as well:
AND ME LINK
AND ME2 LINK
这是我最好的尝试:
问题是如何剪掉边缘渗色的阴影!!!我尝试在 webkit 中使用 background-clip:text,但是 webkit 渲染背景上方的阴影,所以它不起作用。
用CSS制作文本遮罩?
如果没有顶部遮罩层,就不可能在文本上制作真正的内部阴影。
也许有人应该建议 W3C 添加 background-clip:reverse-text,这将通过背景剪切蒙版,而不是剪切背景以适合文本。
或者将文本阴影渲染为背景的一部分,并使用 background-clip: text 对其进行剪辑。
我尝试在其上方绝对放置一个相同的文本元素,但问题是背景剪辑:文本裁剪背景以适合文本内部,但我们需要相反的情况。
我尝试使用文本笔触:20px白色;在此元素及其上方的元素上,但文本笔画既进又出。
替代方法
由于目前无法在 CSS 中制作反向文本蒙版,因此您可以转向 SVG 或 Canvas,并使用三层制作文本替换图像来获得效果。
由于 SVG 是 XML 的子集,因此 SVG 文本仍然是可选择和可搜索的,并且可以使用比 Canvas 更少的代码来产生效果。
使用 Canvas 来实现这一点会更困难,因为它没有像 SVG 那样带有图层的 dom。
您可以在服务器端生成 SVG,也可以在浏览器中将其作为 JavaScript 文本替换方法生成。
进一步阅读:
SVG 与 Canvas:
http://dev.opera.com/articles/view/svg-or-canvas-choosing- Between-the-two/
使用 SVG 文本进行剪辑和遮罩:
http://www.w3.org/TR/SVG/text.html#TextElement
Here's my best try:
The problem is how to clip the shadow that bleeds around the edges!!! I tried in webkit using background-clip:text, but webkit renders the shadow above the background so it doesn't work.
Making a Text Mask with CSS?
Without a top mask layer it is impossible to do a true inner shadow on text.
Perhaps someone should recommend that the W3C add background-clip: reverse-text, that would cut a mask through the background instead of cutting the background to fit inside the text.
Either that or render the text-shadow as part of the background and clip it with background-clip: text.
I tried absolutely positioning an identical text element above it, but the problem is background-clip: text crops the background to fit inside the text, but we need the reverse of that.
I tried using text-stroke: 20px white; on both this element and the one above it, but the text stroke goes in as well as out.
Alternate Methods
Since there is currently no way to make an inverted-text mask in CSS, you could turn to SVG or Canvas and make a text replacement image with the three layers to get your effect.
Since SVG is a subset of XML, SVG text would still be select-able and searchable, and the effect can be produced with less code than Canvas.
It would be harder to achieve this with Canvas because it doesn't have a dom with layers like SVG does.
You could produce the SVG either server-side, or as a javascript text-replacement method in the browser.
Further Reading:
SVG versus Canvas:
http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/
Clipping and Masking with SVG Text:
http://www.w3.org/TR/SVG/text.html#TextElement
在kendo451的答案中对CSS有更精确的解释。
还有另一种获得奇特黑客的方法内阴影错觉,
我将通过三个简单的步骤进行解释。假设我们有这个 HTML:
和这个 CSS:
第 1 步
让我们从使文本透明开始:
步骤 2
现在,我们裁剪背景为文本的形状:
步骤 3
现在,神奇的是:我们将放置一个模糊的
text-shadow
,这将在前面背景,从而给人一种内阴影的印象!
请参阅最终结果。
缺点?
background-clip
不能是text
)。More precise explanation of the CSS in kendo451's answer.
There's another way to get a fancy-hacky inner shadow illusion,
which I'll explain in three simple steps. Say we have this HTML:
and this CSS:
Step 1
Let's start by making the text transparent:
Step 2
Now, we crop that background to the shape of the text:
Step 3
Now, the magic: we'll put a blurred
text-shadow
, which will be in frontof the background, thus giving the impression of an inner shadow!
See the final result.
Downsides?
background-clip
can't betext
).不需要多个阴影或任何类似的东西,您只需在负 y 轴上偏移阴影即可。
对于浅色背景上的深色文本:
如果您有深色背景,则只需反转颜色和 y 位置:
调整 rgba 值、不透明度和模糊以获得恰到好处的效果。这在很大程度上取决于您所拥有的颜色字体和背景,字体越重越好。
There's no need for multiple shadows or anything fancy like that, you just have to offset your shadow in the negative y-axis.
For dark text on a light background:
If you have a dark background then you can simply invert the color and y-position:
Play around with the rgba values, the opacity, and the blur to get the effect just right. It will depend a lot on what color font and background you have, and the weightier the font, the better.
你可以这样做。不幸的是,无法在文本阴影上使用插图,但您可以使用颜色和位置来伪造它。将模糊效果向右下方并沿右上角排列阴影。像这样的东西可能会起作用:
...但是你需要非常非常小心你使用的颜色,否则它会看起来很奇怪。它本质上是一种视错觉,因此并不适用于所有情况。在较小的字体大小下,它看起来也不太好,所以也要注意这一点。
You can kind of do this. Unfortunately there's no way to use an inset on text-shadow, but you can fake it with colour and position. Take the blur right down and arrange the shadow along the top right. Something like this might do the trick:
... but you'll need to be really, really careful about which colours you use otherwise it will look off. It is essentially an optical illusion so won't work in every context. It also doesn't really look great at smaller font sizes, so be aware of that too.
尝试这个小小的变体:
我通常将“没有答案”视为挑战
Try this little gem of a variation:
I usually take "there's no answer" as a challenge
我已经看到了许多建议的解决方案,但没有一个完全符合我的期望。
这是我对此的最佳破解,其中颜色是透明的,背景和顶部文本 -阴影是具有不同不透明度的相同颜色,模拟蒙版,第二个文本阴影是您实际想要的颜色的更暗、更饱和的版本(使用 HSLA 很容易做到)。
(顺便说一句,文本和样式基于 欺骗线程的OP)
I've seen many of the proposed solutions, but none were quite what I was hoping.
Here's my best hack at this, where the color is transparent, the background and the top text-shadow are the same color with varying opacities, simulating the mask, and the second text-shadow is a darker, more saturated version of the color you actually want (pretty easy to do with HSLA).
(btw, text and styling based upon a dupe thread's OP)
我遇到过一些需要在文本上添加内部阴影的情况,以下方法对我来说效果很好:
这将文本的不透明度设置为 80%,然后创建两个阴影:
注意事项
I've had a few instances where I've needed inner shadows on text, and the following has worked out well for me:
This sets the opacity of the text to 80%, and then creates two shadows:
Caveats
看来这个问题大家心里都有答案了。我喜欢@Web_Designer 的解决方案。但它不需要那么复杂,你仍然可以获得你正在寻找的模糊的内部阴影。
http://dabblet.com/gist/3877605
Seems everyone's got an answer to this one. I like the solution from @Web_Designer. But it doesn't need to be as complex as that and you can still get the blurry inner shadow you're looking for.
http://dabblet.com/gist/3877605
结果:
Result:
基于 web_designer 的
:before
:after
技术,这里有一些更接近您的外观的东西:首先,将文本设置为内部阴影的颜色(黑色)在OP的情况下)。
现在,使用 :after psuedo 类创建原始文本的透明副本,直接放置在其顶部。为其指定一个没有偏移的常规文本阴影。将原始文本颜色指定给阴影,并根据需要调整 Alpha。
http://dabblet.com/gist/2499892
您无法完全控制传播等。像在 PS 中那样调整阴影,但对于较小的模糊值,这是相当可以接受的。阴影超出了文本的边界,因此如果您在具有高对比度背景-前景的环境中工作,阴影将会很明显。对于对比度较低的项目,尤其是具有相同色调的项目,它不太明显。例如,我已经能够使用这种技术在金属背景中制作非常漂亮的蚀刻文本。
Building on the
:before
:after
technique by web_designer, here is something that comes closer to your look:First, make your text the color of the inner shadow (black-ish in the case of the OP).
Now, use an :after psuedo class to create a transparent duplicate of the original text, placed directly on top of it. Assign a regular text shadow to it with no offset. Assign the original text color to the shadow, and adjust alpha as needed.
http://dabblet.com/gist/2499892
You don't get complete control over spread, etc. of the shadow like you do in PS, but for smaller blur values it is quite passable. The shadow goes past the bounds of the text, so if you are working in an environment with a high contrast background-foreground, it will be obvious. For lower contrast items, especially ones with the same hue, it's not too noticeable. For example, I've been able to make very nice looking etched text in metal backgrounds using this technique.
这是使用 CSS3 属性 background-clip 实现 TRUE 插入文本阴影的一个很好的解决方案:
Here's a great solution for TRUE inset text shadow using the background-clip CSS3 property:
这是我在查看了这里的一些想法后想到的。主要思想是文本的颜色与两个阴影混合,请注意,这是在灰色背景上使用的(否则白色将无法很好地显示)。
Here's what I came up with after looking at some of the ideas here. The main idea is that the color of the text blends with both shadows, and note that this is being used on a grey background (otherwise the white won't show up well).
有一种更简单的方法可以实现这一目标
Voilà
There's a much simpler way to achieve this
Voilà
对于小按钮上的正常大小的文本,
我发现本页上的其他想法在用于具有小文本字体的小按钮时会失去效力。此答案扩展了RobertPitt 的答案。
这是一个小调整:
注意:
我使用此网站来巧妙处理色调。
For normal-sized text on small-ish buttons
I found the other ideas on this page to lose their efficacy when used on small buttons with small text lettering. This answer expands on RobertPitt's answer.
Here is the minor tweak:
Note:
I used this site to finesse the color shades.
对于框阴影:
可以看到在线文本和框阴影:
在线文本和框阴影
有关更多示例,您可以访问以下地址:
更多 freeclup 示例代码
for box shadow:
you can see online text and box shadow:
online text and box shadow
for more example you can go to this address :
more example code freeclup
尝试使用此示例来插入文本阴影。这是 HTML
和 CSS
Jsfiddle 上的演示
Try this example for inset text shadow. Here's the HTML
and the CSS
Demo on Jsfiddle