Downloading a Google font and setting up an offline site that uses it
CssFontsOfflineGoogle FontsCss Problem Overview
I have a template and it has a reference to a Google font like this:
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300' rel='stylesheet' type='text/css'>
How can I download it and set it up to use in my pages which are running offline all the time?
Css Solutions
Solution 1 - Css
google webfonts helper
Check outIt lets you download every web font of Google and suggests css code for the implementation. This tool also allows you to simply download all formats at once without the hassle.
> Ever wanted to know where Google hosts their webfonts? This service might be handy if you want to download all .eot, .woff, .woff2, .svg, .ttf files of a font variant directly from google (normally your User-Agent would determine the best format).
Also take a look at their Github page.
Solution 2 - Css
Just go to Google Fonts - http://www.google.com/fonts/ , add the font you like to your collection, and press the download button. And then just use the @fontface to connect this font to your web page. Btw, if you open the link you are using, you'll see an example of using @fontface
http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300
For an example
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
src: local('Open Sans Light'), local('OpenSans-Light'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/DXI1ORHCpsQm3Vp6mXoaTaRDOzjiPcYnFooOUGCOsRk.woff) format('woff');
}
Just change the url address to the local link on the font file, you've downloaded.
You can do it even easier.
Just download the file, you've linked:
http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300
Name it opensans.css or so.
Then just change the links in url() to your path to font files.
And then replace your example string with:
<link href='opensans.css' rel='stylesheet' type='text/css'>
Solution 3 - Css
Found a step-by-step way to achieve this (for 1 font):
(as of Sep-9 2013)
> 1. Choose your font at http://www.google.com/fonts
> 2. Add the desired one to your collection using "Add to collection" blue button
> 3. Click the "See all styles" button near "Remove from collection" button and make sure that you have selected other styles you may also need such as 'bold'...
> 4. Click the 'Use' tab button on bottom right of the page
> 5. Click the download button on top with a down arrow image
> 6. Click on "zip file" on the the popup message that appears
> 7. Click "Close" button on the popup
> 6. Slowly scroll the page until you see the 3 tabs "Standrd|@import|Javascript"
> 7. Click "@import" tab
> 8. Select and copy the url between 'url('
and ')'
> 9. Copy it on address bar in a new tab and go there
> 10. Do "File > Save page as..." and name it "desiredfontname.css" (replace accordingly)
> 11. Decompress the fonts .zip file you downloaded (.ttf should be extracted)
> 12. Go to "http://ttf2woff.com/" and convert any .ttf extracted from zip to .woff
> 13. Edit desiredfontname.css
and replace any url within it [between 'url('
and ')'
] with the corresponding converted .woff file you got on ttf2woff.com; path you write should be according to your server doc_root
> 14. Save the file and move it at its final place and write the corresponding <link/>
CSS tag to import these in your HTML page
> 15. From now, refer to this font by its font-family
name in your styles
That's it. Cause I had the same problem and the solution on top did not work for me.
Solution 4 - Css
The other answers are not wrong, but I found this to be the fastest way.
- Download the zip file from Google fonts and unzip it.
- Upload the font files 3 at a time to http://www.fontsquirrel.com/tools/webfont-generator
- Download the results.
Results contain all font formats: woff, svg, ttf, eot.
AND as an added bonus they generate the css file for you too!
Solution 5 - Css
If you'd like to explicitly declare your package dependencies or automate the download, you can add a npm package to pull in google fonts and serve locally.
Fontsource
Fontsource allows you to:
> Self-host Open Source fonts in neatly bundled NPM packages.
You can search npm for scope:fontsource <typefacename>
to browse the available fonts like @fontsource/roboto or @fontsource/open-sans and install like this:
$ npm install @fontsource/roboto --save
$ npm install @fontsource/open-sans --save
Font Downloaders
For a more generic use case, there are several npm packages that will deliver fonts in two steps, first by installing the library, and then by pointing it to the font name and options you'd like to include.
Further Reading:
Solution 6 - Css
3 steps:
- Download your custom font on Goole Fonts and down load .css file ex: Download http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300 and save as example.css
- Open file you download (example.css). Now you must download all font-face file and save them on fonts directory.
- Edit example.css: replace all font-face file to your .css download
Ex:
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 400;
src: local('Open Sans Italic'), local('OpenSans-Italic'), url(http://fonts.gstatic.com/s/opensans/v14/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2) format('woff2');
unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
}
Look at src: -> url. Download http://fonts.gstatic.com/s/opensans/v14/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2 and save to fonts directory. After that change url to all your downloaded file. Now it will be look like
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 400;
src: local('Open Sans Italic'), local('OpenSans-Italic'), url(fonts/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2) format('woff2');
unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
}
** Download all fonts contain .css file Hope it will help u
Solution 7 - Css
Essentially you are including the font into your project.
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: normal;
src: url('path/to/OpenSans.eot');
src: local('Open Sans'), local('OpenSans'), url('path/to/OpenSans.ttf') format('truetype');
Solution 8 - Css
When using Google Fonts, your workflow is divided in 3 steps : "Select", "Customize", "Embed". If you look closely, at the right end of the "Use" page, there is a little arrow which allows you to download the font currently in your collection.
https://i.stack.imgur.com/TGU4k.png" width="608" />
After that, and once the font is installed on your system, you just have to use it like any other regular font using the font-family
CSS directive.
Solution 9 - Css
1)Go at google fonts
2)select your font and it's a style
3)then you will get a link tag, side of link click on import
-
copy its link present inside the URL(' here ') and paste it on the browser search bar.
-
And then you will get something like this
-
and then download it and save it where your project files are there!
7)and then include it in your HTML link tag or import it in your CSS file (https://i.stack.imgur.com/NvJ60.jpg) (https://i.stack.imgur.com/XwK0l.jpg)
8)and then use ton family mentioned in google fonts
Solution 10 - Css
I followed duydb's answer to produce the python code below that automates this process.
import requests
with open("example.css", "r") as f:
text = f.read()
urls = re.findall(r'(https?://[^\)]+)', text)
for url in urls:
filename = url.split("/")[-1]
r = requests.get(url)
with open("./fonts/" + filename, "wb") as f:
f.write(r.content)
text = text.replace(url, "/fonts/" + filename)
with open("example.css", "w") as f:
f.write(text)
Hope this helps with some of the copy-paste death.
Solution 11 - Css
You need to download the font and reference it locally.
Download the CSS
from the link you posted, then download all of the WOFF
files and (if needed) convert them to TTF
.
Then change the CSS
from the link you posted to include the fonts locally.
From
url(http://themes.googleusercontent.com/static/fonts/opensans/v6/
DXI1ORHCpsQm3Vp6mXoaTXhCUOGz7vYGh680lGh-uXM.woff)
To
url(/path/to/font/font.woff)
Voila! There might be some more you need to do but the above is the basics. This article explains a little better.
Solution 12 - Css
just download the font and extract it in a folder. then link that font. the below code worked for me properly.
body {
color: #000;
font-family:'Open Sans';
src:url(../../font/Open_Sans/OpenSans-Light.ttf);
}