Reputation: 265
I have a question for using a variable
as IconPath for setting an background-image: url()
<div style="background-image: url($iconPath)"></div>
I have no idea how I can set the url for the background-image with a variable?
I tried with ""
, ''
, \"
Has anyone an idea?
Upvotes: 1
Views: 12138
Reputation: 9
Assuming your background picture is 'picture.jpg', the php code you need to type:
echo "<div style='background-image:url('picture.jpg')'">;
Upvotes: -2
Reputation: 361
if Your Server Side Language is PHP, u can use the below:
<div style="background-image: url('<?php echo $iconPath?>')"></div>
Upvotes: 1
Reputation: 915
You must echo the variable with PHP. Try like this:
<?php
$iconPath = "/yourPath/image.jpg";
?>
<div style="background-image: url(<?php echo $iconPath; ?> )"></div>
Upvotes: 1
Reputation: 7661
You could use
<div style="background-image: url(<?php echo $iconPath; ?>)"></div>
Or if it is supported (short tags)
<div style="background-image: url(<?= $iconPath; ?>)"></div>
Upvotes: 1
Reputation: 15911
PHP is a server side language, so if you want anything to be visible to DOM (i.e to your HTML), you will have to print it to DOM.This is achieved through echo
<div style="background-image: url('<?php echo $iconPath ?>')"></div>
Any variable value if needed to be printed, just echo
it and it'll be visible to DOM.
Upvotes: 5
Reputation: 8369
You can echo
the PHP variables to get its value in HTML. So try like this
<div style="background-image: url('<?php echo $iconPath;?>')"></div>
Upvotes: 1