1

I'm trying to set a background image for the images preview of a gallery and nothing is shown. I'm using:

echo '<div class="prev" style="background-image:url(' . $image . ')"></div>';

In addition the prev class is:

.prev {
    width:120px;
    height:90px;
    background-size:cover;
    background-position:center; 
}

$image variable is ok, I echoed it for test, and when I put the same url (this echo) in a div not written by php, the image is shown perfectly.

4
  • just a minor style="background-image:url(' . $image . ');" add the semicolon ; Commented Jan 10, 2016 at 16:20
  • Thanks! but doesn't work neither :( Commented Jan 10, 2016 at 16:44
  • Are there any special characters in the url? Commented Jan 10, 2016 at 16:51
  • Inspect with firebus or similar the real link for image .. Commented Jan 10, 2016 at 16:51

2 Answers 2

1

url needs to be between quotes

echo '<div class="prev" style="background-image:url(\'' '. $image . '\');"></div>';
Sign up to request clarification or add additional context in comments.

1 Comment

But when I set the URL in CSS it works without quotes.... I tried your suggestion and result unfortunately is the same. Really weird.
1

I was correct:

echo '<div class="prev" style="background-image:url(' . $image . ')"></div>';

Works perfectly, the problem was the folder name (from where I get the $image variable), this folder contains a space, and PHP glob function reads the directory with spaces perfectly, so when I echo it I get something like:

images/name containing space/any_image.jpg

But CSS needs the URL without these spaces, to work it needs to replace the spaces with %20.

Thanks all for your expertise!

Comments

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.