PHP 代码点火器 CSS
我从网站下载了一个 html 主题,并尝试将其放入我的 codeigniter 项目中。我将下载的主题文件夹中的 .html 文件复制并粘贴到我的视图“index_v.php”中。我还将css文件夹直接复制到views文件夹中。 index_v.php 文件像这样调用 .css 文件,
<link rel="stylesheet" href="style.css">
但是当我加载页面时,它不会加载 .css 文件。
I downloaded a html theme from a website and i'm trying to put it in my codeigniter project. I copy and pasted the .html file from the downloaded theme folder into my view "index_v.php". I also copied the css folder directly into the views folder. The index_v.php file calls the .css file like this
<link rel="stylesheet" href="style.css">
Yet when I load the page it doesn't load the .css file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可能遇到了相对路径的问题。如果您的网址是
/users/welcome/
,它会查找文件/users/welcome/style.css
解决方案:
使用完整网址:
使用 CI 的
link_tag()
它将把你的基本 url 添加到路径如果您无法直接访问该文件,请检查您的 .htaccess 文件(如果您正在使用它) 。大多数 CI 安装都使用这样的规则:
通过此规则,允许直接访问两个文件和一个目录(index.php、名为 public 的目录、robots.txt)。其余部分通过
index.php
路由。最好为所有静态文件(图像、CSS 等)创建一个目录,并将该目录名称添加到排除列表中。将这些文件保留在根目录中只会导致大量混乱。
You're probably running into an issue with relative paths. If your URL is
/users/welcome/
, it's looking for the file/users/welcome/style.css
Solutions:
Use a full URL:
<link rel="stylesheet" href="http://example.com/style.css">
Use CI's
link_tag()
which will prepend your base url to the path<link rel="stylesheet" href="/style.css">
If you are unable to access the file directly, check your .htaccess file if you are using it. Most CI installations use something like this:
With this rule, there are two files and one directory that are allowed direct access (index.php, a directory named public, robots.txt). The rest is routed through
index.php
.It's best to create a directory for all your static files (images, css etc.) and add this directory name to the exclusion list. Keeping those files in the directory root is just going to cause tons of clutter.
而不是这个
尝试这个:
或者如果你的 css 位于一个名为 css 的目录中:
你的 Javascript 将以同样的方式工作
Instead of this
Try this:
Or if you have your css in a directory called css:
Your Javascript will work the same way
你必须将该 CSS 文件放入用于提供静态内容的文件夹中(我有一段时间没有使用 CI.. 可能是
static/
?检查你的 (apache) 重写规则...) 。然后更改href
使其指向该目录中的文件。You have to put that CSS file to the folder which you use to serve static content (I haven't used CI for a while.. probably
static/
? check your (apache) rewriterule... ). Then change thehref
so it's points to the file in that dir.使用 firebug 并查看浏览器尝试解析 CSS 的 URL 是什么。我敢打赌您会发现它收到了 404。
尝试在包含文件中的 CSS 文件之前附加 /Application ,即
问题在于您的视图运行的上下文。
希望有帮助
Use firebug and see what the URL is that the browser is trying to resolve the CSS at. I bet you will find that it is getting a 404.
Try appending /Application before the CSS file in your include i.e
The problem is the context in which your view is running.
Hope that helps
在 codeigniter 中,css 等内容的路径是相对于您的 webroot index.php 文件的。
我建议您将 css 放在 webroot 中的 asset/css 或 /css 文件夹中,然后您的视图的链接将是
例如。
In codeigniter, the paths for content like css is relative to your webroot index.php file.
I advise you to place the css in an assets/css or /css folder in the webroot then your view's link will be
for example.
结合上面的一些答案,这就是我在 CodeIgniter 安装中加载 Blueprint.css 所做的事情:
打开 .htaccess。改变这个...
...对此:
在您的头文件(例如/application/views/template/header.php)中,使您的链接与站点相关:
请注意,此方法适用于任何静态内容(例如 Prototype.js、jQuery、常见图像等)。只需使您的 href 链接与站点相关,它就应该可以工作。
“static”作为文件夹名称并没有什么特别之处。这只是我随意选择的名字。
Combining some of the answers above, this is what I did to get Blueprint.css to load in my CodeIgniter installation:
Open .htaccess. Change this...
... to this:
In your header file (e.g. /application/views/template/header.php), make your links site-relative:
Note that this method works for any static content (e.g. Prototype.js, jQuery, common images, etc.). Just make your href links site-relative and it should work.
And there's nothing special about "static" as a folder name. It's just the name I chose arbitrarily.