1

I have a grid of items with all their data stored in a database, but I want each of their hover overlays to be the color associated with that item in the database.

Using a color picker (jscolor) during the upload process, the hex code is stored in the database. I can then get the hex code but cannot figure out a way of dynamically styling the background-color of the overlay in php.

This is sort of what I'm trying to achieve:

Color Overlay

CSS

.overlay-custom {
position: absolute;
width: 250px;
height: 250px;
opacity: 0;
filter: alpha(opacity=0);
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
-ms-transition-duration: 0.8s;
-o-transition-duration: 0.8s;
transition-duration: 0.8s;
-webkit-transition-property: opacity;
-moz-transition-property: opacity;
-ms-transition-property: opacity;
-o-transition-property: opacity;
transition-property: opacity;
  background-color:#00ff00;
  z-index:2;

I want to dynamically replace the background-color of this div class with the hex code from the database.

3
  • filter is not support in IE at all! Commented Dec 6, 2015 at 2:04
  • Just set a javascript variable in PHP with the hex value and then set the color with JavaScript. Commented Dec 6, 2015 at 2:05
  • is it not? damn.. could you suggest an alternative? That sound as though it would work. Could you possibly give me an example please? I'm a noob at javascript Commented Dec 6, 2015 at 2:12

1 Answer 1

4

Create an inline style using php when you originally output the .overlay-custom.

<?php
    // load overlay color from database somehow
    $overlay_color = get_overlay_color();
?>

<div class="overlay-custom" style="background-color:#<?php echo $overlay_color ?>"></div>

or

<?php
echo '<div class="overlay-custom" style="background-color:#' . $overlay_color . '"></div>';
?>

Then opacity and all that can still be adjusted in the css file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, would it be possible to put it in an echo? it also needs to add the #. Something like? echo '<div class="overlay-custom" style="background-color:#('$overlay_color')">';
Yes, there are lots of ways to get the # sign in there. See how I have edited the last line in the answer.
This is what I tried: echo '<div class="overlay-custom" style="background-color:#<?php echo $overlay_color ?>">'; echo '</div>'; however it didn't work
See my edit with the full echo. You didn't have correct php syntax in the above.
Ah! That was my problem. Damn syntax. Thanks so much, spent ages on that.

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.