【问题标题】:How to include CSS code directly in a php file如何在 php 文件中直接包含 CSS 代码
【发布时间】:2015-06-01 16:42:07
【问题描述】:

我一直在寻找这个,但没有运气,我只是好奇这是否可能。

是否可以在没有 HTML 代码的 php 文件中直接包含 CSS 规则?

img {
  background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
  height: 100%;
  width: 100%;
  position: fixed;
}

我只有这段代码,我认为没有必要创建一个 .css 文件来仅包含这一小段代码,否则我将创建 css 文件。

【问题讨论】:

标签: php css


【解决方案1】:

你可以使用<style>标签把它放到你的PHP文件中:

<style>
    img {
        background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
        height: 100%;
        width: 100%;
        position: fixed;
    }
</style>

或者直接把图片转成style属性。

<img style="position: fixed; width: ...">

【讨论】:

  • 没关系。 &lt;?php PHP here ?&gt; &lt;style&gt; img {...} &lt;/style&gt; &lt;?php other PHP ?&gt;。或者,当然,您可以回显它,例如 echo '&lt;style&gt; img {...} &lt;/style&gt;';。实现这一目标的方法有很多。
【解决方案2】:

是的,这只是一个“inline CSS”政策

【讨论】:

    【解决方案3】:

    PHP 在服务器上运行,而 css/html/javascript 都在 Web 浏览器上运行,所以你不能在两者之间有 css,恐怕你需要一点 html,但只是一点点

    应该是这样的:

    <?php <php code> ?>
    <style>
        img {
            background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
            height: 100%;
            width: 100%;
            position: fixed;
        }
    </style>
    <?php <more php code if you like> ?>
    

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 2021-11-28
      • 2018-10-09
      • 2017-09-20
      • 2014-12-07
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多