26

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

6
  • add provided link between head in application.html.erb and in CSS font-family: 'Font Name', cursive; Commented Aug 29, 2013 at 10:57
  • Probably your font CSS is font-family: 'Raleway', sans-serif; Commented Aug 29, 2013 at 11:00
  • sorry - don't get what you mean! Commented Aug 29, 2013 at 11:03
  • just to clarify - I know how to use CSS and Google Fonts normally but it's the fact that Im using Rails which is the problem Commented Aug 29, 2013 at 11:04
  • 1
    finally solved the issue and it was of course my own stupidity. It was the order of the css files that was causing the problem so over-rode the order by editing the application.css file. Commented Aug 29, 2013 at 11:40

7 Answers 7

17

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You answer should be the accepted solution because this is the proper way to do it imo.
16

Simply add the code provided by google to the top of your application.scss file.

@import url('https://fonts.googleapis.com/css?family=Quicksand|Rubik');

please note rails will provide you with application.css file out of the box with require statements. remove these and rename application.scss

remember to add @import filename; for any other custome scssfiles in the folder

1 Comment

I think this is the best way to do it, the fonts don't belong in application.html.erb. Better yet you can just make a fonts.scss, importing all the fonts you need and then include it in application.scss so you have yourself a little font manifest
3

If you add the font via @import as shown in other comments, under @import Bootstrap in application.css, the asset pipeline compiles it into the application stylesheet. If you put it in the head of your html as a link, it gets added as a separate stylesheet which slows down the page loads. You can add the @import code just as Google provides from the fonts you choose on the Google font site.

Comments

2

application.html.erb

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<%= stylesheet_link_tag    "application", :media => "all" %>
 <%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>

 <!-- Google font -->
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>

</head>
<body>
 ............

application.css

body {
 font-family: 'Ubuntu', sans-serif;
}

I use it this way, maybe this will clarify.

UPDATED

Try to remove your scaffold.css.scss! Or just open and comment out all body part of CSS of this file.

2 Comments

Hmmm - this doesn't work for me for some reason. I have viewed source and the ref is there but no effect!
application.css is a manifest file and should not include styling.
2

I believe the most integrated way is to edit app/assets/views/application.html.erb and change:

<%= stylesheet_link_tag 'application.css' %>

to

<%= stylesheet_link_tag 'https://fonts.googleapis.com/css?family=Catamaran','application.css' %>

Comments

1

stick this inside the head section of application.html.erb

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

stick this in your css file:

body{
  font-family: "Raleway", Arial, sans-serif;
}

1 Comment

This works, but doesn't seem very rails..? How could I re-factor this?
0

Are you sure you are putting <link href='https://fonts.googleapis.com/css?family=Raleway:500,900,200' rel='stylesheet' type='text/css'> this to your desire layout?

And in your respective <layout>.css stylesheet, did you putted font-family:Raleway;?

Make it sure. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.