Home  >  Article  >  Web Front-end  >  Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

青灯夜游
青灯夜游forward
2021-09-27 11:32:246988browse

This article will introduce to you how to use CSS and SVG to add gradient, stroke, and shadow effects to text. I hope it will be helpful to you!

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

In some web activity pages, you can often see specially processed title text, such as this

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

for now Ignoring the special fonts, you can find through the layer style of the design draft that there are three text effects, namely gradient, stroke, projection

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

As a ambitious front-end, of course I will not use pictures directly~ Here we use CSS and SVG to achieve it, let’s take a look together

Warm reminder: The article has a lot of details. If you are not interested, you can jump directly to the bottom to view the online demo

1. CSS text gradient

First let’s look at the implementation in CSS.

There is no direct property in CSS to set the text gradient. Usually the text can only be a solid color. However, you can use background clippingbackground-clip to make the background color appear in the text area. It looks like the text has a gradient

<p class="text">为你定制 发现精彩</p>
.text{
  background-image: linear-gradient(#FFCF02, #FF7352);
  background-clip: text;
  -webkit-background-clip: text;
}

But this has no effect and the text is still the default color

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

The reason is actually very simple. Since it is a cropped background, what is displayed at the end is actually the background color. The colored text covers the background, so the text color needs to be set here. Just make it transparent, which can be achieved with color and -webkit-text-fill-color.

.text{
  background-image: linear-gradient(#FFCF02, #FF7352);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent; /*需要文字透明*/
}

This way you can see the text gradient effect

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

2. SVG text gradient

Let’s take a look at the text gradient in SVG .

SVG naturally supports text gradients. Text can be treated as an ordinary vector path. The structure is as follows

<svg>
  <text>为你定制 发现精彩</text>
</svg>

Just fill it directly with fill, but it should be noted that Filling here is a little more troublesome. Gradient cannot be like CSS, and special gradient tags must be used <lineargradient></lineargradient>. If you are interested, you can view linearGradient - SVG | MDN (mozilla.org) , needs to be defined in the <stop></stop> tag in

Used to define the color slope of the gradient, offset and stop-color define the node and color of the gradient respectively, and then fill the gradient with the fill attribute (specify the id)<pre class="brush:js;toolbar:false;">&lt;svg&gt; &lt;defs&gt; &lt;linearGradient id=&quot;gradient&quot;&gt; &lt;stop offset=&quot;0%&quot; stop-color=&quot;#FFCF02&quot;/&gt; &lt;stop offset=&quot;100%&quot; stop-color=&quot;#FF7352&quot;/&gt; &lt;/linearGradient&gt; &lt;/defs&gt; &lt;text class=&quot;text&quot;&gt;为你定制 发现精彩&lt;/text&gt; &lt;/svg&gt;</pre>The effect is as follows (it is not a problem with image loading)

There are two problems in this way

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

The text is neither centered horizontally nor vertically

  • The gradient direction is horizontal to the right

  • Look at the first question first. The adaptive processing of text in SVG is still very weak. For example, automatic line wrapping, which is common in CSS, can only be manually wrapped at a specified position in SVG. Here you need to use two attributes

    text-anchor
  • and
dominant-baseline

, which mark text anchor alignment and text baseline alignment respectively. Simply put, they are horizontal and vertical alignment. <pre class="brush:html;toolbar:false;">.text{ fill: url(#gradient); }</pre>At the same time, <text></text> also needs to set the

x

, y position. The percentage here can be compared with the background position percentage in CSS. <pre class="brush:css;toolbar:false;">.text{ text-anchor: middle; dominant-baseline: middle; fill: url(#gradient); }</pre>This displays the

in the center. Regarding the gradient direction, SVG uses

x1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG),

y1

, x2, y2 are determined by two sets of coordinates. Given a rectangle, the upper left corner is [0, 0] and the lower right corner is [1, 1], so that any angle can be represented

For example, if you need a vertical downward direction, you can set

x1="0" y1="0" x2="0" y2 in Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)<lineargradient></lineargradient>

="1"

, as follows<pre class="brush:html;toolbar:false;">&lt;text class=&quot;text&quot; x=&quot;50%&quot; y=&quot;50%&quot;&gt;为你定制 发现精彩&lt;/text&gt;</pre>The effect is as follows<p><img src="https://img.php.cn/upload/image/916/762/922/163271296784953Two%20ways%20to%20add%20gradient,%20stroke,%20and%20drop%20shadow%20effects%20to%20text%20(CSS%20and%20SVG)" title="163271296784953Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)" alt="Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)"></p> <h2 data-id="heading-2">三、CSS 文字描边</h2> <p>CSS 中有个专门用于文字描边的属性 <code>-webkit-text-stroke,可以控制描边的宽度和颜色,比如

.text{
  -webkit-text-stroke: 2px #333;
}

效果如下

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

确实有描边了,但是文字好像瘦了一圈,如果觉得不太明显,可以再设置大一点

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

从这里可以看出,-webkit-text-stroke其实是 居中描边,并且是覆盖在文本上的,也无法更改描边方式。而事实上,很多设计工具都是可以选择描边方式的,比如 figma

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

那么,如何实现外描边效果呢?

也是可以的!用两层文本,一层文本描边,一层文本渐变就可以了,为了节省标签,可以用伪元素来生成

<p class="text" data-title="为你定制 发现精彩">为你定制 发现精彩</p>

::before设置渐变,位于上方,原文本设置描边,位于下方,注意把 ::before-webkit-text-stroke去除

.text::before{
    content: attr(data-title);
    position: absolute;
    background-image: linear-gradient(#FFCF02, #FF7352);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke: 0;
}
.text{
    -webkit-text-stroke: 6px #333;
}

叠加示意如下

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

改变不同的描边也不会出现文字“变瘦”的情况

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

四、SVG 文字描边

SVG 也可以实现描边效果,和 CSS 比较类似,应该说 CSS 是借鉴 SVG 的,通过 strokestroke-width来控制描边颜色和大小,比如

.text{
  /*其他*/
  stroke-width: 4px;
  stroke: #333;
}

可以得到这样的效果

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

和 CSS 表现一样,都是居中描边,也无法改变。

不一样的是,SVG 控制更为灵活,默认是先填充、然后再描边,所以看着是描边在填充之上,但是,我们可以改变这种规则,设置先描边,再填充,那么填充的颜色就会覆盖在描边之上了。SVG 中改变这种规则的可以通过 paint-order 来设置,关于这个属性,有兴趣的可以访问张鑫旭老师的这篇文章:CSS paint-order祝大家元旦快乐

.text{
  /*其他*/
  stroke-width: 4px;
  stroke: #333;
  paint-order: stroke; /*先描边*/
}

这样就实现了外描边效果,是不是比 CSS 方便许多?

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

除此之外,SVG 还可以设置描边路径的转角处的形状,比如 figma 中关于转角的设置如下

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

SVG 中与之相对应的属性叫做 stroke-linejoin,这里是圆角,可以做如下设置

.text{
  /*其他*/
  stroke-width: 4px;
  stroke: #333;
  paint-order: stroke; /*先描边*/
  stroke-linejoin: round; /*路径转角为圆角*/
}

各种属性效果如下

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

五、CSS 文字投影

继续添加效果。CSS 可以通过 text-shadow来添加文本投影

.text{
    -webkit-text-stroke: 6px #333;
  	text-shadow: 0 4px 0 #333;
}

结果变成了这样

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

原因其实还和文本渐变有关,渐变其实是背景色,文字是透明的,所以给文字添加阴影,结果阴影就覆盖在了背景之上。除了使用text-shadow,还可以通过 drop-shadow滤镜实现

.text{
  -webkit-text-stroke: 6px #333;
  filter: drop-shadow(0 4px 0 #333);
}

这样就完美实现了

1Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

六、SVG 文字投影

SVG 就比较灵活了,比如上面使用的 drop-shadow滤镜,其实就是借鉴了 SVG 中的 滤镜,所以 SVG 也可以这样实现

<svg>
    <defs>
        <linearGradient id="gradient" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%"  stop-color="#FFCF02"/>
          <stop offset="100%"  stop-color="#FF7352"/>
        </linearGradient>
      	<filter id="shadow">
            <feDropShadow dx="0" dy="4" stdDeviation="0" flood-color="#333"/>
        </filter>
    </defs>
    <text x="50%" y="50%" class="text">为你定制 发现精彩</text>
</svg>

这里dxdystdDeviationflood-colordrop-shadow(dx,dy,stdDeviation,flood-color)中的参数是一一对应的,就不多说明了,然后在文字中应用滤镜

.text{
  /*其他*/
  filter:url(#shadow);
}

这样也能实现文字投影

Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

其实 SVG 中大可不必这么麻烦,刚才上面 text-shadow之所以不能使用,就是因为 CSS 实现的文字渐变是背景,是假的文字渐变,但是 SVG 中是真真正正的渐变填充,所以没错,这里可以直接用 CSS 中的 text-shadow 来实现,SVG 和 CSS 现在很多属性和样式都互通了,如下

.text{
  /*其他*/
  fill: url(#gradient);
  text-shadow: 0 4px 0 #333;
}

实现更加简洁

2Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

七、特殊字体处理

通常活动标题会采用一些特殊的字体,英文字体还好,整个引入都可以,但是中文就不行了,大多数中文字体都非常大,可能达到几十MB或者几百MB。其实我们只需要用到出现的字体,如果把出现的文字这一部分的特殊字体单独提取出来,那么整个字体文件将大大减小,这个过程就叫做字体子集化。

那么该如何处理呢?

这里推荐一个工具 Fontmin - 字体子集化方案,关于字体子集化的原理,可以参考这篇文章:性能优化魔法师:中文字体实践篇 - 掘金

2Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

下载客户端后,导入字体文件.ttf,然后输入需要用到的文字,如下

2Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

点击生成,可以得到如下文件

2Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

其中第一个以-embed为后缀的 CSS,里面是转换 base64 后的文件,可以直接引入

@font-face {
    font-family: "HYLiLiangHeiJ Regular";
    src: url("HYLiLiangHeiJ Regular.eot"); /* IE9 */
    src: url("HYLiLiangHeiJ Regular.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */
    url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMr6khfgAAACsAAAAYGNtYXB/inIFAAABDAAAAYJnbHlmmahvSQAAApAAAARkaGVhZA6mvEEAAAb0AAAANmhoZWEHiwK6AAAHLAAAACRobXR4BJMAmgAAB1AAAAAUbG9jYQPgBSoAAAdkAAAAFG1heHAAEwBIAAAHeAAAACBuYW1lb/SphAAAB5gAAALhcG9zdOu6TDAAAAp8AAAAdAAEA+gBkAAFAAgAZABkAAABRwBkAGQAAAOVAGQA+gAAAAIGAAQBAQEBAaAAAr8QAAAAAAAAFgAAAABITllJAEBOOny+AyD/OAAAA5UBRwAEAAAAAAAAAcAChQAAACAAAQAAAAMAAAADAAAAHAABAAAAAAB8AAMAAQAAABwABABgAAAAFAAQAAMABE46T2BSNlPRW5pfaXOwfL7/////AABOOk9gUjZT0VuaX2lzsHy+/////7HHsKKtzawzpGugnYxXg0oAAQABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAz/14DsALzABUAGQAAEyczFzM3MwchESEnIREjAyE1MxMjNQUzEyNkFrgZCyDiIAGk/hcRASDugf7NdVzSAbG3LLwCX4yMlJT9ALMBpv2mtAGmp+z+6wAAAAAEACj/VQPJAu4ADAAWACI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AASwBFAFkASQBOACAASQBuAGYAbwByAG0AYQB0AGkAbwBuACAAVABlAGMAaABuAG8AbABvAGcAeQAgAEMAbwAuAGwASQBOAOoAUgCbAJEAzwCeANEAewCAAFIAZQBnAHUAbABhAHIASABhAG4AeQBpACAASABZAEwAaQBMAGkAYQBuAGcASABlAGkASgAgAFIAZQBnAHUAbABhAHIAIAB2ADUALgAwADAAbABJAE4A6gBSAJsAkQDPAJ4A0QB7AIAAIABSAGUAZwB1AGwAYQByAFYAZQByAHMAaQBvAG4AIAA1AC4AMAAwAEgAWQBMAGkATABpAGEAbgBnAEgAZQBpAEoAVAByAGEAZABlAG0AYQByAGsAIABvAGYAIABIAEEATgBZAEkAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACQAJAAABAgEDAQQBBQEGAQcBCAEJB3VuaTRFM0EHdW5pNEY2MAd1bmk1MjM2B3VuaTUzRDEHdW5pNUI5QQd1bmk1RjY5B3VuaTczQjAHdW5pN0NCRQ==) format("truetype"), /* chrome、firefox、opera、Safari, Android, iOS 4.2+ */
    url("HYLiLiangHeiJ Regular.svg#HYLiLiangHeiJ Regular") format("svg"); /* iOS 4.1- */
    font-style: normal;
    font-weight: normal;
}
.text{
  /*其他样式*/
  font-family: "HYLiLiangHeiJ Regular";
}

这样几乎实现了和设计稿完全一致的效果

2Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG)

其实如果从头看下来,应该也能自己实现一个,既能掌握原理,也能加深印象,完全变成自己的了。不过可能不是每个同学都有时间,或者能够静下心来研究每一个案例,所以这里还是整理了一下在线 demo,想要快速看结果的直接访问就行了,如下

CSS 实现可以访问 text-css (codepen.io)

SVG 实现可以访问 text-svg (codepen.io)

八、总结和说明

以上介绍了 CSS 和 SVG 两种不同的方式来实现文本的特殊效果,从效果来看,显然 SVG 要更胜一筹,比如描边更加平滑、也无需多层嵌套,但 CSS 也有优势,比如渐变色和投影更加简单,总结一下

  • CSS 文字渐变本质是背景裁剪,需要将文字颜色设为透明

  • SVG 文字天然支持渐变填充,需要借助 linearGradient 标签

  • SVG 文本居中稍微麻烦点,需要借助 text-anchor 和 dominant-baseline

  • CSS and SVG strokes are both centered strokes and cannot be changed

  • ##CSS outer strokes can be achieved through multi-layer structure overlay

  • SVG can use paint-order to make the fill draw on top of the stroke

  • CSS text shadow will penetrate when the text is transparent, you can use drop- shadow simulates projection

  • feDropShadow in SVG and drop-shadow in CSS are similar

  • SVG can be implemented directly with text-shadow in CSS Text projection

  • Font subsetting fontmin

CSS and SVG have their own advantages and influence each other. Many CSS styles can also be used in SVG , many SVG attributes have also begun to be introduced into CSS. In daily development, the advantages of both can be fully combined.

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of Two ways to add gradient, stroke, and drop shadow effects to text (CSS and SVG). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete