Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我在使用以前的目录启动css相对url时遇到问题_Css_Url_Directory - Fatal编程技术网

为什么我在使用以前的目录启动css相对url时遇到问题

为什么我在使用以前的目录启动css相对url时遇到问题,css,url,directory,Css,Url,Directory,我正在加载一个带有如下url的背景图像: background-image: url(../../images/folder/imageName.png); background-image: url(/../../images/folder/imageName.png); http://localhost/rootDirectory//pageName/stylesheet.css 对于某些页面,这很好,但对于其他页面,只有在前面加一条斜线,它才有效,如下所示: background-

我正在加载一个带有如下url的背景图像:

background-image: url(../../images/folder/imageName.png);
background-image: url(/../../images/folder/imageName.png);
 http://localhost/rootDirectory//pageName/stylesheet.css
对于某些页面,这很好,但对于其他页面,只有在前面加一条斜线,它才有效,如下所示:

background-image: url(../../images/folder/imageName.png);
background-image: url(/../../images/folder/imageName.png);
 http://localhost/rootDirectory//pageName/stylesheet.css
我觉得奇怪的是,这种方法有时能奏效,而其他的却不行。而且,我的直觉告诉我,用斜杠开始这些URL是完全错误的


有人对此有什么见解吗?

在没有协议(http://,等等)的情况下启动URL意味着它与当前文档相关。如果不同的页面位于不同的文件夹中,则相对URL指向的文件将发生更改,这可能是您的问题

以斜线开头的URL使其相对于页面所在的整个域/子域。我认为这是一个很好的练习,所以鼓励你使用它。


但是,在您的示例中,您使用.././,这是“上面的目录”提示,这些提示被忽略,因为您已经位于域的底部。

我想我已经找到了答案。我的样式表位于如下地址:

background-image: url(../../images/folder/imageName.png);
background-image: url(/../../images/folder/imageName.png);
 http://localhost/rootDirectory//pageName/stylesheet.css
注意rootDirectory和pageName之间的两个斜杠。似乎当我的图像使用。。要返回目录,它将这些斜杠之间的空白空间视为目录本身(而加载样式表时不会将其视为自己的目录,也不会影响加载的路径)


这就是为什么对于某些样式表,........../md似乎起作用,而对于其他样式表则不起作用(事实上,......../md起作用)

这是在外部样式表中吗?您是从不同级别的目录结构引用它吗?它是一个外部样式表,但它们在目录结构中都处于同一级别。结构有static/css//someCss.css和static/images//pngImageOrSomething.pnginteresing,我想我将以斜杠开始,听起来是个好主意。