10

I'm trying to add dynamic variables (props) on a Tailwind class but something is wrong :

:class="`w-${percent}/12: ${show}`"

This is the output of this code :

<div class="w-0 h-2 transition-all duration-1000 ease-out bg-indigo-600 rounded-lg w-11/12: true"></div>

I don't understand why ':true' is added.

Thanks for your help.

Nb: https://fr.vuejs.org/v2/guide/class-and-style.html

4
  • when true is gone do you get what you want ? Commented Nov 4, 2020 at 22:02
  • If i delete : ${show} yes but I need this to make the transition work (the value of show depend on scroll position). Commented Nov 4, 2020 at 22:12
  • If i use v-if=‘show’ for ex it will work but without the wanted transition. Commented Nov 4, 2020 at 22:16
  • i am sorry i am not familiar with tailwind, i am trying to understand what do you want to see in $show ? just show ? Commented Nov 4, 2020 at 22:24

1 Answer 1

14

Because you use template literals which returns the string. So show is boolean true and it returns "true" as a string.

If you want to toggle class according to show variable, you must use object way.

<div
  class="w-0 h-2 transition-all duration-1000 ease-out bg-indigo-600 rounded-lg"
  :class="{ [`w-${percent}/12`]: show }"
>
  YOUR CONTENT
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Note that you shouldn't use interpolated class names with tailwind. Always use e.g. w-8/12 over w-{percent}/12: tailwindcss.com/docs/content-configuration#dynamic-class-names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.