Search code examples
csspseudo-class

how to change color of each letter of <p> with just css


I am trying to change each letter styles but I'm restricted from using javascript and without using span for each letters , I searched got some pseudo nth-letter but this not working , I know this type of question asked before but the restrictions force me to add this question.

please give some suggestions , it is really helpful

.change:nth-letter(3)
{
color:red;
font-size:20px;
}
<p class="change"> Color </p>


Solution

  • This is a hack that uses:

    • a monospace font so that each letter has a uniform width
    • inline display so that the width of the container is as much as its text
    • uses experimental background-clip: text to clip the background on to the text
    • uses a linear-gradient to color each letter

    See demo below:

    .change {
      font-size: 20px;
      font-family: monospace;
      display: inline;
      -webkit-background-clip: text;
      background-clip: text;
      color:transparent;
      background-image: linear-gradient(to right, black 40%, red 40% 60%, black 60%);
    }
    <p class="change"> Color </p>