简体   繁体   English

我如何在CSS中运行PHP

[英]How do i run PHP inside CSS

i have a stylesheet link like so 我有一个像这样的样式表链接

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

Inside the CSS I want to be able to echo a background image outputted by the db 在CSS内部,我希望能够回显db输出的背景图像

body{ background-image:url(../../images/<?php echo $theme.'/'.$background;?>);}

I tried adding the following to the beginning of my page but it just output everything in html code 我尝试将以下内容添加到页面的开头,但它只是输出html代码中的所有内容

<? header ("Content-type: text/css");?>

Im sure there is a bit to this, can anyone advise the best course of action? 我相信这有一点,有人可以建议最好的行动方案吗?

Thanks in advance 提前致谢

Change the file ending to php so it gets run by the server, and once you've done that you can link to it as if it was a regular css file and make sure the header is correct set with that header code you have at the top of the file 将文件结尾更改为php,使其由服务器运行,一旦完成,您就可以链接到它,就像它是一个常规的css文件一样,并确保标题是正确的,并设置了您在文件顶部

Change: 更改:

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

To: 至:

<link href="css/<? echo $theme;?>/styles.php" rel="stylesheet" type="text/css" />

and top of that file you have 你拥有的那个文件的顶部

<? header ("Content-type: text/css");?>

And btw since you seem to have php shorttags already enabled you might aswell use instead of using 顺便说一句,因为你似乎已经启用了php短标签,你可以使用而不是使用

<? echo $var;?>

Use 使用

<?=$var;?>

Here is a Way: Use that part of the CSS inside the HTML file. 这是一种方式:在HTML文件中使用CSS的那一部分。 Use double quotes " " and write the PHP file inside it. 使用双引号“”并在其中编写PHP文件。 Here is an example to use PHP inside CSS file. 这是在CSS文件中使用PHP的示例。

This CSS is in the HTML file and this code is working fine on my project. 这个CSS在HTML文件中,这个代码在我的项目中工作正常。 Let me know if any issue will test it. 如果有任何问题需要测试,请告诉我。

<style type="text/css">

        @media screen and (max-width: 650px) {
            .benifit {
                background-image:url("<?= $this->getBaseUrl() ?>/www/images/Capture.png") ;
                height:440px;
                border-radius: 5px 0px 0px 5px;
            }

        }
        @media screen and (min-width: 400px) {
            .benifit {

                width: 47%;
                background-image:url("<?= $this->getBaseUrl() ?>/www/images/Capture.png") ;
                height:440px;
                border-radius: 5px 0px 0px 5px;
            }
        }
        #ajax-loader {

            height:     100%;
            width:      100%;
            background: rgba( 255, 255, 255, .8 ) url("<?= $this->getBaseUrl() ?>/www/img/ajax-loader1.gif") 50% 50% no-repeat;
            overflow: hidden;
        }

    </style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM