Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Google Font in Rails App

Tags:

Anyone know how I can include a google fonts style sheet in my rails app? I have tried adding

<link href='https://fonts.googleapis.com/css?family=Raleway:500,900,200'  rel='stylesheet' type='text/css'> 

Into my application.html.erb but that doesn't do anything

like image 325
tommyd456 Avatar asked Aug 29 '13 10:08

tommyd456


People also ask

How do I add a font to Rails app?

Importing it into my Rails app was pretty easy. First, you create a folder called 'fonts' in your 'assets' folder. After that, you go shopping for fonts! After downloading one that catches your eye, simply drag it to your fonts folder.


2 Answers

For external style sheet add this to your layout (application.html.erb)

 stylesheet_link_tag 'application', 'put any external link' 

If you use
1) <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Font+Name"> in rails layout

2)

Style an element with the requested web font, either in a stylesheet:

CSS selector {   font-family: 'Font Name', serif; } 

or with an inline style on the element itself:

Your text

you got font family Tangerine

Example :--

<html>   <head>    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">    <style>      body {       font-family: 'Tangerine', serif;       font-size: 48px;      } </style>  </head> <body>   <div>Making the Web Beautiful!</div> </body> </html> 
like image 156
Rajarshi Das Avatar answered Oct 11 '22 21:10

Rajarshi Das


I was struggling with this one, because I had a line like this:

<%= stylesheet_link_tag 'application',  media: 'all', 'data-turbolinks-track' => true %> 

It's hard to tell where to put your link at a first sight.

That you wanted to do is to place your stylesheet link AFTER 'application', divide it by commas. Like this:

<%= stylesheet_link_tag 'application', 'https://fonts.googleapis.com/css?family=Open+Sans:400&subset=latin',  media: 'all', 'data-turbolinks-track' => true %> 

Then use it in your stylesheet as usual with font-family property.

Of course, we can @import it in a SASS, or do it as usual with a link tag, but that's just an another way and it's should be mentioned here.

like image 41
Alex Pogiba Avatar answered Oct 11 '22 22:10

Alex Pogiba