With CSS is it possible to put 2 colors in 1 letter? Or half the text with a different color?

Asked

Viewed 4,828 times

27

I wonder if with CSS you can leave only half a letter, or half a word, with another color and the other half with another?

It would be possible to get this result from the image below only with CSS?

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

This is the base model I have at the moment.

h1 {
    font-family: sans-serif;
    font-size: 6rem;
    color: red;
    margin:.15em;
}
<h1>A</h1>
<h1>ABC</h1>

  • I lost. I’ll let someone answer

  • @Wallacemaxters if you have something with CSS would be interesting!

  • 1

    I tried to define the color: transparent and put two values in text-shadow. You can play with it if you want.

  • 1

    @Wallacemaxters think that with linear-gradient vc would have a better result huh...

5 answers

36


The closest I could get was using background-clip: textand text-fill-color: transparent.

Behold:

.two-colors{
   font-size: 160px;
   background: linear-gradient(to right, blue 50%, red 0);
   -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
 
  font-weight: 900;
  font-family: Arial;
  
}
<span class="two-colors">A</span>
<span class="two-colors">B</span>
<span class="two-colors">ABC</span>

I developed the CSS similar to this response in the SOEN, however I changed the direction of the gradient, to be half and half vertically colors and preferred to apply the style letter by letter.

In case, for the letters to be "in the same line", it is necessary that the element that receives the style has defined the display with the value inline-block or inline.

17

You can use an SVG with text inside it.

<svg width="200" height="80" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <linearGradient id="bicolored">
            <stop offset="33%" stop-color="red"/>
            <stop offset="33%" stop-color="blue"/>
        </linearGradient>
        <linearGradient id="tricolored">
            <stop offset="33%" stop-color="red"/>
            <stop offset="33%" stop-color="green"/>
            <stop offset="66%" stop-color="green"/>
            <stop offset="66%" stop-color="blue"/>
        </linearGradient>
        <linearGradient id="smooth">
            <stop offset="33%" stop-color="red"/>
            <stop offset="66%" stop-color="blue"/>
        </linearGradient>
    </defs>
    <text x="0" y="20" fill="url(#bicolored)">Some bicolored Text</text>
    <text x="0" y="40" fill="url(#tricolored)">Some tricolored Text</text>
    <text x="0" y="60" fill="url(#smooth)">Some smooth gradient Text</text>
</svg>

Source: https://stackoverflow.com/a/34293307/1452488

13

Using only CSS to do it this way

h1 {
  font-size: 72px;
  background: -webkit-linear-gradient(left, red 10%, blue 0%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
<h1>
The BLA BLA
</h1>

6

One more...

h1 {
    font-family: sans-serif;
    font-size: 6rem;
    margin: .15em;
    background: -webkit-linear-gradient(left,blue 50%, red 50%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    display: inline;
}
<h1>A</h1>

<h1>ABC</h1>

  • 1

    Could add an explanation. This linear-gradient always confuses me and I did not understand why yours was "without opacity" from one color to another. It became solid color. It is because of the left? (I used to left, has some difference?

6

It is also possible using a pseudo-element ::after with the content:"A" same as the text <h1>A</h1>, but "cut" at 50% width.

h1 {
    font-family: sans-serif;
    font-size: 6rem;
    color: red;
    margin:.15em;
    position: relative;
    display: inline-block;
}
h1::after {
    content: attr(data-texto);
    position: absolute;
    overflow: hidden;
    width: 50%;
    top: 0;
    left: 0;
    color: blue;
    text-transform: uppercase;
pointer-events: none;
  user-select: none;
}
<h1 data-texto="a">A</h1>
<h1 data-texto="ABC">ABC</h1>

  • It is an alternative to the previous ones. But in Firefox, at least it hinders a little the selection of text.

  • @fernandosavio cool the remark, you’re right, selecting in the hand gets kind of weird, but giving two clicks on the word is good, anyway is a putting to consider when choosing the method!

  • I was supposed to write that I was a good alternative to previous... But anyway, it is as an observation if someone bumps here.

  • You can do as in the example of that other answer of my question of the day, using ::after and ::before. Dai does not need to repeat the text at the time of selecting does not happen this effect (because it does not select anything)

  • It’s true, I even edited and put pointer-events: none; user-select: none; in the ::avter was worth the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.