我尝试使用CSS变量来生成动态路径。
示例:
int
使用未找到的模块:root {
--fonts-path: "/path/to/font";
}
@font-face {
font-family: "FontName";
src: url(var(--fonts-path) + "/FontName-Light.woff") format('woff');
font-weight: 100;
}
html {
font-family: 'Metric', Arial, sans-serif;
}
失败,这是webpack日志:
'var(--hpe-fonts-path'
任何指针?
答案 0 :(得分:1)
我发现您的方法存在一些问题:
@font-face
规则不会继承:root
上设置的CSS变量。
您不能使用+
来连接CSS字符串。请参阅string concatenation in css
并非所有实施都支持var()
内的url()
。
答案 1 :(得分:0)
此帖中描述了类似的问题:
CSS native variables not working in media queries
用户尝试在@ font-face规则中使用变量。
如接受的答案(https://stackoverflow.com/a/40723269/4879632)中所述,您不能在规则中使用变量,因为变量由以下内容继承:root(html)子元素。 @ Rules(字体,媒体查询等)不是元素,因此它们不从根继承。因此,您无法从那里引用变量。