【问题标题】:CSS doesn't work when using DOCTYPE [closed]使用 DOCTYPE 时 CSS 不起作用 [关闭]
【发布时间】:2013-06-16 13:30:50
【问题描述】:

我有一个非常简单的html页面:

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/assets/css/main.css" />
</head>
<body>
        <div>hello world!</div>
        <br/>
        <div id='global_footer'>copyright 2013</div>
    </body>
</html>

main.css 是:

body { font-family: arial; background-color: rgb(185,179,175); }

我通过单击浏览器源代码页面中的链接来确保链接正常工作,并且浏览器确实正确加载了 css 源代码。

但是,CSS 不适用于页面。如果我删除 DOCTYPE 行,完全相同的代码可以正常工作。真的不知道是什么原因。

有关更多信息,我正在使用 CodeIgniter。 assets 文件夹位于我的文档根目录下,与应用程序文件夹同级。我已将 css 添加到根的 .htaccess 以确保它正确加载 css 文件而不添加 index.php。我觉得CI相关设置没问题,浏览器可以定位到css文件,不然没有DOCTYPE行应该不行。

谢谢。

----编辑----

各位,感谢您测试 html 和 css。我测试过,他们自己工作得很好。我认为问题出在CI。我的文档根目录中有 assets 文件夹,位于 subfoder 和 main.css 文件中。不知道为什么这个 html 和 css 在 CI 下不起作用。我的控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->helper(array('html','url'));
    $this->load->view('main');
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

谢谢。

【问题讨论】:

  • 它工作正常:jsfiddle.net/eHDS3(在预览中查看源代码,您会看到它有 &lt;!DOCTYPE html&gt;。您的问题在其他地方。
  • 非常适合我,我什至在没有 JSFiddle 的情况下在本地对其进行了测试。
  • 你是从127.0.0.1...还是从HD加载你的html文件
  • 顺便说一句:你不需要 http://127.0.0.1 链接到 css。 http://127.0.0.1 仅适用于您计算机上的本地服务器,但不适用于远程服务器。
  • 127.0.0.1 和 localhost 都链接到本地​​服务器,但你不应该混合使用这两者,使用一个或另一个

标签: php css codeigniter


【解决方案1】:

感谢您提供的所有信息/帮助。我刚刚发现了问题:

CSS 外部文件只能在正确的 MIME 类型下工作。我以前的 MIME 类型是 text/plain。我没有在我的 .htaccess 中配置任何东西,它在我的 CI 中以某种方式设置为这个。为了解决这个问题,我需要将 MIME 类型更改为 text/css。使用 Apache,我添加了

AddType text/css .css

到我的 .htaccess。现在可以了。

这个问题现在可以结束了。谢谢大家的帮助。

【讨论】:

  • 打勾即可结束问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2013-05-26
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多