想要使用CSS给文字添加描边效果,主要有两种方法:使用text-stroke属性或text-shadow属性。
方法1:使用text-shadow属性
text-shadow属性用于向文本添加字体边框或阴影。
text-shadow是实现Web描述效果中最为常见的一种方案,简单的原理就是在文本的上、下、左、右添加文本阴影(不带模糊的阴影)。
语法:
text-shadow: h-shadow v-shadow blur-radius color|none;
属性值:
h-shadow:它在字体周围设置水平阴影。
v-shadow:它设置字体周围的垂直阴影。
blur-radius:设置字体周围的模糊半径。
color:它设置字体周围的颜色。
none:它没有在字体周围设置任何内容。
示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> @import url(https://fonts.googleapis.com/css?family=Bangers); body { font-size: 50%; line-height: 1; } h1 { font: 8em/1 Bangers, sans-serif; text-align: center; text-shadow: /* These four shadows create the most outer stroke */ -.025em -.025em 0 #444, .025em -.025em 0 #444, -.025em .025em 0 #444, .025em .025em 0 #444; color: #fff; /* For non-webkit-browsers */ } </style> </head> <body> <h1>Hello World</h1> </body> </html>
效果图:
方法2:使用text-stroke属性
值得庆幸的是,现在可以直接使用text-stroke属性实现描述效果。
text-stroke是text-stroke-width和text-stroke-color(给文本填充颜色)两个属性的简写。
text-stroke属性用于向文本添加描边。此属性可用于更改文字的描边宽度和颜色。使用-webkit-前缀支持此属性。
语法:
text-stroke: <width> <color>;
示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> @import url(https://fonts.googleapis.com/css?family=Bangers); body { font-size: 50%; line-height: 1; } h1 { font: 8em/1 Bangers, sans-serif; text-align: center; -webkit-text-stroke: 1px black; color: #fff; } </style> </head> <body> <h1>Hello World</h1> </body> </html>
效果图:
有了text-stroke属性,咱们可以非常轻易的实现文本描边效果,也告别了使用text-shadow属性来模拟文本描边效果。
© 著作权归作者所有
发表评论