How to bold text and add borders with CSS (skill sharing)

青灯夜游
Release: 2021-11-02 09:03:01
forward
2735 people have browsed it

How to bold text twice and add borders in css? The following article will introduce to you how to use CSS to make text bold and add borders. I hope it will be helpful to you!

How to bold text and add borders with CSS (skill sharing)

This article will explain how to achieve the

  • text bold and border effect in extreme scenarios through a practical business requirement

  • The effect of multiple borders on text

Requirement background - secondary bolding of text

I encountered such an interesting problem today :

  • When displaying text, I usedfont-weight: boldto bold the text, but I think it is still not bold enough. Is there any way to make the text bold? What about thicker?

emm, if compatibility is not considered, the answer is that you can use the-webkit-text-strokeattribute of the text to make the text bold twice. (Learning video sharing:css video tutorial)

MDN - webkit-text-stroke: This attribute adds a border (stroke) to the text character and specifies thewidth of the borderandcolor, which is the abbreviation of the-webkit-text-stroke-widthand-webkit-text-stroke-colorproperties.

Look at the DEMO below, we can use-webkit-text-stroketo bold the text twice:

文字加粗CSS

文字加粗CSS

文字加粗CSS

文字加粗CSS

Copy after login
p { font-size: 48px; letter-spacing: 6px; } p:nth-child(2) { font-weight: bold; } p:nth-child(3) { -webkit-text-stroke: 3px red; } p:nth-child(4) { -webkit-text-stroke: 3px #000; }
Copy after login

Compare the following 4 types of text, the last one This usesfont-weight: boldand-webkit-text-stroketo make the textbold.

How to bold text and add borders with CSS (skill sharing)

CodePen Demo -- font-weight: bold and -webkit-text-stroke secondary bold text

https://codepen .io/Chokcoco/pen/gOxwEvo

How to add a border to twice bold text?

OK, after completing the first step above, the matter is not over yet, and a more terrifying problem has come.

Now the text needs to be bolded twice and a border of a different color is added.

We used the-webkit-text-strokeattribute that might have been used to add a border to the text, and now things get a little tricky. This question can also be transformed into, how to add 2 layers of borders of different colors to the text?

Of course, this is not a problem with the powerful CSS (SVG), let’s try it.

Try method one: Use pseudo-elements of text to enlarge text

The first method to try is a bit troublesome. We can refine each text, use the pseudo elements of the text to slightly enlarge the text, and fit the original text and the retrieved text together.

  • Split the text into independent elements for processing

  • Use theattr()feature of pseudo-element, use The pseudo-element of the element implements the same text

  • enlarges the pseudo-element’s text

  • and superimposes it under the original text

Code:

  • C
  • S
  • S
Copy after login
ul { display: flex; flex-wrap: nowrap; } li { position: relative; font-size: 64px; letter-spacing: 6px; font-weight: bold; -webkit-text-stroke: 3px #000; &::before { content: attr(data-text); position: absolute; top: 0; left: 0; bottom: 0; right: 0; color: red; -webkit-text-stroke: 3px #f00; z-index: -1; transform: scale(1.15); } }
Copy after login

You can simply add an animation to the above effect, and you will understand it at a glance:

How to bold text and add borders with CSS (skill sharing)

##CodePen Demo -- Use pseudo elements to add borders to bold text

https://codepen.io/Chokcoco/pen/ExvgLNm

It looks good, but in fact, if you look closely, the border effect is very Rough, every part of the text is not covered regularly, and the effect is not acceptable:

How to bold text and add borders with CSS (skill sharing)

Try method two: Use text-shadow to simulate the border

One method failed, so we continue to try the second method, using

text-shadowto simulate the border.

We can add a text shadow to the twice bold text:

文字加粗CSS

Copy after login
p { font-size: 48px; letter-spacing: 6px; font-weight: bold; -webkit-text-stroke: 1px #000; text-shadow: 0 0 2px red; }
Copy after login

Look at the effect:

How to bold text and add borders with CSS (skill sharing)

Okay, this It's too far away from the border, it's just a shadow.

But don’t worry,

text-shadowsupports multiple shadows. Let’s overlay the abovetext-shadowseveral times:

p { font-size: 48px; letter-spacing: 6px; font-weight: bold; -webkit-text-stroke: 1px #000; - text-shadow: 0 0 2px red; + text-shadow: 0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red,0 0 2px red; }
Copy after login

How to bold text and add borders with CSS (skill sharing)

Wow, if you don’t look carefully, using this method of superimposing multiple layers of

text-shadow, it really looks like a border!

Of course, if we zoom in, the flaws will be more obvious, and we can still see that they are shadows:

How to bold text and add borders with CSS (skill sharing)

CodePen Demo -- Utilization text-shadow adds a border to text

https://codepen.io/Chokcoco/pen/porEVeg

尝试方法四:利用多重 drop-shadow()

在尝试了text-shadow之后,自然而然的就会想到多重filter: drop-shadow(),主观上认为会和多重text-shadow的效果应该是一致的。

不过,实践出真知。

在实际测试中,发现利用filter: drop-shadow()的效果比多重text-shadow要好,模糊感会弱一些:

p { font-weight: bold; -webkit-text-stroke: 1px #000; filter: drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red) drop-shadow(0 0 0.25px red); }
Copy after login

效果如下:

How to bold text and add borders with CSS (skill sharing)

我们甚至可以利用它制作文字二次加粗后的多重边框:

p { font-weight: bold; -webkit-text-stroke: 1px #000; filter: drop-shadow(0 0 0.2px red) // 重复 N 次 drop-shadow(0 0 0.2px red) drop-shadow(0 0 0.25px blue) // 重复 N 次 drop-shadow(0 0 0.25px blue); }
Copy after login

效果如下:

How to bold text and add borders with CSS (skill sharing)

然而,在不同屏幕下(高清屏和普通屏),drop-shadow()的表现效果差别非常之大,实则也难堪重用。

我们没有办法了吗?不,还有终极杀手锏 SVG。

尝试方法四:利用 SVG feMorphology 滤镜给文字添加边框

其实利用 SVG 的 feMorphology 滤镜,可以非常完美的实现这个需求。

这个技巧,我在有意思!不规则边框的生成方案这篇文章中也有提及。

借用 feMorphology 的扩张能力给不规则图形添加边框

直接上代码:

文字加粗CSS

Copy after login
p { font-size: 64px; letter-spacing: 6px; font-weight: bold; -webkit-text-stroke: 2px #000; filter: url(#dilate); }
Copy after login

效果如下:

How to bold text and add borders with CSS (skill sharing)

我们可以通过 SVG feMorphology 滤镜中的radius控制边框大小,feFlood 滤镜中的flood-color控制边框颜色。并且,这里的 SVG 代码可以任意放置,只需要在 CSS 中利用 filter 引入即可。

本文不对 SVG 滤镜做过多的讲解,对 SVG 滤镜原理感兴趣的,可以翻看我上述提到的文章。

至此,我们就完美的实现了在已经利用font-weight: bold-webkit-text-stroke的基础上,再给文字添加不一样颜色的边框的需求。

放大了看,这种方式生成的边框,是真边框,不带任何的模糊:

How to bold text and add borders with CSS (skill sharing)

CodePen Demo -- 利用 SVG feMorphology 滤镜给文字添加边框

https://codepen.io/Chokcoco/pen/GRvjdMz

最后

OK,本文到此结束,介绍了一些 CSS 中的奇技淫巧去实现文字二次加粗后加边框的需求,实际需求中,如果不是要求任意字都要有这个效果,其实我更推荐切图大法,高保真,不丢失细节。

当然,可能还有更便捷更有意思的解法,欢迎在评论区不吝赐教。

希望本文对你有所帮助 :)

原文地址:https://juejin.cn/post/7023940690476269605

作者:chokcoco

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of How to bold text and add borders with CSS (skill sharing). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:掘金-chokcoco
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!