如何在资产文件夹中引用自定义字体文件?
How do I reference a custom font file in my asset folder?
With Rails 5、如何引用我上传的自定义字体文件?我已将文件放在
app/assets/fonts/chicagoflf-webfont.woff
然后在我的 CSS 文件中,我有
@font-face {
font-family: 'chicagoflfregular';
src: url('fonts/chicagoflf-webfont.woff2') format('woff2'),
url('fonts/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
但我没有看到字体正在加载,即使在重新启动我的服务器之后也是如此。我应该在 "url" 字段中使用的正确路径是什么?
我建议你使用 font_url
助手而不是 url
@font-face {
font-family: 'chicagoflfregular';
src: font_url('fonts/chicagoflf-webfont.woff2') format('woff2'),
font_url('fonts/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
AssetUrlHelper.html#method-i-font_url 来自官方文档
更新
您是否添加了字体文件夹 in config/application.rb
?
module YourApp
class Application < Rails::Application
...
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile += %w( .svg .eot .woff .ttf )
...
end
end
假设您正在使用 Sass,您可以使用 font-url
或 asset-url
来调用您的字体。考虑到您将自定义字体放在 app/assets/fonts
中,您应该可以直接调用字体 ,路径中没有前缀 ,就像这样
@font-face {
font-family: 'chicagoflfregular';
src: font-url('chicagoflf-webfont.woff2') format('woff2'),
font-url('chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
或
@font-face {
font-family: 'chicagoflfregular';
src: asset-url('chicagoflf-webfont.woff2') format('woff2'),
asset-url('chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
或
如果你不使用Sass,那么使用url
你应该能够调用前缀为assets
的字体而不是fonts
像这样
@font-face {
font-family: 'chicagoflfregular';
src: url('/assets/chicagoflf-webfont.woff2') format('woff2'),
url('/assets/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
如果您没有预编译您的资产,您的字体将不会出现。所以你需要预编译资产。例如对于生产环境do
RAILS_ENV=production bin/rails assets:precompile
并且不要忘记重新启动服务器
将字体路径添加到 assets
路径如下:
config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
# Fonts
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|woff2|ttf)\z/
然后,您可以将 css
文件更改为 css.erb
文件,然后您就可以使用 rails 辅助方法 font_path
或 asset_path
用于在 src
中指定字体 URL,如下所示:
custom.css.erb
@font-face {
font-family: 'chicagoflfregular';
src: url("<%= asset_path('chicagoflf-webfont.woff2') %>") format('woff2'),
url("<%= asset_path('chicagoflf-webfont.woff') %>") format('woff');
font-weight: normal;
font-style: normal;
}
请确保您已在 生产 环境中预编译资产。
With Rails 5、如何引用我上传的自定义字体文件?我已将文件放在
app/assets/fonts/chicagoflf-webfont.woff
然后在我的 CSS 文件中,我有
@font-face {
font-family: 'chicagoflfregular';
src: url('fonts/chicagoflf-webfont.woff2') format('woff2'),
url('fonts/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
但我没有看到字体正在加载,即使在重新启动我的服务器之后也是如此。我应该在 "url" 字段中使用的正确路径是什么?
我建议你使用 font_url
助手而不是 url
@font-face {
font-family: 'chicagoflfregular';
src: font_url('fonts/chicagoflf-webfont.woff2') format('woff2'),
font_url('fonts/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
AssetUrlHelper.html#method-i-font_url 来自官方文档
更新
您是否添加了字体文件夹 in config/application.rb
?
module YourApp
class Application < Rails::Application
...
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile += %w( .svg .eot .woff .ttf )
...
end
end
假设您正在使用 Sass,您可以使用 font-url
或 asset-url
来调用您的字体。考虑到您将自定义字体放在 app/assets/fonts
中,您应该可以直接调用字体 ,路径中没有前缀 ,就像这样
@font-face {
font-family: 'chicagoflfregular';
src: font-url('chicagoflf-webfont.woff2') format('woff2'),
font-url('chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
或
@font-face {
font-family: 'chicagoflfregular';
src: asset-url('chicagoflf-webfont.woff2') format('woff2'),
asset-url('chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
或
如果你不使用Sass,那么使用url
你应该能够调用前缀为assets
的字体而不是fonts
像这样
@font-face {
font-family: 'chicagoflfregular';
src: url('/assets/chicagoflf-webfont.woff2') format('woff2'),
url('/assets/chicagoflf-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
如果您没有预编译您的资产,您的字体将不会出现。所以你需要预编译资产。例如对于生产环境do
RAILS_ENV=production bin/rails assets:precompile
并且不要忘记重新启动服务器
将字体路径添加到 assets
路径如下:
config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
# Fonts
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|woff2|ttf)\z/
然后,您可以将 css
文件更改为 css.erb
文件,然后您就可以使用 rails 辅助方法 font_path
或 asset_path
用于在 src
中指定字体 URL,如下所示:
custom.css.erb
@font-face {
font-family: 'chicagoflfregular';
src: url("<%= asset_path('chicagoflf-webfont.woff2') %>") format('woff2'),
url("<%= asset_path('chicagoflf-webfont.woff') %>") format('woff');
font-weight: normal;
font-style: normal;
}
请确保您已在 生产 环境中预编译资产。