CSS 中的完整路径?
例如,我的 css 文件中有这一行来加载图像。它是侧边栏中的标签图标。
background-image:url(../assets/images/icons/arrow_state_grey_expanded.png);
由于它是相对路径,有时当我打开某个页面时,它找不到要加载的图像。
示例 1(可以加载图像)
http://localhost/portal_dev/subkeyword_view/add_subkeyword
示例 2(无法加载图像)
http://localhost/portal_dev/subkeyword_view/view_subkeyword/20/20
除了将图像移动到根项目文件夹之外,如何解决此问题?仅供参考,我在我的项目中使用 codeigniter 2.0。通常对于 php 我只会调用图像文件的完整路径。
更新
由于 CSS 文件 URL 反映了 CSS 文件的位置,因此我的 css 已正确加载,并且仅当当前 url 包含参数时才会出现问题(参见示例 2),我认为问题是与 CodeIgniter 有关,而不是与 css 有关。
For example i have this line in my css file to load an image. It is a label icon in the sidebar.
background-image:url(../assets/images/icons/arrow_state_grey_expanded.png);
Since it is relative path, sometimes when i open a certain page, it cant find the image to load.
Example 1 (can load image)
http://localhost/portal_dev/subkeyword_view/add_subkeyword
Example 2 (cannot load image)
http://localhost/portal_dev/subkeyword_view/view_subkeyword/20/20
How to solve this problem beside move the image to root project folder? FYI, i am using codeigniter 2.0 in my project. Usually for php i will just called the full path of the image file.
UPDATE
Since CSS file URLs are reflective to the location of the CSS file, my css is loaded correctly, and the problem happen only when the current url contains parameter (see example 2), i think the problem is something to do with CodeIgniter not the css.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只是一个路径,请确保页面的标题反映 CSS 文件的确切位置,仅此而已,或者仅声明 CSS 文件的根 + 子文件夹
that's just a path thing, make sure the headers of your page reflect the exact location of the CSS file and that's it OR just declare the root + subfolder(s) for CSS files
考虑到您的 css 文件夹位于该位置
指定图像 url 路径,如下所示
considering your css folder is in the location
specify the image url path as below
在 CSS 文件中,URL 与 CSS 的位置相关。
因此,如果您的 CSS 文件位于
/.../css
中,并且图像位于/.../assets
中,即图像路径在 css 中是有效的,那么图像应该加载。您确定 CSS 正在加载吗?如果您的 CSS 没有使用绝对地址或根相对地址,我建议您使用其中任何一个。
否则,您可以添加
base
< /a> 标记到您的 HTML 文件中,这将对页面上的所有href
和src
属性有效。我遇到了与您描述的类似的 URL 问题。我使用
base
标签来解决我的问题In a CSS file URLs are reletive to the location of the CSS.
So if your CSS file is in
/.../css
and images re in/.../assets
i.e. the image path is valid from the css then the images should load.Are youy sure the CSS is loading. In case you are not using an absolute or root relative address for your CSS I would suggest using either of them.
Other wise you can add a
base
tag to your HTML file, this will be effective for all yourhref
andsrc
attributes on the page.I ran into a similar problem with URLs like you are describing. I used the
base
tag to solve my problem