Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!