0


前端 CSS 经典:文字描边

前言:文字描边有两种实现方式

1. text-shadow

设置 8 个方向的文字阴影,缺点是只有八个方向,文字转角处可能有锯齿状。不支持文字透明,设置 color: transparent,文字会成描边颜色。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta
  7. name="viewport"
  8. content="initial-scale=1.0, user-scalable=no, width=device-width"
  9. />
  10. <title>document</title>
  11. <style>
  12. body {
  13. background: #000;
  14. }
  15. p {
  16. text-align: center;
  17. font-weight: bold;
  18. font-size: 5em;
  19. color: red;
  20. text-shadow: 0 -2px #fff, 2px 0px #fff, 0 2px #fff, -2px 0 #fff,
  21. -2px -2px #fff, 2px 2px #fff, 2px -2px #fff, -2px 2px #fff;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <p>人生若只如初见</p>
  27. <p>何事秋风悲画扇</p>
  28. <script></script>
  29. </body>
  30. </html>

2. -webkit-text-stroke

描边属性,支持文字透明。只留描边。缺点是兼容性,文字会变细,文字变细可以通过 JS 解决。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta
  7. name="viewport"
  8. content="initial-scale=1.0, user-scalable=no, width=device-width"
  9. />
  10. <title>document</title>
  11. <style>
  12. body {
  13. background: #000;
  14. }
  15. p {
  16. font-weight: bold;
  17. font-size: 5em;
  18. color: red;
  19. -webkit-text-stroke: 4px #fff;
  20. position: relative;
  21. }
  22. p::after {
  23. content: attr(data-text);
  24. position: absolute;
  25. left: 0;
  26. top: 0;
  27. text-align: center;
  28. -webkit-text-stroke: 0;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <p>人生若只如初见</p>
  34. <p>何事秋风悲画扇</p>
  35. <script>
  36. const ps = document.querySelectorAll("p");
  37. ps.forEach((p) => {
  38. p.dataset.text = p.textContent;
  39. });
  40. </script>
  41. </body>
  42. </html>
标签: css 前端

本文转载自: https://blog.csdn.net/weixin_64684095/article/details/139917685
版权归原作者 yqcoder 所有, 如有侵权,请联系我们删除。

“前端 CSS 经典:文字描边”的评论:

还没有评论