I have a Rails 4 application and I am trying to use a custom font.
I have followed many tutorials on this and somehow it\'s just not working for my application.
<Your @font-face
declaration is very close, you are just missing the /assets
prefix within the url declaration.
@font-face {
font-family: 'HDVPeace';
src: url('/assets/HDV_Peace.eot');
src: url('/assets/HDV_Peace.eot?iefix') format('eot'),
url('/assets/HDV_Peace.woff') format('woff'),
url('/assets/HDV_Peace.ttf') format('truetype'),
url('/assets/HDV_Peace.svg#webfont') format('svg');
}
Anything that has been added to assets.paths
is available directly under the /assets
path in both development and production. You only need the asset path modification line within application.rb
, doing it again in development.rb
and production.rb
is just redundant.
Additionally, all of the font formats are essentially binary. There is no need to pre-compile them, so you can safely remove the assets.precompile
addition.
This worked for me on a Rails 4.1 app.
@font-face {
font-family: 'icomoon';
src: url(font-path('icomoon.eot') + "?#iefix") format('embedded-opentype'),
url(font-path('icomoon.woff')) format('woff'),
url(font-path('icomoon.ttf')) format('truetype'),
url(font-path('icomoon.svg') + "#icomoon") format('svg');
}
[class^="icon-"], [class*=" icon-"] {
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
Sometimes fonts are not detected from the assets/fonts
directory.
If security is not an issue, we can put the fonts
directory into the public folder. They will then be available at a public/
level.
Forget about changing anything in Rails 4 in your application.rb. Just add /assets/ prefix like @Parker Selbert said and the following will work:
@font-face {
font-family: 'custom-font-name';
src: font-url('/assets/_folder_/fontX-webfont.eot');
src: font-url('/assets/_folder_/fontX-webfont.eot?#iefix') format('embedded-opentype'),
font-url('/assets/_folder_/fontX-webfont.woff') format('woff'),
font-url('/assets/_folder_/fontX-webfont.ttf') format('truetype'),
font-url('/assets/_folder_/fontX-webfont.svg#sociconregular') format('svg');
font-weight: normal;
font-style: normal;
}
I found this article to solve all of my problems.
http://aokolish.me/blog/2011/12/24/at-font-face-with-the-asset-pipeline/
Please do not hardcode your font directory since the location is dynamic.
Just like there are built-in helpers for images there is are also built-in helpers for fonts: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-font_url
Example:
@font-face {
font-family: 'QuicksandOTF';
src: font_url('Quicksand-Regular.otf') format('opentype');
font-weight: normal;
font-style: normal;
}