2

Well, everyone knows we can use HTML inside a PHP file. So can we also use CSS inside a PHP file and still call it like <link rel="stylesheet" type="text/css" href="mystyle.php" /> to pass PHP as a CSS file? I know we can generate CSS from PHP doing something like echo "<style> (…) </style>", but will the above work?

6
  • Why not just echo the link? Commented Apr 29, 2013 at 16:58
  • Why do you need to do this? Commented Apr 29, 2013 at 16:59
  • You might wanna take a look here Commented Apr 29, 2013 at 16:59
  • @vcardillo well i know php isn't interpreted by the browser, i know we can pass a php file and it will understand it as a html, so passing a external php as a css might also work. yes i can echo it internally and not calling it as an external file, but i just had this curiosity, and right now i dont' have time to test this :D Commented Apr 29, 2013 at 17:03
  • So the only reason you want to do this is because you are curious? You shouldn't be defining your CSS in a .php file, unless there's a reason to do so. In that case, the server needs to first invoke PHP to interpret the file before its contents can be returned to the client, and thus you are adding overhead. Commented Apr 29, 2013 at 17:06

7 Answers 7

3

You can do this. In the mystyle.php, add in the first line:

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

Then do your code just like any PHP, using variables and such. Because of the header declaration, your php when opened by the browser will be parsed as a css file.

So you could do something like:

<?php 
header('Content-type: text/css');
/* ------------------------
    PHP Vars
------------------------ */
$color1 = "#fff";
$fontSize = "15px";

?>

.someSelector {
    color: <?=$color1;?>;
    font-size: <?=$fontSize;?>;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Straight answer, not questioning my intentions, great example, you really understood what i meant.
Straight forward clean, just what i wanted to know
@abelm Glad to be of help. I used to do this with javascript (I needed some info from DB but didn't wanted to code an api to make an ajax to get it later so I made the query inside the file and already appended the info on the script). But works same way to anything. That's sometimes used with those captcha script's images ( so <img src="somecaptcha.php" /> with header containing Content-type: image/png and php outputting an image for e.g.)
1

You can do this, i.e., auto-generating CSS with PHP in the mystyle.php script. The only thing to make it work reliably across browsers is to tell them, that it is indeed CSS:

<?php
header('Content-Type: text/css');
?>
/* your CSS here... */

Comments

1

CSS is used to style markup. PHP is not markup, so you cannot apply styles.

However, if you are simply trying to use a PHP script to output CSS, you can do it the same way you would output HTML:

<?php
    header("Content-type: text/css"); 
    echo "h1 { text-decoration: underline; }";
?>

or

<?php
     header("Content-type: text/css"); 
?>
h1 { text-decoration: underline; }

If you are attempting to output CSS from a script that also generates HTML, you can echo a <style block like you indicated in your question, or you can use inline styles in the HTML that you generate:

<?php
    echo '<h1 style="text-decoration: underline">...';

Comments

0

Anything outside <?php ... ?>, or echoed by the script, will simply be passed through to the browser. It can be anything, as long as the browser understands it. In your example, the browser will interpret the output of the PHP script as CSS, so it will work.

3 Comments

you mean this? <link rel="stylesheet" type="text/css" href="mystyle.php">
well i could test it but i don't have time now,,,loool
Yes, that's what you put in your question.
0

I think it is possible if you make sure your php files returns css with the including header types.

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

Comments

0

It should work if you set

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

as first line in your mystyle.php

Comments

0

I don't know what your intentions are, but if all you want is using variables I think a better approach can be using CSS preprocessor. Less is easy to learn and this solution can be quicker than executing the PHP each time a CSS is needed.

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.