特别声明:如果您喜欢小站的内容,可以点击申请会员进行全站阅读。如果您对付费阅读有任何建议或想法,欢迎发送邮件至: airenliao@gmail.com!或添加QQ:874472854(^_^)
在Web中给文本添加下划线常常出现在<a>
链接的文本上,早期一般使用text-decoration
属性给文本添加下划线、删除线等。除了text-decoration
之外,CSS还有很多技术方案可以给文本添加下划线效果,比如border-bottom
、box-shadow
、background-image
等。对于Web开发者而言,更庆幸的是,CSS还有更多的,更灵活的特性实现文本下划线的效果。在这篇文章中,将和大家一起聊聊CSS中其他的特性怎么实现一个更有创意的效果。
新的text-decoration
特性
text-decoration
并不是一个新特性,在CSS 2.1中,text-decoration
就可以使用none
、underline
、overline
和line0-through
给文本添加下划线、删除线等效果。只不过,在新的CSS规范中(CSS Text Decoration Module Level 3 和 Level 4)添加了一些新特性。比如:
text-decoration-line: none | [ underline || overline || line-through || blink ]
text-decoration-style: solid | double | dotted | dashed | wavy
text-decoration-color: <color>
text-decoration-thickness: auto | from-font | <length>
text-decoration-skip: none | [ objects || [ spaces | [ leading-spaces || trailing-spaces ] ] || edges || box-decoration ]
text-decoration-skip-ink: auto| none
其中text-decoration-line
、text-decoration-style
和text-decoration-color
还可以简写成text-decoration
:
text-decoration: <text-decoration-line> || <text-decoration-style> || <text-decoration-color>
除些之外,还新增了text-underline-position
和text-underline-offset
属性给文本设置下划线样式:
text-underline-position: auto | [ under || [ left | right ] ]
text-underline-offset: auto | <length>
来看一个简单的示例:
自定义下划线效果
文章开头就提到过,除了使用text-decoration-*
和text-underline-*
属性可以给文本添加下划线效果之外,还可以使用一些其他方法来给文本添加自定义下划线的效果,比如下面两篇文章中提到的方法:
随着CSS的Clipping和Masking技术越来越成熟,我们可以配合CSS的伪元素实现一些更有创意的下划线效果。
使用clip-path
给文本添加下划线
@Travis Almand有篇文章专门介绍clip-path
实现的不同动画效果。比如下面这样的一个效果:
div {
width: 200px;
height: 200px;
background-color: #f36;
animation: melt-enter 2s ease-in alternate infinite,melt-leave 4s ease-out 2s alternate infinite;
cursor: pointer
}
@keyframes melt-enter {
0% {
clip-path: path('M0 -0.12C8.33 -8.46 16.67 -12.62 25 -12.62C37.5 -12.62 35.91 0.15 50 -0.12C64.09 -0.4 62.5 -34.5 75 -34.5C87.5 -34.5 87.17 -4.45 100 -0.12C112.83 4.2 112.71 -17.95 125 -18.28C137.29 -18.62 137.76 1.54 150.48 -0.12C163.19 -1.79 162.16 -25.12 174.54 -25.12C182.79 -25.12 191.28 -16.79 200 -0.12L200 -34.37L0 -34.37L0 -0.12Z');
}
100% {
clip-path: path('M0 199.88C8.33 270.71 16.67 306.13 25 306.13C37.5 306.13 35.91 231.4 50 231.13C64.09 230.85 62.5 284.25 75 284.25C87.5 284.25 87.17 208.05 100 212.38C112.83 216.7 112.71 300.8 125 300.47C137.29 300.13 137.76 239.04 150.48 237.38C163.19 235.71 162.16 293.63 174.54 293.63C182.79 293.63 191.28 262.38 200 199.88L200 0.13L0 0.13L0 199.88Z');
}
}
@keyframes melt-leave {
0% {
clip-path: path('M0 0C8.33 -8.33 16.67 -12.5 25 -12.5C37.5 -12.5 36.57 -0.27 50 0C63.43 0.27 62.5 -34.37 75 -34.37C87.5 -34.37 87.5 -4.01 100 0C112.5 4.01 112.38 -18.34 125 -18.34C137.62 -18.34 138.09 1.66 150.48 0C162.86 -1.66 162.16 -25 174.54 -25C182.79 -25 191.28 -16.67 200 0L200 200L0 200L0 0Z');
}
100% {
clip-path: path('M0 200C8.33 270.83 16.67 306.25 25 306.25C37.5 306.25 36.57 230.98 50 231.25C63.43 231.52 62.5 284.38 75 284.38C87.5 284.38 87.5 208.49 100 212.5C112.5 216.51 112.38 300.41 125 300.41C137.62 300.41 138.09 239.16 150.48 237.5C162.86 235.84 162.16 293.75 174.54 293.75C182.79 293.75 191.28 262.5 200 200L200 200L0 200L0 200Z');
}
}
效果如下:
如果你的浏览器没看到任何效果的话,请更换Firefox 63+浏览器查阅,你将会看到下面这样的效果:
将这个创意放到文本下划线中也是可以的,只不过需要借助CSS的伪元素:
div {
display: inline-flex;
font-size: 30px;
position: relative;
cursor: pointer;
&::after {
content: '';
position: absolute;
top: calc(100% + 6px);
left: 0;
right: 0;
height: 10px;
background-color: currentColor;
animation: 2s melt-enter;
}
&:hover::after {
color: #f36;
animation: 2s melt-leave;
}
}
效果如下:
使用SVG实现自定义下划线效果
使用clip-path
给文本添加下划线效果,可以帮助我们实现很多具有创意的效果,还可以配合CSS的animation
来实现带有动画效果的下划线。其实,除了上面提到的方案之外,我们还可以在background-image
中使用SVG给文本添加很多与众不同的下划线效果。比如:
上面的示例中引用像下面这样的一个使用SVG绘制的线条:
<svg xmlns="http://www.w3.org/2000/svg" id="Layer_1" viewBox="0 0 260 15.6">
<style>
.st0{fill:#f3bc34}
</style>
<path class="st0" d="M206.8 7.3l-.1.3c.1-.2.2-.3.1-.3zM234.7 10h-.1c-.2.4-.1.3.1 0zM54.8 4.2l-.6-.4c.2.4.4.5.6.4zM17.1 5.1zM34.5 9.6l.1.3c0-.2 0-.3-.1-.3zM22.4 10.8c-.3-.1-.7-.1-1-.1.2.1.7.1 1 .1zM17.5 5c-.1.1-.2.1-.4.2.2-.1.3-.2.4-.2zM52.7 9.8l.5.9c-.1-.3-.3-.6-.5-.9zM19.5 11.6c-.2-.2-.4-.2-.6-.3 0 .2.3.3.6.3zM120.9 11.4c-.1.1-.2.2-.2.3.3-.1.3-.2.2-.3zM80.9 10.4h-.1s.1.1.2.1l-.1-.1zM92.6 10.4l-.2.2c.2-.1.2-.1.2-.2zM72.1 11.3c-.1.1-.3.2-.4.3l.4-.3z"/>
<path class="st0" d="M260 6c-1-.6-4.7-1.2-5.8.3-.2-.1.1-.3.2-.4-.9.2-2.2.1-3.6 0s-2.9-.2-4.2 0c-1 1.5-3.9-.6-4.8 1.4l.5-.4c.9.5-1.2 1.4-1.5 1.9-.8-1.2-.1-1-1-2l1.1.4-.3-1c-3.1 2.8-6.2-.9-8.2 1.1.1-.1.1-.3.2-.4-1.4-.5-2.3.8-3.3 1.2-.1-.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0-1.9-.2-1.7-.7-.4.3-.8.8-1.4.8l.3-.6c-.4.1-.8.5-1.1.6l.6.4c-.9-.5-2.6.8-2.6-.4h.3l-1.7-.5c-.7.5-1.3 1-2.5.9-.5-1.3-2.9-.2-4.3-.3l.1-.4c-1.1.6-4 .4-3.5.6-1.1 0-2.6-.2-2-.6-.8.1-2.7.1-3.2.9l-1.8-1c-1 1.6-3.6-.5-3.6 1.2-1-.2-.8-.6-1.5-.9-1.4.9-2.8.8-4.2.7v-.2c-1.4-.1-3.1.8-5.1 1l-.5-1.2c-1 .2-1.3 1.2-2.3 1-.2-.2 0-.3.2-.3-1 .3-2.3.1-3.1-.2-1.5 1-2.7.7-3.9 1.8-1.3-1 1.7-.6.6-1.6-2.2-.4-4.4.4-6.7 1.1-.2-.2 0-.4.1-.7 0 0-1.2.9-2.2 1.8C.9 8.3 0 9.4.5 10c-.5.9-1.2 1.4.9 2 .6-.5 2.5-1.3 2.9
如需转载,烦请注明出处:https://www.w3cplus.com/css/custom-underlines-in-css.html
如果文章中有不对之处,烦请各位大神拍正。如果你觉得这篇文章对你有所帮助,打个赏,让我有更大的动力去创作。(^_^)。看完了?还不过瘾?点击向作者提问!