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

134
Visualizações
Change class attribute dynamically inline with tailwind

How do I pass in a dynamic value using a string literal with tailwind inline css?

For example I want to change the width dynamically, and not use tailwinds premade w-1, w-2, etc...

enter image description here

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The correct syntax would be w-[${...}px], but Tailwind relies to classes to be statically known, so this will probably not work.

The easiest thing would probably be to just use style:width="..." or style="width: ...". Though this will have high precedence due to being an inline style.

Alternatively, you can create a local component class that reads width from a custom property, then set the property using CSS attribute syntax, e.g. --width="..." (for components) or via style as style="--width: ...".

about 4 years ago · Santiago Trujillo Relatório

0

The other way around, if createTimeBlock will return a fixed set of values ( say [v1, v2, .... vN] ) for which there is a fixed set of width values to set ( assuming creatTimeBlock returns only increments of 15mins for example ), you can do this.

<script>
$timeblock = createTimeBlock( startTime, endTime );
</script>

<div
 class="p-1 border-2..."
 class:width_v1={timeblock === v1}
 class:width_v2={timeblock === v2}
 ...
 class:width_vN={timeblock === vN}
>
 {startTime} to {endTime}
</div>

<style>
 .width_v1{ 
   width: v1_width_px; 
 }
 .width_v2{ width: v2_width_px; }
 ....
 .width_vN{ width: vN_width_px; }
</style>

Note, the actual .width_v1 class can actually be a native width class to tailwind like say w-8. Hope this helps.

about 4 years ago · Santiago Trujillo 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