链接img与css文件路径问题

时间:2015-12-21 23:48:36

标签: html css file background-image filepath

body {
  background: url("images/bg/cloud.jpg") no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

<head>
    <link rel="stylesheet" type="text/css" href="css/index.css">
</head>

根文件夹中包含带有index.css的css文件夹。根文件夹中还有图像和带有cloud.jpg的bg文件夹。

当css在根目录中时,它可以正常工作。然而,一旦我将css文件移动到css文件夹中并通过新的html路径重新链接它就可以找到图像!我觉得css文件正在搜索图像文件而无法找到它。它需要返回/退出css文件夹然后搜索images / bg / cloud.jpg?我想我是在正确的轨道上,如果我做/图像与图像有什么不同吗?并且还添加..让我回到文件夹层次结构中的图层?

好像我可以将图像文件放在文件夹中而不是css文件或我的文件路径错了?

2 个答案:

答案 0 :(得分:1)

这是因为相对路径。你必须添加点和斜线。想想css文件想要从哪里找到图像。使用:

background: url("../images/bg/cloud.jpg") no-repeat center center fixed;

这应该有用。

用两个圆点你说要移出一个文件夹,比如/ css /然后进入/ images /...

答案 1 :(得分:1)

如果图片文件夹 css文件夹位于根目录中。

改变这个:

background: url("images/bg/cloud.jpg") no-repeat center center fixed;

对此:

background: url("../images/bg/cloud.jpg") no-repeat center center fixed;

由于.css文件的相对路径,添加../会使文件路径返回一个文件(在这种情况下返回根目录)。