1

I am trying to echo some inline CSS using PHP using this:

echo '<div class="image" style="background:url("img/testimage.jpg");width:300px;height:232px;">';
echo '</div>';

But for some reason this is returning this:

<div class="image" testimage.jpg");width:300px;height:232px;"="" img="" style="background:url("></div>

This is within a WordPress environment, am I doing something obvious wrong?

3 Answers 3

10

Escape the quotes inside the url declaration properly:

echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
                                    //         ^                  ^
Sign up to request clarification or add additional context in comments.

1 Comment

@fightstarr20 yeah that subtle thing will mess it up. im glad this helped
2

you can't run something like this (in HTML) correctly:

style="background:url("img/testimage.jpg");width:300px;height:232px;"

must merge between single and duple quotes or escape them:

style="background:url('img/testimage.jpg');width:300px;height:232px;"

solution:

echo "<div class='image' style='background:url(\"img/testimage.jpg\");width:300px;height:232px;'></div>";

Comments

1
echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';

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.