PHP Variables in the CSS Stylesheet

后端 未结 3 1225
心在旅途
心在旅途 2021-01-28 12:40

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(\'Co         


        
相关标签:
3条回答
  • 2021-01-28 12:43

    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.

    0 讨论(0)
  • 2021-01-28 12:48

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

    0 讨论(0)
  • 2021-01-28 13:02

    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)
    }
    
    0 讨论(0)
提交回复
热议问题