css中的图像路径无法正常工作

时间:2023-01-16 14:14:04

I am trying to use images in css file in my laravel project that makes use of twitter bootstrap and Jason Lewis' Basset.

我正在尝试在我的laravel项目中使用css文件中的图像,该项目使用了twitter bootstrap和Jason Lewis的Basset。

Now when I use a image in my css like

现在,当我在我的CSS中使用图像时

.div {
  background: url('asset/img/photo.jpg');
}

I get the path to

我明白了

http://localhost/asset/css/less/assets/img/photo.jpg

However the real path should be

然而,真正的道路应该是

http://localhost/~Username/laravelProject/public/asset/img/photo.jpg

The only way to achieve this is to use this url in my css

实现这一目标的唯一方法是在我的CSS中使用此URL

/../~Username/laravelProject/public/asset/img/photo.jpg

/../~Username/laravelProject/public/asset/img/photo.jpg

You guys would understand this is a no go if I have to move the site from local to server and have to change all the urls every time.. So Why is this not working properly? I am using this laravel starter site made by andrew13

如果我必须将站点从本地移动到服务器并且每次都必须更改所有URL,那么你们会理解这是不行的。那么为什么这不能正常工作?我正在使用这个由andrew13制作的laravel启动站点

startersite github

startersite github

Last but not least.. this is the structure

最后但并非最不重要..这就是结构

css中的图像路径无法正常工作

1 个解决方案

#1


-1  

The issue is that the path is bellow the css doc, relative file paths work by adding there files location to the beginning of the url, this means that it is looking for (Mypath)/asset/img/photo.jpg

问题是路径低于css doc,相对文件路径通过将文件位置添加到url的开头来工作,这意味着它正在寻找(Mypath)/asset/img/photo.jpg

(or: public/asset/css/asset/img/photo.jpg from what I can see)

(或者:我可以看到的public / asset / css / asset / img / photo.jpg)

Without a PHP server to return dynamic url's, sadly your only 2 options are:

没有PHP服务器来返回动态网址,遗憾的是你只有两个选项:

  • Move the CSS to the root, or at least lower then the IMG
  • 将CSS移动到根目录,或者至少低于IMG
  • Use a absolute url
  • 使用绝对网址

With php you have a few options, my favorite being a "Node" structure

How this works is placing a php file in the root folder that sets a constant of its location:

这是如何工作的将php文件放在根文件夹中,该文件夹设置其位置的常量:

<?php
       define('ROOTPATH', dirname(__FILE__));
?>

When ever you want to use this just include the file, and use ROOTPATH:

当你想要使用它时只需包含文件,并使用ROOTPATH:

 <?php
       include 'TheFileName.php';
       echo ROOTPATH,'/public/asset/img/photo.jpg';
?>

On most php servers this is already set to $_SERVER['DOCUMENT_ROOT'], but on a local host with will need to use a node.

在大多数php服务器上,这已经设置为$ _SERVER ['DOCUMENT_ROOT'],但在本地主机上需要使用节点。

Please note this is sudo code, some things may have to be changed

请注意这是sudo代码,有些事情可能需要更改

#1


-1  

The issue is that the path is bellow the css doc, relative file paths work by adding there files location to the beginning of the url, this means that it is looking for (Mypath)/asset/img/photo.jpg

问题是路径低于css doc,相对文件路径通过将文件位置添加到url的开头来工作,这意味着它正在寻找(Mypath)/asset/img/photo.jpg

(or: public/asset/css/asset/img/photo.jpg from what I can see)

(或者:我可以看到的public / asset / css / asset / img / photo.jpg)

Without a PHP server to return dynamic url's, sadly your only 2 options are:

没有PHP服务器来返回动态网址,遗憾的是你只有两个选项:

  • Move the CSS to the root, or at least lower then the IMG
  • 将CSS移动到根目录,或者至少低于IMG
  • Use a absolute url
  • 使用绝对网址

With php you have a few options, my favorite being a "Node" structure

How this works is placing a php file in the root folder that sets a constant of its location:

这是如何工作的将php文件放在根文件夹中,该文件夹设置其位置的常量:

<?php
       define('ROOTPATH', dirname(__FILE__));
?>

When ever you want to use this just include the file, and use ROOTPATH:

当你想要使用它时只需包含文件,并使用ROOTPATH:

 <?php
       include 'TheFileName.php';
       echo ROOTPATH,'/public/asset/img/photo.jpg';
?>

On most php servers this is already set to $_SERVER['DOCUMENT_ROOT'], but on a local host with will need to use a node.

在大多数php服务器上,这已经设置为$ _SERVER ['DOCUMENT_ROOT'],但在本地主机上需要使用节点。

Please note this is sudo code, some things may have to be changed

请注意这是sudo代码,有些事情可能需要更改