This is my code :
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
If I do
echo $img_Array["sizes"]["large"];
I get correct image path but when I use it inside background-image it does not worked for me.
When I used inspect element it displayed:
element.style {
background-image: url(<?php echo $img_Array[;
}
That means it needs to escape quotes(") of sizes , i tried to use /" but didnt work .
Any help would be appreciated . Thank you .
Try this..
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ' );">';
When you echo, you are already in PHP. No need to use <?php again.
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
Change it to:
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ');">';
It seems like you've tried to nest PHP code blocks. There are 2 possible solutions:
Echo the entire HTML code from the PHP block:
<?php
echo '<div class="banner" style="background-image: url('
. $img_Array["sizes"]["large"] . ');">';`
?>
Echo just the URL with a PHP block nested in HTML:
<div class="banner" style="background-image: url('<?php echo $img_Array["sizes"]["large"]; ?>');">
Related
On a Wordpress theme site I have this code:
<div class="gallery-img" style="background-image: url(<?php echo esc_url($image['sizes']['thumbnail']); ?>);">
However I need the background image url to be in ''.
So it works like this:
<div class="gallery-img" style="background-image: url('<?php echo esc_url($image['sizes']['thumbnail']); ?>');">
With it like that the PHP then does not work, how can I do the above so it works and so it has the '' around it?
How about inverting your single and double quotes ?
<div class="gallery-img" style='background-image: url("<?php echo esc_url($image['sizes']['thumbnail']); ?>");'>
I know I'm probably missing something really obvious, but the background image isn't pulling through...
<div style='background-image: url(<?php echo $blogImage; ?>");'>
I've also tried:
<div <?php echo "style='background-image: url($blogImage);'";?> >
Syntax error
Use this
<div style="background-image: url('<?php echo $blogImage; ?>');">
Hope this helps
You have a missing " before the php opening tag. try to add it and check.
<div style='background-image: url("<?php echo $blogImage; ?>");'>
My code always got this problem:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url(\''.$imgurl.'\'); ';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
But whatever I do, the html always shows:
<figure id="centerbg" class="centerbg" style="background-image: url("https://cdn.jsdelivr.net/example.png"); background-position: center center; background-attachment: inherit;">
I also tried:
<figure id="centerbg" class="centerbg" style="background-image: url(<?php echo $imgurl ?>);background-position: center center;background-attachment: inherit;">
But no use at all!
Can anyone help me? TAT
Thanks for your help, but seems my and your codes all work fine in this single file:https://api.mashiro.top/cover/test.php , but always get the escape character in my production site (the same HTML id as the cover image): https://2heng.xin/ , really strange, and could be any possibility that other parts get wrong in my site?
I don't know why style="background-image: url("https://cdn.jsdelivr.net/example.jpg");" could show images in my browesr (Chrome & Firefox), but this is not a good expression ugh?
You can also just remove the single quotes on your $get_style variable.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url('.$imgurl.');';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;height:100%;">
And it seems your site is using WordPress, so this link might help you as well.
You can mix double-quotes and single-quotes for this situation:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."'); ";
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
Just us this.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."');";
?>
You also can take a look into http://php.net/manual/en/function.htmlspecialchars-decode.php if you still face the same issue.
Try this out - I use this method a lot without issues.
<?php $imgurl = 'https://cdn.jsdelivr.net/example.png'; ?>
<figure id="centerbg" class="centerbg" style="background-image: url('<?php echo $imgurl; ?>'); background-position: center center; background-attachment: inherit;">
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?
Escape the quotes inside the url declaration properly:
echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
// ^ ^
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>";
echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';
I'm designing a web page to show some commodity
results (such Model, price, Comment) for every Commodity is a database
and I call theme to show in each Commodity (it while be shown in a div)
I wanna to set backgrounds for each div (it saved in database and every Commodity have one background-image)
please tell me whats the true syntax for this div s
for example I wrote this code:
<div class="commodities"> <style>.commodities{ background-image:<?php $Images[$i]?>} </style>
<?php
echo "Model:";
echo $Models[$i];
echo "<br><br>";
echo "Price: ";
echo $Prices[$i];
echo "<br><br>";
echo $Comments[$i];
?>
</div>
please help me to fix this part of code: { background-image:}
You have to do this:
<div class="commodities" style="background-image: url('<?php $Images[$i]?>');">
<?php
echo "Model:";
echo $Models[$i];
echo "<br><br>";
echo "Price: ";
echo $Prices[$i];
echo "<br><br>";
echo $Comments[$i];
?>
</div>
If you put < style > tags in the < body > (put it on the < head > tags) you are doing nothing ;)
Instead use style property. ;)
Apart you are forgetting to use url tag on the background-image property. ;)
You are missing two keywords, scoped and url. See e.g. http://www.w3schools.com/tags/att_style_scoped.asp and http://www.w3schools.com/cssref/pr_background-image.asp.
use inline css.
<div style="background-image: url(<?php echo $variable_name; ?>);">
or
use internal css.
<style type="text/css">
.logo {
background: #FFF url(<?php echo $variable_name; ?>);
}
</style>