【问题标题】:Inline css work but external and internal css doesn't work in index.php内联 css 工作,但外部和内部 css 在 index.php 中不起作用
【发布时间】:2014-07-05 01:17:31
【问题描述】:

正如问题所暗示的,我有一个 index.php 文件,其中包含 html,当我尝试使用外部或内部 css 对其进行样式设置时,它不起作用。但是内联CSS有效。顺便说一句,我在win7上使用xampp来测试网站。文件结构看起来像这样 c:/xampp/htdocs/test/index.php

相关html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width>

        <!--<link rel="stylesheet" type="text/css" href="style.css">-->
        <!--This css file contains the css shown below as internal css-->       

        <style>
            body{
                background-color: blue;
                color: white;
            }
            .header{
                color: white;
            }
        </style>

        <?php
            function somefuntion(){
                /*I will be adding this function later once the html skeleton               
                is done.
                This function will be calculating some numbers based on the 
                current date and echoing it.*/
            }
        ?>
    </head>

    <body>
        <section class="header">
            <p>Spring</p>
        </section>
        <section class="body">
            <p>Hello world</p>
        </section>
    </body>
    </html>

谁能解释一下为什么内部 CSS 不起作用?

hRdCoder 找到的解决方法:元标记的内容属性中缺少 " 标记。

【问题讨论】:

  • 请确保不要在标头中注释掉您的外部 CSS 文件,否则将不会应用。
  • 我在使用内部和外部 css 时都有问题,所以在检查内部 css 是否有效时,我在标题中注释掉外部 css 文件,反之亦然。
  • 试试&lt;link rel="stylesheet" type="text/css" &lt;?php echo 'href="'.base_url().'/css/style.css"'; ?&gt;&gt;
  • 它说致命错误:调用未定义的函数 base_url()
  • 很抱歉;错字。 &lt;link rel="stylesheet" type="text/css" &lt;?php echo 'href="/test/style.css"'; ?&gt;&gt; 如果它位于名为 test 的文件夹中。如果不是并且文件在根目录下,请尝试&lt;link rel="stylesheet" type="text/css" &lt;?php echo 'href="/style.css"'; ?&gt;&gt;

标签: php html css


【解决方案1】:
<link rel="stylesheet" type="text/css" href="style.css"></link>

唯一需要检查的是您添加了结束标记,并且 style.css 文件与您的 index.php 位于同一目录中。

编辑:

我刚刚注意到您在内容属性中缺少最后一组引号 (")。这会影响页面的其余部分吗?

【讨论】:

  • 是的,您需要 href 属性路径才能真正找到 css 文件,以便从中加载任何 css。
  • 不,我的意思是&lt;/link&gt;
  • 那么路径可能不正确,或者有错字。这些只是我有时无法加载外部css的原因。 @Fred -ii- TIL “链接”的结束标签是可选的。
  • @Icarus 我刚刚注意到在您的 您缺少内容中的最后一组引号 (")属性。这会影响页面的其余部分吗?
  • @hRdCoder 我建议您在答案中放置your comment 的副本,这样将来的访问者就会清楚地了解这个问题。
【解决方案2】:

还要确保您的外部 css 文件中没有包含 &lt;style&gt;&lt;/style&gt; 标记

【讨论】:

  • css 文件没有任何样式标签。
【解决方案3】:

做一件事。通过 xampp 或 wampp 打开 css 文件 .. 喜欢 打开浏览器 -> 搜索 localhost/您的文件夹,然后打开 css 或 js 文件,现在复制该地址并粘贴到 href 或 src 中。希望这会有所帮助

【讨论】:

    【解决方案4】:

    链接外部 css 时路径有问题

    你的 index.php 和你的 style.css 是否在同一个文件夹中?

    <link rel="stylesheet" type="text/css" href="style.css">
    

    尝试将它放在一个名为 css 的单独文件夹中 然后将其链接为

    <link rel="stylesheet" type="text/css" href="css/style.css">
    

    您也可以使用 firefox 的 firebug 插件或 chrome 的 firebug lite 插件来检查错误

    【讨论】:

    • 尝试在同一文件夹和不同文件夹中使用 index.php 和 css 文件。两者都不起作用。
    • 真的吗?你能发布你的文件夹结构吗?
    • C:/xampp/htdocs/test 在 test 文件夹内存放 index.php 等文件
    • 尝试使用 firebug 插件,如果您从事 Web 开发不仅仅是路径问题,而且还有更多很棒的事情,它真的会对您有很大帮助。
    • 感谢您的建议。肯定会这样做,因为问题是由于缺少 " 标记。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多