1

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?

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean "apache, IIS, or other HTTP daemon" in place of "PHP" with regards to reconfiguration.
2

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

Comments

2

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

4 Comments

file extensions are meaningless you can have a *.css or a *.JGHMYGNMY file parsed as php
Yes, that is correct, but supposing that your webserver won't pass the .css-file through the PHP-pipeline, you can't. Like I said, you could just have a php-file emit CSS. Or, otherwise, reconfigure your webserver so it also treats CSS-files as PHP (which for obvious reasons you usually don't want)
Hi Kenneth, that's all stuff that should go into your actual answer as an edit. What you said in your comment would make it be a correct answer, IMHO. Hope this helps! :)
you don't have to parse all css files as php, you can just parse one. the server only does it for php because its told to, there's no difference regarding any other file.
0

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');

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.