?> 背景知识: linear-gradient, text-shadow, text-decoration
通过text-decoration: underline
实现的下划线存在很多问题,比如无法控制位置、无法实现避让(text-decoration-skip
浏览器几乎都不支持)、更重要的是无法满足强迫症患者
的需求,并且不同语言有不同的对齐习惯,中文以中心对齐,英文以基线对齐,所以建议通过自定义来实现下划线。
<script v-pre type="text/x-template" id="underline-solid-cn-shadow"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { box-shadow: 0 -1px 0 0 #b4a078 inset; } </style> <script> </script> </script>
box-shadow
模拟下划线效果
<script v-pre type="text/x-template" id="underline-solid-cn-after"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { position: relative; } p > a:after { content: ''; width: 100%; position: absolute; bottom: 0; right: 0; left: 0; border-bottom: 1px solid #b4a078; } </style> <script> </script> </script>伪元素
after
模拟下划线效果
<script v-pre type="text/x-template" id="underline-solid-cn"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1.5 Helvetica, sans-serif; } p > a { padding-bottom: 1px; background: linear-gradient(#b4a078, #b4a078) no-repeat; background-size: 100% 1px; background-position: 0 18px; } p > a:hover{ animation: text-underline-slideInLeft 1.2s linear infinite forwards; } @keyframes text-underline-slideInLeft { from { background-position-x: -432px; } 50% { background-position-x: 0; } to { background-position-x: 432px; } } </style> <script> </script> </script>
linear-gradient
模拟下划线效果
<script v-pre type="text/x-template" id="underline-solid"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { background: linear-gradient(#b4a078, #b4a078) no-repeat; background-size: 100% 1px; background-position: 0 1em; text-shadow: .05em 0 white, -.05em 0 white; /* 避让超出基线以下的部分*/ } p > a:hover{ animation: text-underline-slideInLeft 1.2s linear infinite forwards; } @keyframes text-underline-slideInLeft { from { background-position-x: -432px; } 50% { background-position-x: 0; } to { background-position-x: 432px; } } </style>
linear-gradient
+text-shadow
模拟solid
型下划线效果
CSS tricks web developerperpers need to know!
<script> </script> </script><script v-pre type="text/x-template" id="underline-dashed"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { background: linear-gradient(90deg, #b4a078 66%, transparent 0) repeat-x; background-size: .3em 1px; background-position: 0 1em; text-shadow: .05em 0 white, -.05em 0 white; /* 避让超出基线以下的部分*/ } </style>
linear-gradient
+text-shadow
模拟dashed
型下划线效果
CSS tricks web developerperpers need to know!
<script> </script> </script><script v-pre type="text/x-template" id="underline-wavy-gradient"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { background: linear-gradient(45deg, transparent 45%, #b4a078 45%, #b4a078 60%, transparent 0), linear-gradient(-45deg, transparent 45%, #b4a078 45%, #b4a078 60%, transparent 0); background-repeat: repeat-x; background-size: .3em .15em; background-position: 0 1em, .2em 1em; text-shadow: .05em 0 white, -.05em 0 white; /* 避让超出基线以下的部分*/ } </style>
linear-gradient
+text-shadow
模拟wavy
型下划线效果
CSS tricks web developerperpers need to know!
<script> </script> </script><script v-pre type="text/x-template" id="underline-wavy"> <style> main { width: 100%; display: flex; flex-direction: column; align-items: center; padding: 39px 0; user-select: none; font: 16px / 1 Helvetica, sans-serif; } p > a { text-decoration: underline wavy #b4a078; } </style>
text-decoration: underline wavy #34495e
实现wavy
型下划线效果
CSS tricks web developerperpers need to know!
<script> </script> </script>缺点:text-decoration: underline wavy #34495e
几乎没有得到浏览器支持,并且波浪线大小无法单独控制。
创造良好的用户体验应当养成一种习惯~
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。