Ignore special fonts for the time being, and you can find out through the layer style of the design draft that there are 3 special text effects, namely Gradients, strokes, drop shadows
As a pursuit front end, of course it will not be used directly Pictures~ Here are two ways to implement CSS and SVG respectively, let’s take a look together
Reminder: There are many details in the article, if you are not interested, you can directly skip to the bottom to view the online demo
1. CSS text gradient
First look at CSS accomplish.
There is no direct property in CSS to set the text gradient, usually the text can only be a solid color. However, the background color can be displayed in the text area by clipping the background background-clip
, which looks like the text has a gradient
<p class='text' > Customized for you to discover wonderful </p>
.text{
background-image: linear-gradient span>(#FFCF02, #FF7352);
background-clip: text;
-webkit-background-clip: text;
}< br>
But this has no effect, the text is still the default color
The reason is actually very simple, because it is a cropped background, the last display is actually the background color, and the colored text is covered It is above the background, so here you need to set the text color to 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;< br> -webkit-text-fill-color: transparent; /*requires text to be transparent*/
}
< p data-tool='mdnice editor' > In this way, you can see the text gradient effect2. SVG text gradient
Take another look at text gradients in SVG.
SVG naturally supports text gradients, and text can be regarded as ordinary vector paths. The structure is as follows
<svg>
<< span >text> Customized for you to discover wonderful </text>
</ svg>
Just fill it directly with fill
, but you need Note that the filling here is a little troublesome. Gradients cannot be like CSS, and you must use a special gradient tag <linearGradient>
. If you are interested, you can check linearGradient - SVG | MDN (mozilla.org )[1], need to be defined in <defs></defs>
<svg>
<< span >defs>
<linearGradient id='gradient'>
<stop offset='0%' stop-color ='#FFCF02'/>
<stop offset='100% ' stop-color='#FF7352'/>
</linearGradient>
</defs>
<text class ='text'> Customized for you Discover wonderful </text>
< span ></svg>
<linearGradient>< The
<stop>
tag in /code> is used to define the color gradient of the gradient, offset
and stop-color
respectively define the gradient node and Color, and then fill the gradient with the fill
attribute (specify the id)
.text{
fill: url span>(#gradient);
}
The effect is as follows (it is not a problem with image loading)
There are two problems in this way
The text is not centered horizontally and vertically The gradient direction is horizontal to the right
First look at the first question. The adaptive processing of text in SVG is still very weak. For example, in CSS, automatic line wrapping is common. In SVG, you can only manually wrap lines at specified positions. Two attributes text-anchor
and dominant-baseline
are needed for centering here, which are text anchor alignment and text baseline alignment respectively, which are horizontal and vertical in simple terms. alignment
.text{
text-anchor: middle;
dominant-baseline: middle;
fill: url(#gradient);
}
At the same time <text>
also need to set x
, y
position, The percentage here can be compared to the background position percentage in CSS
<text class='text' x='50%' y='50%'> Customized for you Discover wonderful </text>
This way centered
Regarding the gradient direction, SVG uses x1
, 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 expressed
For example, if you need a vertical downward direction, you can set it in <linearGradient>
x1='0' y1='0' x2='0' y2='1'
, as follows
<svg>
<< span >defs>
<linearGradient id='gradient' x1='0' y1='0' x2='0 ' y2='1'>
<stop offset ='0%' stop-color='#FFCF02'/>
<stop offset='100%' stop-color='#FF7352'< /span>/>
</linearGradient>
</defs >
<text class='text'> Customized for you Discover wonderful</text>
</svg>
code>
The effect is as follows
3. CSS text stroke
There is an attribute -webkit-text-stroke
specially used for text stroke in CSS, which can control the width and color of the stroke, such as
.text{
-webkit-text-stroke: < span >2px #333;
}
The effect is as follows
There is indeed a stroke, but the text seems to be thinner Circle, if you think it is not obvious, you can set it a little bigger
As can be seen from here, -webkit- text-stroke
is actually a centered stroke, and it is covered on the text, and the stroke method cannot be changed. In fact, many design tools can choose the stroke method, such as figma
So, how to achieve the outer stroke effect?
is also possible! Use two layers of text, one layer of text stroke, and one layer of text gradient. In order to save labels, you can use pseudo-elements to generate
<p class='text' data-title='Customized for you to find wonderful'>Customized for you to discover wonderful</p >
::before
set the gradient, at the top, the original text This setting stroke is located at the bottom, pay attention to remove the -webkit-text-stroke
of ::before
.text::before{
content< /span>: attr(data-title);
position: absolute;
background-image: linear -gradient(#FFCF02, #FF7352);
background-clip: text;
-webkit-background-clip: text;< br> -webkit-text-fill-color: transparent;
-webkit-text-stroke: 0;
}
.text{
-webkit-text-stroke: 6px #333;
}
The overlay is as follows
Change different strokes There will be no text "thinning"
Fourth, SVG text description Edge
SVG can also achieve stroke effect, which is similar to CSS. It should be said that CSS is based on SVG, through stroke
and stroke-width
to control the stroke color and size, such as
.text{
/*other*/
stroke-width: 4px;
stroke: #333;
}
< /code>
You can get this effect
The performance is the same as CSS, both are centered strokes, and also Can not change.
The difference is that SVG control is more flexible. The default is to fill first, then stroke, so it looks like the stroke is on top of the fill , However, we can change this rule, set the stroke first, then fill, then the filled color will cover the stroke. To change this rule in SVG, you can set it through paint-order[2]. About this property, you can visit this article by Zhang Xinxu: CSS paint-order wishes everyone a happy New Year[3]
.text{
/*other*/
stroke-width: 4px;
stroke: #333;
paint- order: stroke; /*stroke first*/
}
like this It achieves the outer stroke effect, is it much more convenient than CSS?
In addition, SVG can also set the stroke path The shape of the corner, for example, the setting of the corner in figma is as follows
The corresponding attribute in SVG is called stroke-linejoin[4 ], here is the rounded corner, you can do the following settings
.text{
/*other*/
stroke-width: 4px;
stroke: #333;
paint- order: stroke; /*stroke first*/
stroke-linejoin: round; /*path corners are rounded*/
}
The effects of various attributes are as follows
5. CSS text projection
Continue adding effects. CSS can add text shadow through text-shadow
.text{
-webkit-text-stroke: < span >6px #333;
text-shadow: 0 4px 0 #333;
}
The result becomes like this
The reason is actually related to the text gradient, which is actually the background The text is transparent, so adding a shadow to the text will result in the shadow covering the background. In addition to using text-shadow
, you can also use the drop-shadow
filter to achieve
.text{
-webkit-text-stroke: < span >6px #333;
filter: drop-shadow(0 4px 0 #333);
}
This is perfect
6. SVG text projection
SVG is more flexible. For example, the drop-shadow
filter used above actually borrows from the [ 5] filter, so SVG can also be implemented like this
<svg>
<< span >defs>
<linearGradient id='gradient' x1='0' y1='0' x2='0 ' y2='1'>
<stop offset ='0%' stop-color='#FFCF02'/>
<stop offset='100%' stop-color='#FF7352'< /span>/>
</linearGradient>
<filter < span >id='shadow'>
<feDropShadow dx=< span >'0' dy='4' stdDeviation='0' flood-color='#333'/>
</filter>< br> </defs>
<text x= '50%' y='50%' class='text'>< /span> Customized for you to find wonderful </text>
</svg>
here dx
, dy
, stdDeviation
, flood-color
and drop-shadow(dx,dy,stdDeviation,flood-color)
are one-to-one correspondence, so I won’t explain more, and then Apply filters to text
.text{
/*other*/
filter:url(#shadow);
}
like this Can also implement text projection
In fact, there is no need to be so troublesome in SVG. The reason why the above text-shadow
cannot It is used because the text gradient implemented by CSS is the background, which is a fake text gradient, but in SVG it is a real gradient filling, so yes, you can directly use text-shadow
in CSS here To achieve, SVG and CSS now have many attributes and styles that are interoperable, as follows
.text{
/*other*/
fill: url(#gradient);
text-shadow: 0 4px span> 0 #333;
}
more concise
Seven, special font processing
Usually, some special fonts will be used for the title of the activity. It may reach tens of MB or hundreds of MB. In fact, we only need to use the fonts that appear. If the special fonts that appear in the text are extracted separately, the entire font file will be greatly reduced. This process is called font subsetting.
So what to do?
Here is a recommended tool Fontmin - font subsetting scheme[6], about the principle of font subsetting, you can Refer to this article: Performance Optimization Magician: Chinese Font Practice - Nuggets[7]
After downloading the client, import the font file . ttf
, and then enter the required text, as follows
Click Generate to get the following files
The first one starts with -embed Code> is the CSS suffix, which contains the converted base64 file, which can be imported directly
@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+gAAAAIGAAQBAQEBA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truetype' span>), /* 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{
/*other styles*/
font-family: 'HYLiLiangHeiJ Regular';CSS implementation can access text-css (codepen .io)[8]
SVG implementation can access text-svg (codepen.io)[9]
VIII. Summary and explanation
The above introduces two different CSS and SVG From the effect point of view, it is obvious that SVG is better, such as smoother strokes and no need for multi-layer nesting, but CSS also has advantages, such as gradients and shadows are simpler, to sum up
The essence of CSS text gradient is background clipping, you need to set the text color to transparent SVG text naturally supports gradient filling, you need to use the linearGradient tag SVG text centering is a little troublesome, you need to use text-anchor and dominant-baseline CSS and SVG strokes are both centered strokes and cannot be changed CSS outer strokes can be achieved by superimposing multi-layer structures SVG can draw the fill on top of the stroke through paint-order CSS text shadows will be visible when the text is transparent Through it, you can use drop-shadow to simulate projection feDropShadow in SVG is similar to drop-shadow in CSS SVG can directly use text-shadow in CSS to realize text projection fontmin
CSS and SVG have their own advantages and influence each other. Many CSS styles can also be used in SVG, and many SVG attributes have also been introduced into CSS. In normal development, the advantages of the two can be fully combined. Finally, if you feel good and helpful to you, please like, bookmark, forward ❤❤❤
References
linearGradient - SVG | MDN (mozilla.org): https: //developer.mozilla.org/zh-CN/docs/Web/SVG/Element/linearGradient
[2]paint-order: https://developer.mozilla.org/en-US/docs/ Web/CSS/paint-order
[3]CSS paint-order wishes everyone a Happy New Year: https://www.zhangxinxu.com/wordpress/2020/01/css- paint-order/
[4]stroke-linejoin: https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/stroke -linejoin
[5]Fontmin - font subsetting scheme: http://ecomfe.github.io/fontmin/
[7]Performance Optimization Magician: Chinese Font Practice - Nuggets: https://juejin.cn/post/6844904073905307656
[8]text-css (codepen.io): https://codepen.io/xboxyan/pen/PojRjPg
[9]text-svg (codepen.io): https://codepen.io/xboxyan/pen/ZEyxyQL
iCSS is not limited to CSS. If you are also interested in various novel and interesting front-end (CSS) knowledge, welcome to pay attention. At the same time, if you have any questions, or want to join the group to participate in the discussion of large front-end technology, watch and answer questions, grow and progress together, you canscan the code to join the group:
Because the WeChat official account has revised the rules, if you do not star or click to read, you may not receive the push of my official account article, please share thisto the public , after reading the article, rememberlikeor< span >Looking, thank you!
Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/Simple%20to%20achieve%20highend%20effect%20%20CSS%20and%20SVG%20to%20achieve%20text%20gradient%20stroke%20drop%20shadow.html
评论列表(196条)
测试