How do i run PHP inside CSS
Change the file ending to php so it gets run by the server, and once you've done that you can link to it as if it was a regular css file and make sure the header is correct set with that header code you have at the top of the file
Change:
<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />
To:
<link href="css/<? echo $theme;?>/styles.php" rel="stylesheet" type="text/css" />
and top of that file you have
<? header ("Content-type: text/css");?>
And btw since you seem to have php shorttags already enabled you might aswell use instead of using
<? echo $var;?>
Use
<?=$var;?>
How to include PHP code in CSS?
Rename your CSS file to end in .php
,
styles.css -> styles.php
However, I doubt that you really need to have PHP code in a CSS file.
Good point from the comments below, place
<?php header("Content-type: text/css"); ?>
at the top of the file.
How can I use PHP code in a CSS file
Personally, I do not think this is the right problem to address.
The whole point in separating markup and styles is to simplify things. I see very little value in separating it so rigidly you have to mix css, php and sql instead (you just moved the same problem elsewhere).
My suggestion is to configure .box
's background using inline css in the html page itself and inject the url as any other value:
<div class="box" style="background: url('<?php ... ?>')">...</div>
Include PHP inside CSS file without PHP extension
This is just for apache2 and php? Then one idea would be to interpret the .css
as a .php
which can be done by editing the .conf
that includes the php extension or find this line somewhere in the config files:
AddType application/x-httpd-php .php
and edit to show
AddType application/x-httpd-php .php .css .phtml .etc .etc
Running php code within css
By default, most Apache installations will only interpret PHP code in actual .php files. The PHP processor won't process other file types without reconfiguring Apache (or whatever webserver you're running), and they'll -- by-default -- be rendered to the browser as whatever the default is for that file type.
However, from PHP, you can generate any type of content. Most people new to PHP think of HTML when they think about what the PHP will generate, but that's only the beginning:
<?php header('Content-Type:text/css'); ?>
<? /* php code defining $row2 */ ?>
.large {
width: 175px;
height: 175px;
position: absolute;
border-radius: 100%;
box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85),
0 0 7px 7px rgba(0, 0, 0, 0.25),
inset 0 0 20px 2px rgba(0, 0, 0, 0.25);
background: url('http://www.tahara.es/images/LargeImage/<?php echo $row2[imageLarge]; ?>.jpg') no-repeat;
}
Using this technique, you can generate HTML, JavaScript, CSS, XML, JSON, and other types of content.
Now, when injecting this server-generated CSS into your webpage, the link tag would look like this:
<link rel="stylesheet" type="text/css" href="myDynamicCSS.php" />
Note that the extension is .php, not .css, and the content sent to the browser as a final product is "text/css".
Related Topics
Create Ini File, Write Values in PHP
Compare Multiple Values in PHP
Serializing PHP Object to Json
Parentheses Altering Semantics of Function Call Result
How to Successfully Rewrite Old MySQL-PHP Code With Deprecated MySQL_* Functions
Page Redirect After Certain Time PHP
Best Way to Check If MySQL_Query Returned Any Results
Strtotime With Different Languages
How to Use PHP Namespaces With Autoload
Jquery - Call Ajax Every 10 Seconds
Calculating Days of Week Given a Week Number
Use One Bind_Param() With Variable Number of Input Vars
PHPunit Assert That an Exception Was Thrown
How to Hide/Encode/Encrypt PHP Source Code and Let Others Have the System