Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text with two colors

Tags:

css

svg

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. Is there a way to do this in css without having the text twice? Maybe using svg?

div{
  width: 400px;
  height: 40px;
  border: 1px solid #000;
  position: relative;
}
div>span{
  position: absolute;
  top: 0;
  left: 0;
}

div :nth-child(2){
  color: blue;
  clip: rect(0 200px 40px 45px);
}
div :nth-child(1){
  color: red;
  clip: rect(0 45px 40px 0);
}
<div>
<span>Some bicolored Text</span>
<span>Some bicolored Text</span>
</div>
like image 881
Manuel Schweigert Avatar asked Dec 15 '15 12:12

Manuel Schweigert


People also ask

How do I put two colors of text in HTML?

Steps to add multicolor into text: Add a simple text inside the <div> tag with the required selector. Apply the linear gradient property with any colors of your choice. Apply webkit properties that will fill the text with the gradient background and declare the color property with transparent background.

How do you split text color in Word?

To change the color of text in Word, select the text you want to modify and pick a new color from the Font Color drop-down menu on the Home tab. Each selection of text retains its own coloring. After picking the color for one selection, select another block of text and pick another color.

How do I combine two colors in Word?

Select the shape or text box. On the Drawing Tools Format tab, click Text Fill > More Fill Colors. In the Colors box, either click the color you want on the Standard tab, or mix your own color on the Custom tab. Custom colors and colors on the Standard tab aren't updated if you later change the document theme.


1 Answers

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);
}
<div data-text="Some bicolored Text">
</div>
like image 170
Mosh Feu Avatar answered Nov 16 '22 01:11

Mosh Feu