0

In my CSS file a div has following property: background-color: blue;

Is it possible to change the color with PHP?

HTML:

<div id="div_id" class="div_class">
    Text
</div>

CSS:

.div_class {
   background-color: blue;
}

PHP:

if($variable == "Black")
{
    ... *change css color to black* ...
}
6
  • 1
    <div id="div_id" class="div_class" style="background-color: black;"> or add additional class like <div id="div_id" class="div_class div_class_black_bg"> Commented Mar 23, 2020 at 11:26
  • 1
    Why would you use PHP for this?JS and CSS would work fine Commented Mar 23, 2020 at 11:29
  • Are you using forms? Commented Mar 23, 2020 at 11:29
  • @AnuragSrivastava Yes It could be done with JS but I need to get the information which color the div gets (from the php variable) Commented Mar 23, 2020 at 11:33
  • @MARSHMALLOW No :) Commented Mar 23, 2020 at 11:33

5 Answers 5

4

You can do it by in-line style or additional class.

In-line:

<?php
    if($variable == "Black")
      echo '<div id="div_id" style="background-color:black" class="div_class">
        Text
      </div>';
?>

Additional class:

<?php
    if($variable == "Black")
      echo '<div id="div_id" class="div_class backgroundcolor-black">
        Text
      </div>';
?>

Your css looks like:

.div_class.backgroundcolor-black {
    background-color:black;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Vote for the "Additional class" solution.
4

Why not just append the variable to the class and add an override style:

<div id="div_id" class="div_class div_class--<?php echo strtolower($variable); ?>"  >
    Text
</div>
.div_class {
  background-color: blue;
}

.div_class--black {
  background-color: black;
}

5 Comments

if the user can pass the value via the url, this solution will be open to xss.
@Vidal There is no mention that this is a $_GET variable
yes but you are not doing any sanitizing on the variable $variable, and as I mention if the user can pass the value via url. just a note.
@Vidal the sanitizing should have happened from whatever changed the $_GET into $variable so why should I show that in my answer - even when the OP has not said this comes from the url directly - you are poking holes in an answer to a question that has not been asked
my friend, you haven't read my first comment...**IF** THE USER CAN PASS THE VALUE VIA THE URL. if the user can't then is not suppose to be an issue. I always sanitize and take measures of small details.
2

CSS :

.div_class{
    background-color: blue;
}
.div_red{
    background-color: red;
}
.div_green{
    background-color: green;
}

We can change color using below code

PHP:

$class = 'div_class';
if($variable == "red")
{
    $class = 'div_red';
} else if($variable == "green") {
    $class = 'div_green';
}

<div id="div_id" class="<?php echo $class; ?>">
    Text
</div>

I hope above will help.

Comments

1

<div id="div_id" class="<?= ($variable=='black')?'div_class_black':'div_class"?>"> Text </div>

In css:

.div_class{
    background-color: blue;
}

.div_class_black{
   background-color: black;
}

You have to create two classes

2 Comments

How does the ternary operator looks like when the are 3 oder 4 colors?
for multiple colors $colors = ['red','black','blue','yellow']; if (in_array($variable, $colors)){ echo "div_class_".$variable; }else{ echo "div_class_"; }
1

Define 2 clases

.div_class_blue{
   background-color: blue;
}
.div_class_black{
   background-color: #000;
}

on the PHP you can do a if/else with ternary operator.

<div id="div_id" class="<?php ($variable=="black")?"div_class_black":"div_class_blue" ?>"  >
    Text
</div>

PHP Manual

2 Comments

How does the ternary operator looks like when the are 3 oder 4 colors?
you can stack them, but in that case for easy reading people use regular if/else statements

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.