7

I have a css file named test.css and I want to use into it of $var.$var is at test.php. test.css is attached in test.php. My structure is something like this:

//test.php

<html>
<head>
<?php $var = 'anything';?>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>

and this is test.css:

// test.css

.<?php echo $var> { // css property }

Currently test.css does not work. In fact, I want to knoe how can I use of a php variable as a class name into a css file ?

8
  • as written, impossible. css is not executed as a php script, so any php code in the css will go out to the client and be considered a css syntax error. you CAN make it execute as php on the server, but then it will execute completely SEPARATELY from the script that's producing the <link> tag, and $var will be undefined in the css. Commented Jul 3, 2015 at 21:03
  • I also add another solution for you. If you think that is the answer, please change your decision for new comers. Commented Jul 3, 2015 at 21:33
  • @MarcB when I put a .css file in head, It will be cache in user browser. but what happens when I write my css property on head between <style> and </style> ? what happens with each request ? is it optimized ? Commented Jul 3, 2015 at 21:35
  • then you're wasting a bit of bandwidth by sending the same css snippet on every one of your pages. it'd get cached as part of the page, but since it's duplicated on ALL pages, it'd be cached mutiple times. Commented Jul 3, 2015 at 21:48
  • @MarcB look, for preventing of parsing my data in html page, I use dynamic class name for my elements, and I create these names with php, now in your opinion, is it bad a idea ? Commented Jul 3, 2015 at 21:53

2 Answers 2

16

Actually you can.

1st Solution

Instead of using the .css file extension, use .php

Set up variables

<?php

   header("Content-type: text/css; charset: UTF-8"); //look carefully to this line

   $brandColor = "#990000";
   $linkColor  = "#555555";
?>

Use variables

#header {
   background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
  color: <?php echo $linkColor; ?>;
}

...

ul#main-nav li a {
  color: <?php echo $linkColor; ?>;
}

2nd and short solution

Create a file and name it like style.php, then in your style.php set your styles in tags like below

style.php

<style>
.blabla{
   ....
}

#heeeHoo{
   ...
}
</style>

then include style.php to your file (test.php) like

<html>
<head>
    <?php include 'style.php'; ?>
</head>
<body>

</body>
</html>

That is the correct answer. Think like inline css but that is actually in external file

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

8 Comments

your Idea is great ! +1 for you.
include or require or include_once or require_once ? which one is more optimized ?
Please see this answer for your new question: stackoverflow.com/a/3546316/1848929
then require_once() is the best in this case.
You should mod_rewrite style.css to style.php though imho
|
0

You can use <style> in the PHP file.

//test.php

<html>
<head>
<?php $var = 'anything';?>
</head>
<body>
<style>
<?php echo $var; ?>
</style>
</body>
</html>

The style can also be put in the <head>.

5 Comments

Then you'd have to configure your server to treat your css files as PHP files.
@mittmemo is it possible ?! and is it hard ?
@rjdown what ? what is your mean of Most browsers don't understand scoped , using <style> is a bad idea ?
Parsing CSS as PHP is Ill-advised. Just use a PHP file in your link. And @stack I was referring to the unedited answer stackoverflow.com/revisions/31214136/1 - but yes, using inline styles is generally a bad idea, especially half-way through the document.
@stack I had included the "scoped" attribute in my original answer and edited it out. rjdown saw that before it was edited out.

Your Answer

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