css – 具有两种颜色的文本

前端之家收集整理的这篇文章主要介绍了css – 具有两种颜色的文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要一个文本,在一定点x切换它的颜色.我提供了一个样本,使用文本两次来生成结果,切换为45px.有没有办法这样做在css没有文本两次?也许使用svg?
  1. div{
  2. width: 400px;
  3. height: 40px;
  4. border: 1px solid #000;
  5. position: relative;
  6. }
  7. div>span{
  8. position: absolute;
  9. top: 0;
  10. left: 0;
  11. }
  12.  
  13. div :nth-child(2){
  14. color: blue;
  15. clip: rect(0 200px 40px 45px);
  16. }
  17. div :nth-child(1){
  18. color: red;
  19. clip: rect(0 45px 40px 0);
  20. }
  1. <div>
  2. <span>Some bicolored Text</span>
  3. <span>Some bicolored Text</span>
  4. </div>

解决方法

另一个可能的选择是使用SVG.您可以使用梯度在SVG中创建多色文本.如果两个相邻的梯度停止位置在相同的位置,那么您将得到颜色之间的一个明显的过渡.如果两个相邻的梯度停止位置在不同的位置,那么您将获得颜色之间的平滑过渡.你可以拥有你想要的色块.例如…
  1. <svg width="200" height="80" xmlns="http://www.w3.org/2000/svg">
  2. <defs>
  3. <linearGradient id="bicolored">
  4. <stop offset="33%" stop-color="red"/>
  5. <stop offset="33%" stop-color="blue"/>
  6. </linearGradient>
  7. <linearGradient id="tricolored">
  8. <stop offset="33%" stop-color="red"/>
  9. <stop offset="33%" stop-color="green"/>
  10. <stop offset="66%" stop-color="green"/>
  11. <stop offset="66%" stop-color="blue"/>
  12. </linearGradient>
  13. <linearGradient id="smooth">
  14. <stop offset="33%" stop-color="red"/>
  15. <stop offset="66%" stop-color="blue"/>
  16. </linearGradient>
  17. </defs>
  18. <text x="0" y="20" fill="url(#bicolored)">Some bicolored Text</text>
  19. <text x="0" y="40" fill="url(#tricolored)">Some tricolored Text</text>
  20. <text x="0" y="60" fill="url(#smooth)">Some smooth gradient Text</text>
  21. </svg>

注意,在SVG中,颜色停止在相对位置(例如0到1,0%到100%).如果您尝试将颜色停留在特定的像素位置,这可能会使其有点困难.

请注意,您必须手动将文本元素放置在svg元素中.

猜你在找的CSS相关文章