search
HomeWeb Front-endHTML TutorialA first look at Google Font [Translated from Google official documentation]_html/css_WEB-ITnose

这个指南解释了如何使用Google Font的API,把网络字体添加到自己的页面上。你不需要任何的编码,你所要做的只是添加一个特定的CSS到HTML页面上,然后把字体关联到这个CSS样式。

一个快速的例子: 

这是一个例子,复制下面的HTML代码到一个文件中:

Html代码  

  1.   
  2.     
  3.       
  4.     
  5.       body {  
  6.         font-family: 'Tangerine', serif;  
  7.         font-size: 48px;  
  8.       }  
  9.       
  10.     
  11.     
  12.     

    Making the Web Beautiful!

      
  13.     
  14.   

 

用一个现代浏览器打开这个HTML文件,你就可以看到页面显示如下,用了一个叫做“Tangerine”的字体:

  
 
那个句子是普通的文本,所以你可以通过使用CSS来改变它的样式。试着在前面的例子中添加一个阴影的样式:

Html代码  

  1. body {  
  2.   font-family: 'Tangerine', serif;  
  3.   font-size: 48px;  
  4.   text-shadow: 4px 4px 4px #aaa;  
  5. }  

 

你可以看到,文字下面有阴影了:


 这只是你能用字体API和CSS做东西的一个开始。

概述: 
您开始使用Google字体API只需要两个步骤:
1.添加一个样式表来请求网络字体

Html代码  

  1.   

2.同样,在一个样式表中定义一个使用所请求的网络字体的节点

Html代码  

  1. CSS selector {  
  2.   font-family: 'Font Name', serif;  
  3. }  

 

或者在这个节点上使用内嵌的样式

Html代码  

  1. Your text

Note: When defining a web font in a CSS stylesheet, always list at least one web-safe fallback font (fallback web- safe font ) is used to avoid undesirable behavior. Specifically, add a CSS default font at the end of the list, like one named "serif" or "sans-serif". This way the browser can fall back to its default font if necessary.


For a list of web fonts you can use, see the Google Font Catalog .


Specify the font set and style in the stylesheet's URL:

To decide what URL to use in your stylesheet connection, you need to download the URL from Google The base URL of the font API starts:

Html code

  1. http://fonts.googleapis.com/css

Then add the URL parameters of the font set, and you can use the name and style of one or more font sets.
For example, to request the Inconsolata font:

Html code

  1. http://fonts.googleapis.com/css?family=Inconsolata

Note: Replace all spaces in the font set name with plus signs ( ).

When requesting multiple font sets, use vertical bars (|) to separate the names.
For example, to request Tangerine, Inconsolata, and Droid Sans fonts:

Html code

  1. http://fonts. googleapis.com/css?family=Tangerine|Inconsolata|Droid Sans


Requesting multiple fonts will allow you to use all of them on your page. (But don't get too outrageous, most pages don't need a very large number of fonts, and requesting many fonts will make your page load slower.)
The Font API provides a plain version of the requested font by default. To request another style or size, add a colon (:) after the font name, followed by a list of styles and sizes separated by commas (,).
For example:

Html code

  1. http://fonts.googleapis.com/css?family=Tangerine:bold,bolditalic |Inconsolata:italic|Droid Sans

To know what sizes and styles of fonts are provided, you need to check the Google Font Directory.
For each style you request, you can provide either a full name or an abbreviation, and for the size, you can specify an additional number:

样式 符号
斜体 italic 或 i
粗体 bold 或 b 或者是一个数字,就像700
粗体 斜体 bolditalic 或 bi


For example, to request "Cantarell" italic and "Droid Serif" bold, you can use any of the following URLs:

Html code

  1. http://fonts.googleapis.com/css?family=Cantarell:italic|Droid Serif:bold

Html code

  1. http://fonts.googleapis.com/css?family=Cantarell:i|Droid Serif:b

Html code

  1. http://fonts.googleapis.com/css?family=Cantarell:i|Droid Serif:700

Specify a subset of scripts:

Some fonts support multiple scripts (like Latin and Cyrillic) in the Google Fonts directory, in order to specify Which subset will be downloaded requires adding the subset parameter after the URL.

For example, to specify the Cyrillic subset for the Philosopher font, the URL should be written as:

Html code

  1. http:/ /fonts.googleapis.com/css?family=Philosopher&subset=cyrillic

To request Latin and Cyrillic subsets for the Philosophe font, the URL should be written:

Html code

  1. http://fonts.googleapis.com/css?family=Philosopher&subset=latin,cyrillic

To get For a complete list of fonts and font subsets, please refer to the Google Fonts Catalog .

The above is translated from Google official documents, original text link: http://code.google.com/intl/zh-CN/apis/webfonts/docs/ getting_started.html

Although the official link is zh-CN, it is still in English when I open it. I translated it myself and the translation was very poor. . . . .

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),