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

timstats91's avatar

Tailwind Conditional Colors with If/ElseIf

I'm trying to render different colors in tailwind for a category button on blog cards using if/elseif statements. I'm really sure if this is the best way to do this so if anyone has a better solution I'm all ears.

However, my main question is this: the category slug and category name are being rendered correctly but the colors for the buttons are not. Why might this be?

Here is my code:

@if ($post->postCategory->id = 1)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#8AC49F] hover:bg-[#A4DEB9] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id = 2)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#79C9BF] hover:bg-[#90F0E3] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id = 3)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#C476C2] hover:bg-[#F794F4] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id = 4)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#CC5C90] hover:bg-[#FF73B4] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@endif
0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue in the code is that the assignment operator = is being used instead of the comparison operator == in the if/elseif statements. The assignment operator will always return true, so the first if statement will always be executed.

To fix this, you need to change the assignment operator = to the comparison operator == in each if/elseif statement.

Here's the corrected code:

@if ($post->postCategory->id == 1)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#8AC49F] hover:bg-[#A4DEB9] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id == 2)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#79C9BF] hover:bg-[#90F0E3] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id == 3)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#C476C2] hover:bg-[#F794F4] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@elseif ($post->postCategory->id == 4)
  <a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="bg-[#CC5C90] hover:bg-[#FF73B4] text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">{{ $post->postCategory->name }}</div>
  </a>
@endif

By using the comparison operator ==, the if/elseif statements will correctly compare the category ID with the specified values and render the appropriate colors for the buttons.

1 like
newbie360's avatar

@timstats91

@php
    $classes = [
        1 => 'bg-[#8AC49F] hover:bg-[#A4DEB9]',
        2 => 'bg-[#79C9BF] hover:bg-[#90F0E3]',
        3 => 'bg-[#C476C2] hover:bg-[#F794F4]',
        4 => 'bg-[#CC5C90] hover:bg-[#FF73B4]',
    ][$post->postCategory->id];
@endphp

<a href="/blog/categories/{{ $post->postCategory->slug }}" class="flex w-max no-underline">
    <div class="{{ $classes }} text-base rounded-full text-slate-700 font-semibold px-2 py-[1px] ease-in-out duration-300">
        {{ $post->postCategory->name }}
    </div>
</a>

Or turn it to blade component

1 like
Snapey's avatar

i would create an accessor on the model that returns a string to be used in the class statement based on the postCategory

But you will need to whitelist the colours in tailwind config

Also categories of 1,2,3,4 is a code smell. Who apart from you knows what these numbers mean? If these are IDs from a database table then they could easily be 5,6,7,8 if you had some small hiccup creating the database table

1 like

Please or to participate in this conversation.