如何在 HTML 中的 URL 的 src 路径中上一层?
我将网站的样式表存储在 {root}/styles 中,而将图像存储在 {root}/images 中。 如何在样式表中给出路径以在图像目录中查找指定图像?
例如在 background-image: url('/images/bg.png');
I am storing style sheets in {root}/styles while images in {root}/images for a website.
How do I give the path in the style sheets to go look in the images directory for the specified images?
e.g. In background-image: url('/images/bg.png');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
..
表示父目录:Use
..
to indicate the parent directory:以下是您需要了解的有关相对文件路径的所有信息:
从
/
开始返回到根目录并从那里开始开始于
../
移动一个向后移动一个目录并从那里开始开始于
../../
向后移动两个目录并从那里开始(依此类推...)要向前移动,只需从第一个子目录开始并继续向前移动。
点击此处了解更多详情!
Here is all you need to know about relative file paths:
Starting with
/
returns to the root directory and starts thereStarting with
../
moves one directory backward and starts thereStarting with
../../
moves two directories backward and starts there (and so on...)To move forward, just start with the first sub directory and keep moving forward.
Click here for more details!
使用
../
:您可以根据需要多次使用它,例如
../../images/
,甚至可以在不同的位置使用,例如.. /images/../images/../images/
(当然与../images/
相同)Use
../
:You can use that as often as you want, e.g.
../../images/
or even at different positions, e.g.../images/../images/../images/
(same as../images/
of course)在 Chrome 中,当您从某些 HTTP 服务器加载网站时,会使用绝对路径(例如
/images/sth.png
)和某些上层目录的相对路径(例如../images/sth.png)。 png
)工作。但是!
当您从本地文件系统加载(在 Chrome 中!)HTML 文档时,您无法访问当前目录之上的目录。即您无法访问
../something/something.sth
并且将相对路径更改为绝对路径或其他任何内容都无济于事。In Chrome when you load a website from some HTTP server both absolute paths (e.g.
/images/sth.png
) and relative paths to some upper level directory (e.g.../images/sth.png
) work.But!
When you load (in Chrome!) a HTML document from local filesystem you cannot access directories above current directory. I.e. you cannot access
../something/something.sth
and changing relative path to absolute or anything else won't help.如果您将样式表/图像存储在一个文件夹中以便多个网站可以使用它们,或者您想在同一服务器上的另一个站点上重复使用相同的文件,我发现我的浏览器/Apache 不允许我访问网站根 URL 上方的任何父文件夹。出于安全原因,这似乎是显而易见的 - 人们不应该能够在服务器上除指定的 Web 文件夹之外的任何位置浏览。
例如。不起作用:www.mywebsite.com/../images
作为解决方法,我使用符号链接:
转到 www.mywebsite.com 的目录
运行命令 ln -s ../images images
现在 www.mywebsite.com/images 将指向 www.mywebsite.com/../images
If you store stylesheets/images in a folder so that multiple websites can use them, or you want to re-use the same files on another site on the same server, I have found that my browser/Apache does not allow me to go to any parent folder above the website root URL. This seems obvious for security reasons - one should not be able to browse around on the server any place other than the specified web folders.
Eg. does not work: www.mywebsite.com/../images
As a workaround, I use Symlinks:
Go to the directory of www.mywebsite.com
Run the command ln -s ../images images
Now www.mywebsite.com/images will point to www.mywebsite.com/../images
假设您有以下文件结构:
在 CSS 中,您可以访问
image1
,例如,使用行../images/image1.png
。注意:如果您使用的是 Chrome,它可能无法正常工作,并且您会收到一条错误消息,指出无法找到该文件。我遇到了同样的问题,所以我只是从 chrome 中删除了整个缓存历史记录并且它起作用了。
Supposing you have the following file structure:
In CSS you can access
image1
, for example, using the line../images/image1.png
.NOTE: If you are using Chrome, it may doesn't work and you will get an error that the file could not be found. I had the same problem, so I just deleted the entire cache history from chrome and it worked.
如果你想转到文件夹的根目录,请使用 / 或 ctrl+space
如果你想转到后面的文件夹,请使用 ../ 并按 ctrl+space(如果没有建议)
如果您使用../,则不要使用实时服务器
if you want to go to the root of the folder use / or ctrl+space
if you want to go to the back folder use ../ and ctrl+space if it dont suggest
and not use the live server if you use the ../