Text with two colors

后端 未结 6 1355
醉话见心
醉话见心 2021-01-11 11:49

I would like to have a text that switches its color at a certain point x. I have provided a sample that uses the text twice to produce the result, with the switch at 45px. I

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-11 12:32

    You can use :before and :after pseudo classes:

    div {
      width: 400px;
      height: 40px;
      border: 1px solid #000;
      position: relative;
    }
    
    div:before,
    div:after {
      content:attr(data-text);
    }
    
    div:after{
      position: absolute;
      top: 0;
      left: 0;
    }
    
    div:after {
      color: blue;
      clip: rect(0 200px 40px 45px);
    }
    
    div:before {
      color: red;
      clip: rect(0 45px 40px 0);
    }

提交回复
热议问题