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

195
Visualizações
How to override CSS style with javascript?

I have an interactive page that changes td background color dynamically, with javascript. Without any style, this page works fine. As a style requirement, table background color should be black, and td background should be white. But due to cascade rules, dynamic changes have lower priority, so javascript is not able to override static style. How can I solve this? Thanks.

Sample 1:

cell.style.cssText = "background-color: gray;" // it works fine

Sample 2:

table {
  background-color:black;
}

td {
  background-color: white;
}

cell.style.cssText = "background-color: gray;" // it does not work anymore, even with "!important;"
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Inline styles override styles defined in <style> and it works even with dynamic styling using cssText (see my code example). Your problem is probably somewhere else, e.g. in the cssText value itself (it should be background-color: gray;, not just gray

function foo() {
  document.getElementById("cell3").style.cssText = "background-color: green";
}
table {
  background-color:black;
}

td {
  background-color: red;
}
<table>
    <tr>
      <td>cell1</td>
      <td>cell2</td>
      <td id="cell3" onclick="foo()">cell3</td>
    </tr>
  </table>

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