Official way of adding custom fonts to Rails 4?
Just did it...
Download and save the font files (eot, woff, woff2...) in your assets/fonts/ directory
- In your config/application.rb add this line config.assets.paths << Rails.root.join("app", "assets", "fonts")
What this does is it precompiles your fonts folder just as it does by default with your images, stylesheets etc.
and make sure this line is set to true config.assets.enabled = true
In your sass/scss or even inline with
<script>
tag@font-face { font-family: 'Bariol Regular'; src: font-url('Bariol_Regular_Webfont/bariol_regular-webfont.eot'); src: font-url('Bariol_Regular_Webfont/bariol_regular-webfont.eot?iefix') format('eot'), font-url('Bariol_Regular_Webfont/bariol_regular-webfont.woff') format('woff'), font-url('Bariol_Regular_Webfont/bariol_regular-webfont.ttf') format('truetype'), font-url('Bariol_Regular_Webfont/bariol_regular-webfont.svg#webfont3AwWkQXK') format('svg'); font-weight: normal; font-style: normal; }
Note that you should use the font-url
helper instead of css' url
to address the fingerprinting done by Rails when it precompiles the assets
Finally, set the font-family in your CSS files
body {
font-family: 'Bariol Regular', serif;
}
FREE TIP: This being said, the best way in terms of performance is to set this up with JS so that these fonts get loaded asynchronously. Check this loader: https://github.com/typekit/webfontloader
Yes the link given will explain it well, however if u need another detailed explanation then here it is
Firstly to use custom fonts in your app you need to download font files, you can try https://www.1001freefonts.com/ and look for fonts
Few of the most popular font file formats are mainly .otf(Open Type Format) .ttf(True Type Format) .woff(Web Open Font Format)
You can move the downloaded fonts to your app folder under app/assets/fonts/
After downloading the file you need to "declare" the fonts you will be using in your css like this
@font-face { font-family: "Kaushan Script Regular"; src: url(/assets/KaushanScript-Regular.otf) format("truetype"); }
Finally you can use the font-family that you just declared wherever you want like this
#e-registration { font-family: "Kaushan Script Regular"; }
Hope this helps.