一、文本

background-clip:text 实现渐变文字

  1. div {
  2. color: transparent;
  3. background-clip: text;
  4. }

通过将文字设置为透明,原本 div 的背景就显现出来了,而文字以外的区域全部被裁剪了,这就是 background-clip:text 的作用。

https://code.juejin.cn/pen/7112616977457217544

  1. <h1>
  2. Improve how you
  3. <span class="bg-clip-text bg-gradient-to-r text-transparent pink200-to-blue500">
  4. architect
  5. </span>
  6. Improve
  7. </h1>
  8. <h1 class="bg-clip-text bg-gradient-to-r text-transparent pink-to-purple">
  9. Practical examples on CodeSandbox
  10. </h1>
  11. <h1 class="bg-clip-text bg-gradient-to-r text-transparent pink-to-orange">
  12. Practical examples on CodeSandbox
  13. </h1>
  1. h1{
  2. margin: 5rem 15rem;
  3. font-size: 5.5rem;
  4. font-weight: 800;
  5. }
  6. .text-transparent {
  7. color: transparent;
  8. }
  9. .bg-clip-text {
  10. -webkit-background-clip: text;
  11. background-clip: text;
  12. }
  13. .bg-gradient-to-r {
  14. background-image: linear-gradient(90deg,var(--tw-gradient-stops));
  15. }
  16. .pink200-to-blue500{
  17. --tw-gradient-from: #ec008c;
  18. --tw-gradient-to: #0070f3;
  19. --tw-gradient-stops: var(--tw-gradient-from),var(--tw-gradient-to,rgba(236,0,140,0));
  20. }
  21. .pink-to-purple {
  22. --tw-gradient-from: #FF00BB;
  23. --tw-gradient-to: #5C00CB;
  24. --tw-gradient-stops: var(--tw-gradient-from),var(--tw-gradient-to,rgba(51,140,245,0));
  25. }
  26. .pink-to-orange {
  27. --tw-gradient-from: #EC008C;
  28. --tw-gradient-to: #FFF200;
  29. --tw-gradient-stops: var(--tw-gradient-from),var(--tw-gradient-to,rgba(51,140,245,0));
  30. animation: huerotate 5s infinite;
  31. }
  32. @keyframes huerotate {
  33. 100% {
  34. filter: hue-rotate(360deg);
  35. }
  36. }

image.png

首字母大写

  1. text-transform: Capitalize;

  1. <p class="custom-text-selection">Select some of this text.</p>
  2. ::selection {
  3. background: aquamarine;
  4. color: black;
  5. }
  6. .custom-text-selection::selection {
  7. background: deeppink;
  8. color: white;
  9. }

自定义文本选择

image.png

蚀刻文本

  1. <p class="etched-text">I appear etched into the background.</p>
  1. .etched-text {
  2. text-shadow: 0 2px white;
  3. font-size: 1.5rem;
  4. font-weight: bold;
  5. color: #b8bec5;
  6. }

image.png

溢出滚动渐变

向溢出元素添加渐变以更好地指示有更多内容需要滚动。

  1. <div class="overflow-scroll-gradient">
  2. <div class="overflow-scroll-gradient__scroller">
  3. Content to be scrolled
  4. </div>
  5. </div>
  1. .overflow-scroll-gradient {
  2. position: relative;
  3. }
  4. .overflow-scroll-gradient::after {
  5. content: '';
  6. position: absolute;
  7. bottom: 0;
  8. width: 240px;
  9. height: 25px;
  10. background: linear-gradient(
  11. rgba(255, 255, 255, 0.001),
  12. white
  13. ); /* transparent keyword is broken in Safari */
  14. pointer-events: none;
  15. }
  16. .overflow-scroll-gradient__scroller {
  17. overflow-y: scroll;
  18. background: white;
  19. width: 240px;
  20. height: 200px;
  21. padding: 15px 0;
  22. line-height: 1.2;
  23. text-align: center;
  24. }

image.png
说明

  1. position: relative 在父级上为伪元素建立笛卡尔定位上下文。
  2. ::after 定义伪元素。
  3. background-image: linear-gradient(...) 添加从透明渐变为白色(从上到下)的线性渐变。
  4. position: absolute 从文档流中取出伪元素,并将其相对于父元素定位。
  5. width: 240px 匹配滚动元素的大小(它是具有伪元素的父元素的子元素)。
  6. height: 25px 是衰落梯度伪元素的高度,应当保持相对较小。
  7. bottom: 0 将伪元素定位在父元素的底部。
  8. pointer-events: none 指定伪元素不能是鼠标事件的目标,允许其后面的文本仍然是可选/交互式的。

文本高光

利用 background-clip, 我们还可以轻松的给文字增加高光动画。
点击查看【codepen】

  1. <p data-text="Lorem ipsum dolor"> Lorem ipsum dolor </p>
  1. p {
  2. position: relative;
  3. color: transparent;
  4. background-color: #E8A95B;
  5. background-clip: text;
  6. }
  7. p::after {
  8. content: attr(data-text);
  9. position: absolute;
  10. left: 0;
  11. top: 0;
  12. width: 100%;
  13. height: 100%;
  14. background-image: linear-gradient(120deg, transparent 0%, transparent 6rem, white 11rem, transparent 11.15rem, transparent 15rem, rgba(255, 255, 255, 0.3) 20rem, transparent 25rem, transparent 27rem, rgba(255, 255, 255, 0.6) 32rem, white 33rem, rgba(255, 255, 255, 0.3) 33.15rem, transparent 38rem, transparent 40rem, rgba(255, 255, 255, 0.3) 45rem, transparent 50rem, transparent 100%);
  15. //去掉伪元素的 background-clip: text,就能看懂原理:
  16. background-clip: text;
  17. background-size: 150% 100%;
  18. background-repeat: no-repeat;
  19. animation: shine 5s infinite linear;
  20. }
  21. @keyframes shine {
  22. 0% {
  23. background-position: 50% 0;
  24. }
  25. 100% {
  26. background-position: -190% 0;
  27. }
  28. }

添加了 mask 属性的元素,其内容会与 mask 表示的渐变的 transparent 的重叠部分,并且重叠部分将会变得透明
点击查看【codepen】

镂空文字

image.png

拿到了镂空文字后,我们还可以利用混合模式 mix-blend-mode: multiply 生成渐变色的文字。

  1. p {
  2. position: relative;
  3. -webkit-text-stroke: 3px #9a9acc;
  4. &::before{
  5. content: ' ';
  6. width: 100%;
  7. height: 100%;
  8. position: absolute;
  9. left: 0;
  10. top: 0;
  11. background-image: linear-gradient(45deg, #ff269b, #2ab5f5, #ffbf00);
  12. mix-blend-mode: multiply;
  13. }
  14. }

image.png

hover 悬停下划线动画

  1. <p class="hover-underline-animation">Hover this text to see the effect!</p>\
  2. .hover-underline-animation {
  3. display: inline-block;
  4. position: relative;
  5. color: #0087ca;
  6. }
  7. .hover-underline-animation::after {
  8. content: '';
  9. position: absolute;
  10. width: 100%;
  11. transform: scaleX(0);
  12. height: 2px;
  13. bottom: 0;
  14. left: 0;
  15. background-color: #0087ca;
  16. transform-origin: bottom right;
  17. transition: transform 0.25s ease-out;
  18. }
  19. .hover-underline-animation:hover::after {
  20. transform: scaleX(1);
  21. transform-origin: bottom left;
  22. }

image.png

禁用文本选择

  1. <p>你可以选择我。</p>
  2. <p class="unselectable">你不能选择我!</p>
  3. .unselectable {
  4. user-select: none;
  5. }

说明

user-select: none 指定无法选取文字。

文本溢出

文本超出不定行

文本 - 图9

  1. <div class="con">
  2. <div class="section">
  3. <h3 class="title">LGD 在 TI10 放猛犸,RNG 在 S7 放加里奥最后都输了,哪个更让你愤怒失望?</h3>
  4. <p class="excerpt">猛犸是对面的绝中绝,大家都知道,并且之前扳回两局已经证明了,当lgd选择ban掉猛犸,或者自己抢掉猛犸时,对面完全不是对手。</p>
  5. </div>
  6. <div class="section">
  7. <h3 class="title">《饥荒》为什么这么难玩?</h3>
  8. <p class="excerpt">玩的是联机版,30个小时了还没过第一个冬天。</p>
  9. </div>
  10. <div class="section">
  11. <h3 class="title">你觉得国漫的巅峰之作是哪个?</h3>
  12. <p class="excerpt">一直觉得国漫做的一般,之前看了灵笼这部国漫,做的不错,还有什么YYDS的国漫推荐吗!</p>
  13. </div>
  14. </div>
  15. html,body{
  16. margin: 0;
  17. height: 100%;
  18. display: flex;
  19. justify-content: center;
  20. align-items: center;
  21. }
  22. h3,p{
  23. margin: 0;
  24. }
  25. .con{
  26. width: 500px;
  27. overflow: hidden;
  28. resize: horizontal;
  29. }
  30. .section{
  31. display: flex;
  32. position: relative;
  33. border: 10px solid transparent;
  34. outline: 1px solid;
  35. overflow: hidden;
  36. height: 72px;
  37. margin: 10px;
  38. line-height: 1.5;
  39. text-align: justify;
  40. flex-direction: column;
  41. }
  42. .title{
  43. overflow: hidden;
  44. display: -webkit-box;
  45. -webkit-line-clamp: 2;
  46. -webkit-box-orient: vertical;
  47. font-size: 16px;
  48. margin-bottom: 3px;
  49. }
  50. .excerpt{
  51. font-size: 14px;
  52. margin-top: auto;
  53. flex: 1;
  54. overflow: hidden;
  55. max-height: 3em;
  56. }
  57. .excerpt::before{
  58. content: '...';
  59. float: right;
  60. height: 100%;
  61. shape-outside: inset(calc(100% - 1.5em) 0 0 0);
  62. display: flex;
  63. align-items: flex-end;
  64. margin-left: 2px;
  65. }
  66. .excerpt::after{
  67. content: '';
  68. position: absolute;
  69. width: 999vh;
  70. height: 999vh;
  71. background: #fff;
  72. box-shadow: -2em 2em #fff;
  73. }

点击查看【codepen】

文字排版布局

文字沿着不规则路径排版布局的实现

https://www.zhangxinxu.com/wordpress/2020/09/svg-text-around-path/

直线等图形3D穿过文字的CSS实现

https://www.zhangxinxu.com/wordpress/2021/02/css-3d-through/
image.png

文本特效-掘金

https://css-tricks.com/creating-playful-effects-with-css-text-shadows/
https://juejin.cn/post/6937102296442470413#heading-17