我如何在CSS中运行PHP

时间:2021-10-02 07:12:38

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

提前致谢

2 个解决方案

#1


16  

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;?>

#2


0  

Here is a Way: Use that part of the CSS inside the HTML file. Use double quotes " " and write the PHP file inside it. Here is an example to use PHP inside CSS file.

这是一种方式:在HTML文件中使用CSS的那一部分。使用双引号“”并在其中写入PHP文件。这是在CSS文件中使用PHP的示例。

This CSS is in the HTML file and this code is working fine on my project. Let me know if any issue will test it.

这个CSS在HTML文件中,这个代码在我的项目中工作正常。如果有任何问题需要测试,请告诉我。

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

#1


16  

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;?>

#2


0  

Here is a Way: Use that part of the CSS inside the HTML file. Use double quotes " " and write the PHP file inside it. Here is an example to use PHP inside CSS file.

这是一种方式:在HTML文件中使用CSS的那一部分。使用双引号“”并在其中写入PHP文件。这是在CSS文件中使用PHP的示例。

This CSS is in the HTML file and this code is working fine on my project. Let me know if any issue will test it.

这个CSS在HTML文件中,这个代码在我的项目中工作正常。如果有任何问题需要测试,请告诉我。

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