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条)

报告相同问题?

悬赏问题

  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集
  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink