Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

n212's avatar
Level 2

Alpine.js class conditional not working with blade

Hi. I'm using Alpine.js with blade to add CSS to a given element once open is true. Everything seems to be working fine, except that ::class="{ 'text-red-200': open }" in the description div, it doesn't do anything.

I know open is set to true since clicking the button, the x-show attribute div does show my content dynamically.

Am I doing something wrong here with the class attribute from Alpine? Everything else seems to work fine.

{{-- Body --}}
<div class="bg-custom-secondary p-4" x-data="{ open: false }">
    <div class="" x-show="open">
        This works
    </div>
    {{-- Categories --}}
    <div class="">
        <span class="bg-custom-4th rounded text-xs font-bold py-1 px-2 text-white">
            Minecraft
        </span>
    </div>


    {{-- Description --}}
    <div ::class="{ 'text-red-200':  open }" class="text-sm mt-4 h-56 overflow-hidden">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Molestias, cum soluta praesentium, beatae
        nobis iste, fugit laboriosam accusamus veritatis ratione distinctio odio libero maxime ut ea! Minus
        ducimus assumenda incidunt!

    </div>

    {{-- Arrow down container --}}
    <div class="flex justify-center mt-4 cursor-pointer" @click="open = ! open">
        {{-- Arrow down --}}
        <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-white" fill="none"
            viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
            <path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" />
        </svg>
    </div>
</div>
0 likes
3 replies
Sinnbeck's avatar

Check your html to see if it actually gets the class. Perhaps you don't have the class as tailwind can remove it if it's not used

TimiAde's avatar

@Snapey @sinnbeck i am trying to bind class using the property in

 <div :class="$wire.candidate_id == {{$person->id}} ? 'outl' : ''" wire:click="set({{$person->id}})"  class="focused flex rounded-lg flex-row gap-2 drop-shadow-md items-center p-3  mx-auto sm:mx-0 bg-white hover:ring-primary-700 hover:ring-4 ring-offset-2 cursor-pointer dark:bg-gray-800 dark:text-white h-36 w-60">
</div>

it is showing this in chrome dev tools

<div :class="$wire.candidate_id == 1 ? 'outl' : ''" wire:click="set(1)" class="focused flex rounded-lg flex-row gap-2 drop-shadow-md items-center p-3  mx-auto sm:mx-0 bg-white hover:ring-primary-700 hover:ring-4 ring-offset-2 cursor-pointer dark:bg-gray-800 dark:text-white h-36 w-60">
              </div>

Please or to participate in this conversation.