im not sure how to ask this so im just gonna say it. i have the following:
<style type="text/css">
div.background
{
width:300px;
height:120px;
background:url(<?php
echo '<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />'
?>) repeat;
border:2px solid black;
}
div.transbox
{
width:200px;
height:100px;
margin:30px 50px;
background-color:#ffffff;
border:1px solid black;
opacity:0.6;
filter:alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p
{
margin:30px 40px;
font-weight:bold;
color:#000000;
}
</style>
</head>
<body>
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.
This is some text that is placed in the transparent box.
This is some text that is placed in the transparent box.
</p>
</div>
</div>
where:
<?php
echo '<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />'
?>
it displays, or should i say, its supposed to display an image based to what a user has selected and some text on top of the image. Obviously it doesnt work. my question is, Can i call php from within css embedded in a php page?.... if not is there a workaround? Thank you for reading.
It does not work not because of any PHP here, but mainly because of your syntax. You cannot inject HTML code into CSS:
background:url(<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />) repeat;
It will never works. Valid syntax below:
background:url('img/ships/ship_xxx_tn.jpg') repeat;
However if you want to generate CSS document with PHP, you have to change file's extension to .php
. Then server will regard it as PHP file and executes code:
background:url('img/ships/ship_<?php echo $user_ship['shipclass']; ?>_tn.jpg') repeat;
By taking a look at your code, I think there's a problem, sorry if I don't know a lot about css but this line disturbs me :
background:url(<?php
echo '<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />'
?>)
It would look like this :
background:url(<img src="img/ships/ship_shipclass_tn.jpg" />)
Shouldn't it be :
background:url(<?php
echo 'img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg'
?>)
??
Regards
You don't need the img tag in the css.
background:url(<?php echo '"img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg"'?>) repeat;
you can not use img tag in background:url();
here is the code that will work fine..
<?php
$image_name = 'Blue.jpg';
?>
<style type="text/css">
div.background
{
width:300px;
height:120px;
border:2px solid black;
background:url(<?php echo $image_name;?>);
}
div.transbox
{
width:200px;
height:100px;
margin:30px 50px;
background-color:#ffffff;
border:1px solid black;
opacity:0.6;
filter:alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p
{
margin:30px 40px;
font-weight:bold;
color:#000000;
}
</style>
</head>
<body>
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.
This is some text that is placed in the transparent box.
This is some text that is placed in the transparent box.
</p>
</div>
</div>
</body>