download201401 2016-09-12 16:12 采纳率: 0%
浏览 249

通过php定义CSS颜色

I need a way of defining a css highlight colour as a php value and then using that as a class in my stylesheets.

I know that someone people do this as inline styles but I just have far too many elements using the class to put it inline. Ideally I need a way to echo the variable in the stylesheet (SASS?)

I can't find this anywhere, which seems odd!

  • 写回答

1条回答 默认 最新

  • doushizhou4477 2016-09-12 16:36
    关注

    At the moment you have 2 viable options.

    Using Javascript

    The first option is using some javascript inside your HTML file, in a script tag.

    Define a variable with PHP and use it to target the desired elements.

    var myVariable = "dodgerblue";
    
    $(".el").css("background-color", myVariable);
    body {
      counter-reset: example;
      display: flex;
      justify-content: space-around;
      height: 100vh;
      margin: 0;
    }
    div {
      flex: 0 0 10%;
      counter-increment: example;
    }
    div::after {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      content: counter(example);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="el"></div>
    <div class="el"></div>
    <div class="el"></div>
    <div class="el"></div>
    <div class="el"></div>


    Another option is like @cale_b suggests.

    Using an internal stylesheet.

    In your output (ideally in the section of your page), at some point after your stylesheet is referenced, cause PHP to do something like this: echo <style>.highlight_color {color: #ffff00;}</style>.


    Using CSS is not possible at the moment.

    Sadly there is not a CSS solution for this, yet.

    Hopefully some day browsers will support the use of attr() CSS function in all property values, not just content.

    div {
      background-color: attr(data-myvariable);
    }
    div::after {
      content: attr(data-myvariable);
    }
    <div data-myvariable="red">
      Example
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏