Bootstrap - Sass:相对glyphicons图标路径

Jos*_*chi 3 twitter-bootstrap bootstrap-sass

如何在bootstrap sass版本中设置相对glyphicons图标路径?

默认情况下,css font-face中使用的路径是绝对路径.

@font-face {
  font-family: 'Glyphicons Halflings';
  src: url(/fonts/bootstrap/glyphicons-halflings-regular.eot?1422262129);
  src: url(/fonts/bootstrap/glyphicons-halflings-regular.eot?&1422262129#iefix) format("embedded-opentype"), url(/fonts/bootstrap/glyphicons-halflings-regular.woff2?1422262129) format("woff2"), url(/fonts/bootstrap/glyphicons-halflings-regular.woff?1422262129) format("woff"), url(/fonts/bootstrap/glyphicons-halflings-regular.ttf?1422262129) format("truetype"), url(/fonts/bootstrap/glyphicons-halflings-regular.svg?1422262129#glyphicons_halflingsregular) format("svg");
}
Run Code Online (Sandbox Code Playgroud)

但我需要一个相对的:"../ fonts/bootstrap" - 所以在文件_bootstrap-variables.scss中,我设置了$ icon-font-path

$icon-font-path: "../fonts/bootstrap/"
Run Code Online (Sandbox Code Playgroud)

给出以下内容

@font-face {
  font-family: 'Glyphicons Halflings';
  src: url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.eot?1422262129);
  src: url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.eot?&1422262129#iefix) format("embedded-opentype"), url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.woff2?1422262129) format("woff2"), url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.woff?1422262129) format("woff"), url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.ttf?1422262129) format("truetype"), url(/fonts/../fonts/bootstrap/glyphicons-halflings-regular.svg?1422262129#glyphicons_halflingsregular) format("svg");
}
Run Code Online (Sandbox Code Playgroud)

在网络上,我找到了在变量之前包含"bootstrap-sprockets"的提示,结果是

@font-face {
  font-family: 'Glyphicons Halflings';
  src: url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.eot"));
  src: url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.eot?#iefix")) format("embedded-opentype"), url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.woff2")) format("woff2"), url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.woff")) format("woff"), url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.ttf")) format("truetype"), url(font-path("../fonts/bootstrap/glyphicons-halflings-regular.svg#glyphicons_halflingsregular")) format("svg");
}
Run Code Online (Sandbox Code Playgroud)

网址本身似乎没问题 - 但这个"字体路径"来自哪里,我该如何摆脱它呢?

或者是否有另一种指定相对路径的方法?我错了什么?

小智 9

在不使用罗盘的情况下解决此问题的一种简单方法是在包含引导程序之前声明字体路径功能.你的sass应该如下:

app.scss

// your bootstrap default overrides here

$icon-font-path: '../fonts/bootstrap/';

@function font-path($path) {
  @return $path;
}

@import 'bootstrap-sprockets';
@import 'bootstrap';

// your application styles here
Run Code Online (Sandbox Code Playgroud)

有点黑客,我不确定是否还有其他任何东西不能用这样的设置(不使用指南针),但它似乎对我来说是有用的.