0

I am looking to introduce PHP variables to stylesheets (ie. CSS).

I have worked out that I can print a PHP page as a stylesheet by declaring:

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

At the top of the CSS page.

However the variable I am passing is not displaying in the stylesheet.

In this case the PHP variable $css will be '-webkit-', '-moz-', '-ms-', or '-o-'.

And in the stylesheet I want to echo it in front of CSS3.

Originally I was achieving this by having a separate CSS file for each however this would be more efficient and allow me to pass genuine styling from the database, such as background-color and font.

Possible? How?

EXAMPLE PHP File called as a CSS link.

<?php
global $css;
header('Content-Type: text/css');

?>
.wheel {
    position:absolute; top:50%; left:50%; height:32px; width:32px; margin:-16px; <?php echo $css;?>transition:opacity 0.3s;
}
.wheel li {
    width:3px; height:9px; border-radius:2px; background:#555; <?php echo $css;?>animation:loading 1.2s infinite; position:absolute; <?php echo $css;?>transform-origin:2px 16px; left:16px; opacity:0; box-shadow:inset 0 0 2px rgba(255,255,255,0.4);
}

@<?php echo $css;?>keyframes loading { 0% {opacity:0.2;} 50% {opacity:0.9;} 100% {opacity:0.2;} }
5
  • I think this needs more information: where are you echoing the variable? Commented Aug 1, 2013 at 15:47
  • Can you show some of the code you're using? Commented Aug 1, 2013 at 15:47
  • What is it you think that "global $css;" is doing? Is this the entire PHP file or is it included in another one? Commented Aug 1, 2013 at 15:54
  • I don't wanna play mood killer but this smells like terrible practice no? How do you detect vendor prefixes? What is the point? You might want to consider using a css preprocessor like less, sass... developers.google.com/chrome-developer-tools/docs/… Commented Aug 1, 2013 at 15:57
  • possible duplicate of How do I set a variable in a PHP page, pass it to a CSS file and assign it to a single CSS attribute? Commented Oct 1, 2013 at 11:13

3 Answers 3

2

If you just want to be able to use variables in your css (not necessarily php), you could consider using less

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

Comments

1

You do this the same way you would with HTML:

<?php 
    header('Content-Type: text/css'); 
    $css = $_GET['css']; // or wherever your're initializing the variable from...
?>
body {
    <?= $css ?>border-radius: 3px
}

But, I don't think this is necessary for your use case. It's actually not uncommon to just statically include all the various -*- options in a css file:

body {
    -moz-border-radius: 3px;
    border-radius: 3px;
}

Just add all effective options, and the browser will determine which are most effective for it. This also means you get to avoid the dull and error prone task of browser sniffing.

8 Comments

+1, also there are arguments against dynamic CSS generation: programmers.stackexchange.com/a/118072
@Kian, Fully agree. And, there's really no good argument for it.
@Kian There are some good arguments for it. And you can cache the page anyways.
@PaulProgrammer :: What if you allow the user to determine how their web profile appears (ie. the styling.) you we need to store that information in database on a per user basis. Would you not?
I would generate separate CSS files (see @Benubird's suggestion below), and determine which CSS file to <link> from the profile.
|
0

SASS CSS extension would allow you to use variables without actually needing to use PHP and the downsides that come with it. Mixins would simplify the generation of vendor-specific style rules.

@mixin vendor-prefix($name, $argument) {
  -webkit-#{$name}: #{$argument};
  -ms-#{$name}: #{$argument};
  -moz-#{$name}: #{$argument};
  -o-#{$name}: #{$argument};
  #{$name}: #{$argument};
}
p {
  @include vendor-prefix(hyphens, auto)
}

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.