Get Started with the Google Fonts API
Get Started with the Google Fonts API
This guide explains how to use the Google Fonts API to add fonts to your web pages. You don't need to do any programming; all you have to do is add a special stylesheet link to your HTML document, then refer to the font in a CSS style.
A quick example
Here's an example. Copy and paste the following HTML into a file:
<html>
<head>
<link rel="stylesheet"
href="https://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>
Then open the file in a modern web browser. You should see a page displaying the following, in the font called Tangerine:
That sentence is ordinary text, so you can change how it looks by using CSS. Try adding a shadow to the style in the previous example:
body {
font-family: 'Tangerine', serif;
font-size: 48px;
text-shadow: 4px 4px 4px #aaa;
}
You should now see a drop shadow under the text:
And that's only the beginning of what you can do with the Fonts API and CSS.
Overview
You can start using the Google Fonts API in just two steps:
Add a stylesheet link to request the desired web font(s):
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Font+Name">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:
<div style="font-family: 'Font Name', serif;">Your text</div>
Note: When specifying a web font in a CSS style, always list at least one fallback web-safe font in order to avoid unexpected behaviors. In particular, add a CSS generic font name like serif
or sans-serif
to the end of the list, so the browser can fall back to its default fonts if need be.
For a list of fonts you can use, see Google Fonts.
Specifying font families and styles in a stylesheet URL
To determine what URL to use in your stylesheet link, start with the Google Fonts API base URL:
https://fonts.googleapis.com/css
Then, add the family=
URL parameter, with one or more font family names and styles.
For example, to request the Inconsolata font:
https://fonts.googleapis.com/css?family=Inconsolata
Note: Replace any spaces in the font family name with plus signs (+
).
To request multiple font families, separate the names with a pipe character (|
).
For example, to request the fonts Tangerine, Inconsolata, and Droid Sans:
https://fonts.googleapis.com/css?family=Tangerine|Inconsolata|Droid+Sans
Requesting multiple fonts allows you to use all of those fonts in your page. (But don't go overboard; most pages don't need very many fonts, and requesting a lot of fonts may make your pages slow to load.)
The Google Fonts API provides the regular version of the requested fonts by default. To request other styles or weights, append a colon (:
) to the name of the font, followed by a list of styles or weights separated by commas (,
).
For example:
https://fonts.googleapis.com/css?family=Tangerine:bold,bolditalic|Inconsolata:italic|Droid+Sans
To find out which styles and weights are available for a given font, see the font's listing in Google Fonts.
For each style you request, you can give either the full name or an abbreviation; for weights, you can alternatively specify a numerical weight:
Style | Specifiers |
---|---|
italic | italic or i |
bold | bold or b or a numerical weight such as 700 |
bold italic | bolditalic or bi |
For example, to request Cantarell italic and Droid Serif bold, you could use any of the following URLs:
https://fonts.googleapis.com/css?family=Cantarell:italic|Droid+Serif:bold
https://fonts.googleapis.com/css?family=Cantarell:i|Droid+Serif:b
https://fonts.googleapis.com/css?family=Cantarell:i|Droid+Serif:700
Specifying script subsets
Some of the fonts in the Google Font Directory support multiple scripts (like Latin, Cyrillic, and Greek for example). In order to specify which subsets should be downloaded the subset parameter should be appended to the URL.
For example, to request the Cyrillic subset of the Roboto Mono font, the URL would be:
https://fonts.googleapis.com/css?family=Roboto+Mono&subset=cyrillic
To request the Greek subset of the Roboto Mono font, the URL would be:
https://fonts.googleapis.com/css?family=Roboto+Mono&subset=greek
To request both Greek and Cyrillic subsets of the Roboto Mono font, the URL would be:
https://fonts.googleapis.com/css?family=Roboto+Mono&subset=greek,cyrillic
The Latin subset is always included if available and need not be specified. Please note that if a client browser supports unicode-range (http://caniuse.com/#feat=font-unicode-range) the subset parameter is ignored; the browser will select from the subsets supported by the font to get what it needs to render the text.
For a complete list of available fonts and font subsets please see Google Fonts.
Optimizing your font requests
Oftentimes, when you want to use a web font on your website or application, you know in advance which letters you'll need. This often occurs when you're using a web font in a logo or heading.
In these cases, you should consider specifying a text=
value in your font request URL. This allows Google to return a font file that's optimized for your request. In some cases, this can reduce the size of the font file by up to 90%.
To use this new beta feature, simply add text=
to your Google Fonts API requests. For example if you're only using Inconsolata for the title of your blog, you can put the title itself as the value of text=
. Here is what the request would look like:
https://fonts.googleapis.com/css?family=Inconsolata&text=Hello
As with all query strings, you should URL-encode the value:
https://fonts.googleapis.com/css?family=Inconsolata&text=Hello%20World
This feature also works for international fonts, allowing you to specify UTF-8 characters. For example, ¡Hola! is represented as:
https://fonts.googleapis.com/css?family=Inconsolata&text=%c2%a1Hola!
Note: there's no need to specify the subset=
parameter when using text=
as it allows you to refer to any character in the original font.
Enabling font effects (Beta)
When making headers or display texts on your website, you'll often want to stylize your text in a decorative way. To simplify your work, Google has provided a collection of font effects that you can use with minimal effort to produce beautiful display text. For example:
To use this new beta feature, simply add effect=
to your Google Fonts API request and add the corresponding class name to the HTML element(s) that you want to affect. In our example above, we used the shadow-multiple
font effect, so the request would look like:
https://fonts.googleapis.com/css?family=Rancho&effect=shadow-multiple
To use the effect, add the corresponding class name to your HTML element(s). The corresponding class name is always the effect name prefixed with font-effect-
, so the class name for shadow-multiple
would be font-effect-shadow-multiple
:
<div class="font-effect-shadow-multiple">This is a font effect!<div>
You can request multiple effects by separating the effect names with a pipe character (|
).
https://fonts.googleapis.com/css?family=Rancho&effect=shadow-multiple|3d-float
Here is a complete listing of all the font effects that we offer:
Effect | API Name | Class Name | Support |
---|---|---|---|
Anaglyph | anaglyph |
font-effect-anaglyph |
Chrome, Firefox, Opera, Safari |
Brick Sign | brick-sign |
font-effect-brick-sign |
Chrome, Safari |
Canvas Print | canvas-print |
font-effect-canvas-print |
Chrome, Safari |
Crackle | crackle |
font-effect-crackle |
Chrome, Safari |
Decaying | decaying |
font-effect-decaying |
Chrome, Safari |
Destruction | destruction |
font-effect-destruction |
Chrome, Safari |
Distressed | distressed |
font-effect-distressed |
Chrome, Safari |
Distressed Wood | distressed-wood |
font-effect-distressed-wood |
Chrome, Safari |
Emboss | emboss |
font-effect-emboss |
Chrome, Firefox, Opera, Safari |
Fire | fire |
font-effect-fire |
Chrome, Firefox, Opera, Safari |
Fire Animation | fire-animation |
font-effect-fire-animation |
Chrome, Firefox, Opera, Safari |
Fragile | fragile |
font-effect-fragile |
Chrome, Safari |
Grass | grass |
font-effect-grass |
Chrome, Safari |
Ice | ice |
font-effect-ice |
Chrome, Safari |
Mitosis | mitosis |
font-effect-mitosis |
Chrome, Safari |
Neon | neon |
font-effect-neon |
Chrome, Firefox, Opera, Safari |
Outline | outline |
font-effect-outline |
Chrome, Firefox, Opera, Safari |
Putting Green | putting-green |
font-effect-putting-green |
Chrome, Safari |
Scuffed Steel | scuffed-steel |
font-effect-scuffed-steel |
Chrome, Safari |
Shadow Multiple | shadow-multiple |
font-effect-shadow-multiple |
Chrome, Firefox, Opera, Safari |
Splintered | splintered |
font-effect-splintered |
Chrome, Safari |
Static | static |
font-effect-static |
Chrome, Safari |
Stonewash | stonewash |
font-effect-stonewash |
Chrome, Safari |
Three Dimensional | 3d |
font-effect-3d |
Chrome, Firefox, Opera, Safari |
Three Dimensional Float | 3d-float |
font-effect-3d-float |
Chrome, Firefox, Opera, Safari |
Vintage | vintage |
font-effect-vintage |
Chrome, Safari |
Wallpaper | wallpaper |
font-effect-wallpaper |
Chrome, Safari |
Note: Some of the font effects (e.g. 3d) do not scale particularly well, and tend to look best when used with larger font sizes. Additionally, you might want to style the fonts further, such as changing the color of the text to match your page.
There are many more ways to style your fonts, and many things are possible through CSS. We are simply providing a few ideas to get you started. For more ideas, try Google searching "css text effects" and browse through many of the ideas that are already on the web!
Further reading
- See a complete list of font families provided by the Google Fonts API on Google Fonts.
- Learn how to use the Web Font Loader to have more control over loading fonts.
- Learn more about how the Google Fonts API works on the Technical Considerations page.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated June 2, 2017.
http://jq22.com/cdn/
Get Started with the Google Fonts API的更多相关文章
- 使用 Google Fonts 为网页添加美观字体
前言 文字是网页中很重要的组成部分.为文字选择一个合适的字体,能够更好的展现一个网站的个性,表达所要传递的信息,同时吸引用户来产生兴趣. 说到字体,我们首先会想到 CSS 里面的 font,例如: & ...
- (转)Google Fonts 的介绍与使用
转载自“前端笔记” http://www.cnblogs.com/milly/archive/2013/05/10/google-fonts.html Google Fonts 是什么?(以下翻译为 ...
- 前端 CDNJS 库及 Google Fonts、Ajax 和 Gravatar 国内加速服务
由于某些众所周知的原因,好多开源的 JS 库采用的国外 CDN 托管方式在国内访问速度不如人意.所以我们特意制作了这个公益项目,托管了 CDNJS 的所有开源 JS 库以及反代了 Google Fon ...
- Google字体API使用简单示例
一.前面的话 Google总会做些造福大众的事情,例如提供了web在线字体的API,这玩意其实去年就有了,但是字体种类手指头+脚趾头就可以数出来.but 最近,貌似Google对字体API进行了升级, ...
- 使用Google Fonts注意事项
Google Fonts是一个字体嵌入服务库. 这包括免费和开源字体系列.用于浏览库的交互式 Web 目录以及用于通过 CSS 和 Android 使用字体的 API. Google 字体库中的流行字 ...
- 使用google 语言 api 来实现整个网站的翻译
---恢复内容开始--- 使用google 语言 api 来实现整个网站的翻译,这时我们可以利用免费的google api来做处理来实现多语言的功能. 放在 HTML 文件中 <div id=& ...
- bootstrap之google fonts
bootstrap之google fonts 在学习一个bootstrap时,看到了一行引用代码:@import url(http://fonts.googleapis.com/css?family= ...
- Google Map API Version3 :代码添加和删除marker标记
转自:http://blog.sina.com.cn/s/blog_4cdc44df0100u80h.html Google Map API Version3 教程:在地图 通过代添加和删除mark标 ...
- Google Map API V3开发(1)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
随机推荐
- 报错:bash: pip: command not found
$ wget https://bootstrap.pypa.io/get-pip.py$ python get-pip.py$ pip -V #查看pip版本
- excel两张表数据匹配数据(VLOOKUP)
最近项目中需要跨项目导入数据,现整理Excel的一个小技能,记录如下. 第一步:我们打开一个excel表,创建数据如下. 第二步:我们把光标定位在需要展示数据的单元格中,如下图所示. 第三步:我们可以 ...
- 最近IOS10.2.1 iphone6 无法通过appStore 来更新 下载任何APP。好烦啊。
今天打开爱思助手,查看事实日志,发现了原因: Mar :: iPhone6 appstored(StoreServices)[] <Notice>: SSSQLiteDatabase: C ...
- Cassandra的数据模型的理解
Cassandra属于NoSQL数据库,NoSQL和传统关系型数据库不同,NOSQL偏好数据冗余,因为NoSQL一般无法做表关联查询. (1) keySpace 基本上可以将Keyspa ...
- AI通过了艺术创作图灵测试,你根本分不出来作者是不是人
各位geek朋友们,今年不用再看画了:近年来最大的艺术成就已经发生了. 这项艺术成就的诞生地,不是北京.新加坡.柏林郊区颜料四溅的画室中,不是威尼斯双年展上.请记住它出现的地点:美国新泽西州新布朗斯维 ...
- IDEA下使用Maven的test命令乱码
IDEA下使用Maven的test命令乱码的时候,加上 -Dfile.encoding=GBK 就可以解决啦 如下图所示: 或者在Maven的pom.xml文件中增加: <propert ...
- Jenkins常用插件
Generic Webhook Trigger Plugin触发器webhook用户触发构建 Deploy to container Plugin部署到tomcat Gradle Plugin Gra ...
- C语言定义共享全局变量
好久没写C语言了,突然忘记怎么定义全局共享变量了,由于老项目的Code Base都是C的风格,其中又大量用了全局变量,只能跟着糊一坨shit上去了.没办法. 再共享全局变量的global_shared ...
- Matlab如何连接Oracle数据库及基本操作
语言和语法都是相通的,了解Oracle的连接和处理,对于其他的数据库也是类似的. 一 通过ODBC实现到oracle的连接 )控制面板->管理工具->ODBC数据源->在系统DSN中 ...
- iOS蓝色和黄色文件夹新建方式区别(区别之前)
关于这个问题,好像xcode9之前还是多少来着,创建方式是这样: 1.New Group创建的黄色文件夹是“假”文件夹,也就是说 show in finder 是看不到的 解释:这个文件夹并不是真正的 ...