0

How can I change the color of the first few lines of text using css? Please check my snippet below, and all will be clear. I know that it is possible, but can't remember how... I have to be able to set a certain color for both halves.

.container{
  max-width: 300px;
}

h1 {
  position: relative;
  font-family: verdana;
  color: lightblue;
  text-align: left;
}

h1:after{
  content: "";
  position: absolute;
  left: 0px;
  right: 0px;
  top: 0px;
  height: 50%;
  background-color: rgb(255, 255, 255); 
  mix-blend-mode: difference;
}
<div class="container">
  <h1>This is a very very long text, that should be written in the same element, but with two different colors</h1>
</div>
csba
  • 720
  • 8
  • 20

1 Answers1

3

No pseudo-element needed. You can make this with only background-image. I used some CSS variable to control easily.

.container {
  max-width: 300px;
}

h1 {
  --line-height: 1em; /* we should use line-height */
  --line-count: 3; /* you can change the line count */
 
  line-height: var(--line-height);
  background-color: lightblue;
  background-image: linear-gradient(red, red);
  background-size: 100% calc( var(--line-height) * var(--line-count));
  background-repeat: no-repeat;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  
   position: relative;
  font-family: verdana;
  text-align: left;
}
<div class="container">
  <h1>This is a very very long text, that should be written in the same element, but with two different colors</h1>
</div>
doğukan
  • 23,073
  • 13
  • 57
  • 69