Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Cannot pass a pattern to background-color

I'm using React Javascript.

I'm trying to apply a background linear pattern to the cells of a table. The goal is to have the cell colored for a given percentage, while the remaining is white.

The function is the following:

    const describeColoredSensation = (val,r,g,b,a) => {
        const pos = 10 * val;
        const def = `rgba(${r},${g},${b},${a}) 0%, rgba(${r},${g},${b},${a}) ${pos}%, rgba(255,255,255,1) ${pos}%, rgba(255,255,255,1) 100%)`
        return (
                <td 
                    style={{ background: `${def}`}} 
                >{val}</td>
        )
    };

The function is call as

<tr>                
    {describeColoredSensation(item.value,253,253,62,255)}
</tr>

A sample def variable content is:

"rgba(253,253,62,255) 0%, rgba(253,253,62,255) 80%, rgba(255,255,255,1) 80%, rgba(255,255,255,1) 100%)"

But inspecting the browser the background color is not set (the background-color attribte is not present).

While I have just 4 different colors, I cannot use CSS, because the value of the val variable changes dynamically.

BTW, if I define const def = "rgba(255,0,0,255)"; it runs correctly, displaying all the cells in red.

Also, if I copy the content of the def variable in the debugger on the browser, it shows correctly.

So, I believe that the problem is how to make digest the statement to React Javascript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

By default, background takes only a single color. You should use a gradient if you want to mix colors.

Note, while the rgb color channels are in the range [0,255], the alpha channel is only in the range [0,1].

const describeColoredSensation = (val, r, g, b, a) => {
  const pos = 10 * val;
  const def = `linear-gradient(to right, rgba(${r},${g},${b},${a}) 0%, rgba(${r},${g},${b},${a}) ${pos}%, rgba(255,255,255,1) ${pos}%, rgba(255,255,255,1) 100%)`;
  return <td style={{ background: def }}>{val}</td>;
};

Edit cannot-pass-a-pattern-to-background-color

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda