在Sass的图像路径中有变量吗?

pro*_*365 111 css sass

我想要一个包含我的CSS文件中所有图像的根路径的变量.我无法弄清楚这在纯Sass中是否可行(实际的web项目不是RoR,因此不能使用asset_pipeline或任何那些花哨的爵士乐).

这是我的例子不起作用.在编译时,它会在后台url属性say("Invalid CSS after "..site/background": expected ")")中的变量的第一个实例处出现.

定义返回路径的函数:

//Vars
$assetPath : "/assets/images";

//Functions
@function get-path-to-assets($assetPath){
  @return $assetPath;
}
Run Code Online (Sandbox Code Playgroud)

使用功能:

body {
  margin: 0 auto;
  background: url($get-path-to-assets/site/background.jpg) repeat-x fixed 0 0;
  width: 100%; }
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Wes*_*rch 197

你试过Interpolation语法吗?

background: url(#{$get-path-to-assets}/site/background.jpg) repeat-x fixed 0 0;
Run Code Online (Sandbox Code Playgroud)

  • 它太美了我觉得我会哭 (17认同)
  • 也适用于引用的路径,例如在指南针字体混合中。`'#{$fontName}.ext', ..` (2认同)

ste*_*eax 88

不需要功能:

$assetPath : "/assets/images";

...

body {
  margin: 0 auto;
  background: url(#{$assetPath}/site/background.jpg) repeat-x fixed 0 0;
  width: 100%; }
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅插值文档.

  • 绝对更喜欢简洁直接指向变量 (5认同)

Ner*_*boy 6

正在寻找同一问题的答案,但我想我找到了一个更好的解决方案:http://blog.grayghostvisuals.com/compass/image-url/

基本上,您可以在config.rb中设置图像路径,并使用image-url()帮助程序