如何在Grails中引用CSS中的图像资源?

Sim*_*mon 12 css grails resources

我想在我的主样式表中引用Grails应用程序中的图像,但我无法使其工作.我的图片位于我的Grails应用程序的标准位置...

project\web-app\images\outbound-blue.png
Run Code Online (Sandbox Code Playgroud)

在我的样式表中,我想将它用作类的背景图像......

.messageimg {
    height:17px;
    width:16px;
    background-image:url(images/outbound-blue.png);
    background-repeat:no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,这不起作用.我的样式表也在正常位置,即

project\web-app\css\main.css
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中加载页面时,我得到一个丢失的图像标记.我已经检查过我在名字等方面没有拼写错误.我也尝试在网址中摆弄虚拟路径,但是我无法弄清楚我需要在Grails中使用什么才能完成这项工作.

我不想使用GSP并在我的代码中插入IMG标记,因为我想通过样式控制图像.

那么,我做错了什么?

Gen*_*sky 19

指定图像位置的更便携方式是使用resource()函数:

.messageimg {
    height:17px;
    width:16px;
    background-image:url('${resource(dir: "images", file: "outbound-blue.png")}');
    background-repeat:no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

  • 这将在`.css`文件中工作,还是仅在视图/`.gsp`页面中使用内联样式时? (5认同)
  • 它只能在`.gsp`文件中运行,而不是`.css`文件@aroth (4认同)

Jar*_*red 12

尝试在URI的开头添加"../".例如:

../images/outbound-blue.png
Run Code Online (Sandbox Code Playgroud)

URI开头的"../"告诉浏览器上一级到父目录然后查看images目录.目前,您已将其设置为查找images包含样式表的目录中调用的子目录.