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

111
Visualizações
Conditional if statement with CSS

Is there any way to configure a conditional with CSS?

What I'm trying to configure is, having a single class, if the text on that class change so I can define different colors to the text. For example, if "div class:´test´" have assigned the text "Disabled" I want to apply red color to the text, but if the text for the same class is "Enabled" want to assign green color to the text.

Is there any way to achieve that with only one class?

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

0

The closest solution to the one you propose with css is using attribute selectors, an example below

p[data-text="Enabled"]{
   color: blue;
}

p[data-text="Disabled"]{
   color: red;
}
<p data-text="Enabled">Enabled</p>
<p data-text="Disabled">Disabled</p>

But with javascript you can get a solution like the following:

let elemento= document.querySelectorAll(".case")
elemento.forEach(e => e.setAttribute( "data-text",e.textContent ) )
.case[data-text="Enabled"]{
  color:red;
}
<p class="case">Enabled</p>
<p class="case">Disabled</p>

Update: improvement recommended by user @connexo

let elemento= document.querySelectorAll(".case")
elemento.forEach(e => e.dataset.text= e.textContent )
.case[data-text="Enabled"]{
  color:red;
}
<p class="case">Enabled</p>
<p class="case">Disabled</p>

about 4 years ago · Juan Pablo Isaza Relatório

0

Adding to the solution suggested by Usiel, you can also use CSS to display the text in the data-attributes:

[data-text]::before { content: attr(data-text); }

[data-text="Enabled"]{
   color: blue;
}

[data-text="Disabled"]{
   color: red;
}
<p data-text="Enabled"></p>
<p data-text="Disabled"></p>

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you could achieved this with Javascript. document.getElementById(id).style.property = new style as shown in JavaScript HTML DOM - Changing CSS

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