Changing CSS class class using PHP

How can I change the CSS class of an HTML element in response to a page change using PHP?

I already got a solution exactly like this , but this is a Javascript solution.

Basically, I want to add an extra class when a certain IF condition is met. This is my code.

<li <?php if ($selected == "profile") echo 'class="active"'; ?> class="dropdown">
<a href="">Profile</a>
</li>

      

From my code, you can see that you <li>

already got a class in it. So when I change echo

another class in IF

, it goes completely to class in IF

.

I need an additional class and not another class. How can i do this?

+3


source to share


3 answers


Just move it?

<li class="dropdown<?php if ($selected == "profile") echo ' active'; ?>">

      



The result is class="dropdown active"

, which is great in HTML - you can have multiple classes separated by spaces. Any CSS rules targeting both of these will apply, and you can even combine them to require both:

li.active.dropdown {
  /* Underline active dropdown elements */
  text-decoration:underline;
}

      

+10


source


 <li class="dropdown <?php if ($selected == "profile") echo 'active'; ?>">
    <a href="">Profile</a>
 </li>

      



try it

+2


source


You can try this:

<li class="<?php if ($selected == "profile") echo 'active'; ?> OTHER CONDITION HERE ">
    <a href="">Profile</a>
</li>

      

+2


source







All Articles