samyb8
samyb8

Reputation: 2588

Running php code within css

I am adding some php into a css file but the php does not seem to be running, rather I just see printed the whole php code..

.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;
}

Am I doing anything wrong? Or is it crazy to run PHP within a CSS?

Upvotes: 1

Views: 874

Answers (4)

jamesmortensen
jamesmortensen

Reputation: 34038

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".

Upvotes: 5

Kenneth
Kenneth

Reputation: 28737

EDIT: rewritten answer.

You can run PHP in a CSS-file, on one condition:

your server send requests for *.CSS-files through the PHP-pipeline.

Usually this is not a good idea, as this will process all your normally static CSS-files with PHP which is a performance loss.

The better option would be to rename your *.css-file to PHP and then just link the PHP-file as if it were a CSS-file

Upvotes: 2

svidgen
svidgen

Reputation: 14282

Yes, you certainly can have your CSS parsed by PHP, thus generating it dynamically. The easiest way to accomplish this is just to point your browser at a PHP script -- remember, it doesn't care what the extension is. It just prefers that the response be text/css.

In your HTML:

<link type='text/css' rel='stylesheet' href='/styles/default.php' />

At the top of your /styles/default.php:

header('Content-type: text/css');

You can also configure your HTTP daemon/service to run all your .css files through the PHP parser. But, then you have to run all your .css files through the parser -- an unnecessary performance hit in most cases.

However, in normal circumstances, it's better to simply define your stylesheet(s) to be flexible enough to accomodate all your pages. Ideally, your stylesheets "never" expire and the client "never" has to download them more than once. If you're generating styles dynamically, my suspicion is that you're missing the point. Dynamically generated rules should usually be embedding right into the page.

(There are exceptions to the rule, of course ... )

Upvotes: 2

Paul
Paul

Reputation: 141829

No it's not crazy, you just need to give you css file a .php extension (not .css), so that your web server knows to execute it as a PHP script, and add a few lines at the top to make sure that the browser knows it is CSS not HTML or raw text. Those lines would be:

<?php

header('Content-Type: text/css');

?>

Upvotes: 0

Related Questions