I want to use Google\'s Roboto font on my website and I am following this tutorial:
http://www.maketecheasier.com/use-google-roboto-font-everywhere/2012/03/15
<Try this
<style>
@font-face {
font-family: Roboto Bold Condensed;
src: url(fonts/Roboto_Condensed/RobotoCondensed-Bold.ttf);
}
@font-face {
font-family:Roboto Condensed;
src: url(fonts/Roboto_Condensed/RobotoCondensed-Regular.tff);
}
div1{
font-family:Roboto Bold Condensed;
}
div2{
font-family:Roboto Condensed;
}
</style>
<div id='div1' >This is Sample text</div>
<div id='div2' >This is Sample text</div>
This is what I did to get the woff2 files I wanted for static deployment without having to use a CDN
TEMPORARILY add the cdn for the css to load the roboto fonts into index.html and let the page load. from google dev tools look at sources and expand the fonts.googleapis.com node and view the content of the css?family=Roboto:300,400,500&display=swap file and copy the content. Put this content in a css file in your assets directory.
In the css file, remove all the greek, cryllic and vietnamese stuff.
Look at the lines in this css file that are similar to:
src: local('Roboto Light'), local('Roboto-Light'), url(https://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmSU5fBBc4.woff2) format('woff2');
copy the link address and paste it in your browser, it will download the font. Put this font into your assets folder and rename it here, as well as in the css file. Do this to the other links, I had 6 unique woff2 files.
I followed the same steps for material icons.
Now go back and comment the line where you call the cdn and instead use use the new css file you created.
Spent an hour, fixing the font issues.
Related answer - Below is for React.js
website:
Install the npm module:
npm install --save typeface-roboto-mono
import in .js file you want to use
one of the below:
import "typeface-roboto-mono";
// if import is supported
require('typeface-roboto-mono')
// if import is not supported
For the element you can use
one of the below:
fontFamily: "Roboto Mono, Menlo, Monaco, Courier, monospace", // material-ui
font-family: Roboto Mono, Menlo, Monaco, Courier, monospace; /* css */
style="font-family:Roboto Mono, Menlo, Monaco, Courier, monospace;font-weight:300;" /*inline css*/
Hope that helps.
You don't really need to do any of this.
Roboto
in the search box at the top rightThe page will give you a <link>
element to include in your pages, and a list of sample font-family
rules to use in your CSS.
Using Google's fonts this way guarantees availability, and reduces bandwidth to your own server.
The src
refers directly to the font files, therefore if you place all of them on /media/fonts/roboto
you should refer to them in your main.css like this:
src: url('../fonts/roboto/Roboto-ThinItalic-webfont.eot');
The ..
goes one folder up, which means you're referring to the media
folder if the main.css is in the /media/css
folder.
You have to use ../fonts/roboto/
in all url references in the CSS (and be sure that the files are in this folder and not in subdirectories, such as roboto_black_macroman
).
Basically (answering to your questions):
I have css in my media/css/main.css url. So where do i need to put that folder
You can leave it there, but be sure to use src: url('../fonts/roboto/
Do i need to extract all eot,svg etc from all sub folder and put in fonts folder
If you want to refer to those files directly (without placing the subdirectories in your CSS code), then yes.
Do i need to create css file fonts.css and include in my base template file
Not necessarily, you can just include that code in your main.css. But it's a good practice to separate fonts from your customized CSS.
Here's an example of a fonts LESS/CSS file I use:
@ttf: format('truetype');
@font-face {
font-family: 'msb';
src: url('../font/msb.ttf') @ttf;
}
.msb {font-family: 'msb';}
@font-face {
font-family: 'Roboto';
src: url('../font/Roboto-Regular.ttf') @ttf;
}
.rb {font-family: 'Roboto';}
@font-face {
font-family: 'Roboto Black';
src: url('../font/Roboto-Black.ttf') @ttf;
}
.rbB {font-family: 'Roboto Black';}
@font-face {
font-family: 'Roboto Light';
src: url('../font/Roboto-Light.ttf') @ttf;
}
.rbL {font-family: 'Roboto Light';}
(In this example I'm only using the ttf)
Then I use @import "fonts";
in my main.less file (less is a CSS preprocessor, it makes things like this a little bit easier)
For Website you can use 'Roboto' font as below:
If you have created separate css file then put below line at the top of css file as:
@import url('https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i,900,900i');
Or if you don't want to create separate file then add above line in between <style>...</style>
:
<style>
@import url('https://fonts.googleapis.com/css?
family=Roboto:300,300i,400,400i,500,500i,700,700i,900,900i');
</style>
then:
html, body {
font-family: 'Roboto', sans-serif;
}