阅读(2444)

CSS3 字体

最后一次修改 2017年08月03日

CSS3 字体


With CSS3, web designers are no longer forced to use only web-safe fonts


CSS3 @font-face 规则

在CSS3之前,web设计师必须使用已在用户计算机上安装好的字体。

通过CSS3,web设计师可以使用他们喜欢的任意字体。

当您找到或购买到希望使用的字体时,可将该字体文件存放到web服务器上,它会在需要时被自动下载到用户的计算机上。

您"自己的"的字体是在 CSS3 @font-face 规则中定义的。


浏览器支持

表格中的数字表示支持该属性的第一个浏览器版本号。

属性          
@font-face 4.0 9.0 3.5 3.2 10.0

Internet Explorer 9+, Firefox, Chrome, Safari, 和 Opera 支持 WOFF (Web Open Font Format) 字体.

Firefox, Chrome, Safari, 和 Opera 支持 .ttf(True Type字体)和.otf(OpenType)字体字体类型)。

Chrome, Safari 和 Opera 也支持 SVG 字体/折叠.

Internet Explorer 同样支持 EOT (Embedded OpenType) 字体.

注意: Internet Explorer 8 以及更早的版本不支持新的 @font-face 规则。


使用您需要的字体

在新的 @font-face 规则中,您必须首先定义字体的名称(比如 myFirstFont),然后指向该字体文件。

lamp  提示:URL请使用小写字母的字体,大写字母在IE中会产生意外的结果

如需为 HTML 元素使用字体,请通过 font-family 属性来引用字体的名称 (myFirstFont):

OperaSafariChromeFirefoxInternet Explorer

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style> 
@font-face
{
	font-family: myFirstFont;
	src: url('Sansation_Light.ttf')
		,url('Sansation_Light.eot'); /* IE9 */
}

div
{
	font-family:myFirstFont;
}
</style>
</head>
<body>

<p><b>Note:</b> Internet Explorer 9 only supports .eot fonts.</p>

<div>
With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.
</div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


使用粗体文本

您必须添加另一个包含粗体文字的@font-face规则:

OperaSafariChromeFirefoxInternet Explorer

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style> 
@font-face
{
	font-family: myFirstFont;
	src: url(sansation_light.woff);
}

@font-face
{
	font-family: myFirstFont;
	src: url(sansation_bold.woff);
	font-weight:bold;
}

div
{
	font-family:myFirstFont;
}
</style>
</head>
<body>

<div>
With CSS3, websites can <b>finally</b> use fonts other than the pre-selected "web-safe" fonts.
</div>

<p><b>Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule.</p>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

该文件"Sansation_Bold.ttf"是另一种字体文件,包含Sansation字体的粗体字。

浏览器使用这一文本的字体系列"myFirstFont"时应该呈现为粗体。

这样你就可以有许多相同的字体@font-face的规则。


CSS3 字体描述

下表列出了所有的字体描述和里面的@font-face规则定义:

描述符 描述
font-family name 必需。规定字体的名称。
src URL 必需。定义字体文件的 URL。
font-stretch
  • normal

  • condensed

  • ultra-condensed

  • extra-condensed

  • semi-condensed

  • expanded

  • semi-expanded

  • extra-expanded

  • ultra-expanded

可选。定义如何拉伸字体。默认是 "normal"。
font-style
  • normal

  • italic

  • oblique

可选。定义字体的样式。默认是 "normal"。
font-weight
  • normal

  • bold

  • 100

  • 200

  • 300

  • 400

  • 500

  • 600

  • 700

  • 800

  • 900

可选。定义字体的粗细。默认是 "normal"。
unicode-range unicode-range 可选。定义字体支持的 UNICODE 字符范围。默认是 "U+0-10FFFF"。


CSS3 字体


With CSS3, web designers are no longer forced to use only web-safe fonts


CSS3 @font-face 规则

在CSS3之前,web设计师必须使用已在用户计算机上安装好的字体。

通过CSS3,web设计师可以使用他们喜欢的任意字体。

当您找到或购买到希望使用的字体时,可将该字体文件存放到web服务器上,它会在需要时被自动下载到用户的计算机上。

您"自己的"的字体是在 CSS3 @font-face 规则中定义的。


浏览器支持

表格中的数字表示支持该属性的第一个浏览器版本号。

属性          
@font-face 4.0 9.0 3.5 3.2 10.0

Internet Explorer 9+, Firefox, Chrome, Safari, 和 Opera 支持 WOFF (Web Open Font Format) 字体.

Firefox, Chrome, Safari, 和 Opera 支持 .ttf(True Type字体)和.otf(OpenType)字体字体类型)。

Chrome, Safari 和 Opera 也支持 SVG 字体/折叠.

Internet Explorer 同样支持 EOT (Embedded OpenType) 字体.

注意: Internet Explorer 8 以及更早的版本不支持新的 @font-face 规则。


使用您需要的字体

在新的 @font-face 规则中,您必须首先定义字体的名称(比如 myFirstFont),然后指向该字体文件。

lamp  提示:URL请使用小写字母的字体,大写字母在IE中会产生意外的结果

如需为 HTML 元素使用字体,请通过 font-family 属性来引用字体的名称 (myFirstFont):

OperaSafariChromeFirefoxInternet Explorer

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style> 
@font-face
{
	font-family: myFirstFont;
	src: url('Sansation_Light.ttf')
		,url('Sansation_Light.eot'); /* IE9 */
}

div
{
	font-family:myFirstFont;
}
</style>
</head>
<body>

<p><b>Note:</b> Internet Explorer 9 only supports .eot fonts.</p>

<div>
With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.
</div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


使用粗体文本

您必须添加另一个包含粗体文字的@font-face规则:

OperaSafariChromeFirefoxInternet Explorer

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>html教程(html.cn)</title> 
<style> 
@font-face
{
	font-family: myFirstFont;
	src: url(sansation_light.woff);
}

@font-face
{
	font-family: myFirstFont;
	src: url(sansation_bold.woff);
	font-weight:bold;
}

div
{
	font-family:myFirstFont;
}
</style>
</head>
<body>

<div>
With CSS3, websites can <b>finally</b> use fonts other than the pre-selected "web-safe" fonts.
</div>

<p><b>Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule.</p>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

该文件"Sansation_Bold.ttf"是另一种字体文件,包含Sansation字体的粗体字。

浏览器使用这一文本的字体系列"myFirstFont"时应该呈现为粗体。

这样你就可以有许多相同的字体@font-face的规则。


CSS3 字体描述

下表列出了所有的字体描述和里面的@font-face规则定义:

描述符 描述
font-family name 必需。规定字体的名称。
src URL 必需。定义字体文件的 URL。
font-stretch
  • normal

  • condensed

  • ultra-condensed

  • extra-condensed

  • semi-condensed

  • expanded

  • semi-expanded

  • extra-expanded

  • ultra-expanded

可选。定义如何拉伸字体。默认是 "normal"。
font-style
  • normal

  • italic

  • oblique

可选。定义字体的样式。默认是 "normal"。
font-weight
  • normal

  • bold

  • 100

  • 200

  • 300

  • 400

  • 500

  • 600

  • 700

  • 800

  • 900

可选。定义字体的粗细。默认是 "normal"。
unicode-range unicode-range 可选。定义字体支持的 UNICODE 字符范围。默认是 "U+0-10FFFF"。