Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP变量中的数字百分比调用为HTML/CSS?_Php_Css - Fatal编程技术网

如何将PHP变量中的数字百分比调用为HTML/CSS?

如何将PHP变量中的数字百分比调用为HTML/CSS?,php,css,Php,Css,我试图通过HTML/CSS的百分比,但我不能成功 我正在努力: <?php $myPercentage = 100; ?> 我正在尝试以HTML/CSS的形式传递变量。我希望我的进度条根据PHP值增加/减少 <style> .bar-4 {width:70%; height: 18px; background-color: red} /* Here is the problem, the above progress bar 4 is working and

我试图通过HTML/CSS的百分比,但我不能成功

我正在努力:

<?php
$myPercentage = 100;
?>
我正在尝试以HTML/CSS的形式传递变量。我希望我的进度条根据PHP值增加/减少

<style>
  .bar-4 {width:70%; height: 18px; background-color: red}

   /* Here is the problem, the above progress bar 4 is working and its width increases to 70% but below code is not working. */

  .bar-5 {width: <?php echo $myPercentage;?>%; height: 18px; background-color: #4CAF50;}

</style>
欢迎您提出意见或建议。
谢谢。

您的问题缺少一些重要数据,但以下是使其有效的一般指导原则:

要在CSS块中有一个动态变量,需要回显相关部分,或者将其包含在PHP文件中,而不是单独的CSS文件中

给定给变量的值必须在CSS块之前

例如,您的PHP文件应该有如下内容:

<?php
$myPercentage = 100;
?>
<style>
  .bar-5 {width: <?php echo $myPercentage;?>%;}
</style>
对于更干净的代码,.bar-5 CSS的其余部分最好保留在CSS文件中,并且只有动态值应该作为内联CSS打印。
您在生成的样式块中看到了什么?当页面第一次加载时,PHP在服务器上运行。您是如何更新$percentage的?您的html是通过PHP解析器运行的,还是只是读入并回显的,例如回显文件\u get\u contents'mystyle.html'?如果是后一种情况,那么您的代码就不会被处理。请注意,尽管在php中更新该变量不会更新已加载的页面,但用户需要重新加载才能看到update@Barmar谢谢你的回复。我知道百分比公式a/t*100,假设$percentage是100,我给css这个值。我怎样才能像css中的bar-4那样获得动态条呢。