donglian8407 2012-01-16 22:30
浏览 79
已采纳

可以通过PHP实现内联动态CSS吗?

I'm looking to dynamically generate some inline CSS and was wondering if it was possible via PHP.

Basically, I'm passing multiple variables via query string and am doing some arithmetic with the values to create values for CSS. Here's what my page looks like:

<?php
$var1 = "var1";
$var2 = "var2";
$divided_amount = $var1/$var1;
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Derp</title>
</head>
        <div style="height: <?php echo $divided_amount ?>px;">
Dynamic height content here
        </div>

</body>
</html>

This is not working out for me and I'm really trying to avoid going with an external stylesheet if possible.

Any help/insight is very much appreciated!

  • 写回答

3条回答 默认 最新

  • doudao2407 2012-01-16 22:39
    关注

    You are dividing $var1 with the same variable ($var1). Assuming you actually use numbers and not strings, this will always result in 1. Which means your div element will always be 1px high.

    Secondly, you should ceil() or floor() your result, just to make sure that you are not using floats to set pixel height.

    And remember to validate your HTML - this would have proved that something was wrong.

    Here's a working example:

    <?php
    $var1 = "111";
    $var2 = "7";
    $divided_amount = floor($var1/$var2);
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Derp</title>
        </head>
        <body>
    
            <div style="height: <?php echo $divided_amount ?>px; background-color: #F2F;">
    Dynamic height content here
            </div>
    
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器