I'm researching how to add custom fonts to my Rails app, e.g. by adding a fonts
folder in the assets folder - and I cannot find an "official" Rails way on how to do this.
Yes, there are plenty of questions/answers here on SO about the matter, all seemingly with their own hacks. But shouldn't such a common practice go under Rails' famous "convention over configuration"?
Or if I've missed it - where is the documentation reference on how to organize fonts in a Rails app?
Just did it...
Download and save the font files (eot, woff, woff2...) in your assets/fonts/ directory
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>
tagNote that you should use the
font-url
helper instead of css'url
to address the fingerprinting done by Rails when it precompiles the assetsFinally, set the font-family in your CSS files
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
I had some trouble with the approaches listed above, because the production css was not pointing to the compiled ttf font, so I then successfully used this alternative approach borrowed from https://gist.github.com/anotheruiguy/7379570:
Places all font files in
assets/stylesheets/fonts
. e.g.assets/stylesheets/fonts/digital_7.ttf
.I defined in a .scss file that we use:
I leveraged the custom font in other .scss files:
This said, a much cleaner way of doing this is to put the font definition (digital_7_mono.ttf in this example) on a separate site.
If you are using Cloudfront, for example, in a Cloudfront directory called
my_path
, upload your font files, then define a css file (e.g.digital_fonts.css
)In your html, inside the
<head>
tag, add: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 http://www.1001freefonts.com/
http://www.piccsy.com/everything-design/ 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
Finally you can use the font-family that you just declared wherever you want like this
Hope this helps.