kimbl
kimbl

Reputation: 265

$variable in style="background-image: url:( );"

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

Answers (6)

Kevin
Kevin

Reputation: 9

Assuming your background picture is 'picture.jpg', the php code you need to type:

echo "<div style='background-image:url(&#039;picture.jpg&#039;)'">;

Upvotes: -2

Mouhamad Ounayssi
Mouhamad Ounayssi

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

Luka Krajnc
Luka Krajnc

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

SuperDJ
SuperDJ

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

NoobEditor
NoobEditor

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

Jenz
Jenz

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

Related Questions