最适合网络开发者的网站

CSS 教程

CSS 主页 CSS 简介 CSS 语法 CSS 选择器 CSS 如何 CSS 注释 CSS 颜色 CSS 背景 CSS 边框 CSS 边距 CSS 填充 CSS 高度/宽度 CSS 盒子模型 CSS 大纲 CSS 文本 CSS 字体 CSS 图标 CSS 链接 CSS 列表 CSS 表格 CSS 显示 CSS 最大宽度 CSS 位置 CSS Z 索引 CSS 溢出 CSS 浮动 CSS 内联块 CSS 对齐 CSS 组合器 CSS 伪类 CSS 伪元素 CSS 不透明度 CSS 导航栏 CSS 下拉菜单 CSS 图片库 CSS 图像精灵 CSS Attr 选择器 CSS 表单 CSS 计数器 CSS 网站布局 CSS 单位 CSS 特殊性 CSS !重要 CSS 数学函数

CSS 高级

CSS 圆角 CSS 边框图像 CSS 背景 CSS 颜色 CSS 颜色关键字 CSS 渐变 CSS 阴影 CSS 文本效果 CSS Web 字体 CSS 2D 变换 CSS 3D 变换 CSS 过渡 CSS 动画 CSS 工具提示 CSS 样式图像 CSS 图像反射 CSS 对象适合 CSS 对象位置 CSS 遮罩 CSS 按钮 CSS 分页 CSS 多列 CSS 用户界面 CSS 变量 CSS 盒子尺寸 CSS 媒体查询 CSS MQ 示例 CSS 弹性框

CSS 响应式

RWD 简介 RWD 视口 RWD 网格视图 RWD 媒体查询 RWD 图像 RWD 视频 RWD 框架 RWD 模板

CSS 网格

网格简介 网格容器 网格项

CSS 南苏丹援助协会

SASS 教程

CSS 例子

CSS 模板 CSS 示例 CSS 测验 CSS 练习 CSS 证书

CSS 参考

CSS 参考 CSS 选择器 CSS 函数 CSS 参考 Aural CSS Web 安全字体 CSS 动画 CSS 单位 CSS PX-EM 转换器 CSS 颜色 CSS 颜色值 CSS 默认值 CSS 浏览器支持

初学者的 CSS 课程

尿素

CSS Web Fonts


The CSS @font-face Rule

Web fonts allow Web designers to use fonts that are not installed on the user's computer.

When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed.

Your "own" fonts are defined within the CSS @font-face rule.


Different Font Formats

TrueType Fonts (TTF)

TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems.

OpenType Fonts (OTF)

OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered trademark of Microsoft. OpenType fonts are used commonly today on the major computer platforms.

The Web Open Font Format (WOFF)

WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C Recommendation. WOFF is essentially OpenType or TrueType with compression and additional metadata. The goal is to support font distribution from a server to a client over a network with bandwidth constraints.

The Web Open Font Format (WOFF 2.0)

TrueType/OpenType font that provides better compression than WOFF 1.0.

SVG Fonts/Shapes

SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification define a font module that allows the creation of fonts within an SVG document. You can also apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents.

Embedded OpenType Fonts (EOT)

EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded fonts on web pages.


Browser Support for Font Formats

The numbers in the table specifies the first browser version that fully supports the font format.

Font format
TTF/OTF 9.0* 4.0 3.5 3.1 10.0
WOFF 9.0 5.0 3.6 5.1 11.1
WOFF2 14.0 36.0 39.0 10.0 26.0
SVG Not supported Not supported Not supported 3.2 Not supported
EOT 6.0 Not supported Not supported Not supported Not supported

*IE: The font format only works when set to be "installable".


Using The Font You Want

In the @font-face rule; first define a name for the font (e.g. myFirstFont) and then point to the font file.

Tip: Always use lowercase letters for the font URL. Uppercase letters can give unexpected results in IE.

To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property:

例子

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

div {
  font-family: myFirstFont;
}
亲自尝试 »

Using Bold Text

You must add another @font-face rule containing descriptors for bold text:

例子

@font-face {
  font-family: myFirstFont;
  src: url(sansation_bold.woff);
  font-weight: bold;
}
亲自尝试 »

The file "sansation_bold.woff" is another font file, that contains the bold characters for the Sansation font.

Browsers will use this whenever a piece of text with the font-family "myFirstFont" should render as bold.

This way you can have many @font-face rules for the same font.


通过练习测试自己

锻炼:

Add a web font with the name "sansation" and the URL "sansation_light.woff".

<style>
 {
  font-family: ;
  : ;
}

body {
  font-family: sansation;
}
</style>

<body>
  <h1>This is a heading</h1>
  <p>This is a paragraph</p>
  <p>This is a paragraph</p>
</body>


CSS Font Descriptors

The following table lists all the font descriptors that can be defined inside the @font-face rule:

Descriptor Values Description
font-family name Required. Defines a name for the font
src URL Required. Defines the URL of the font file
font-stretch normal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
Optional. Defines how the font should be stretched. Default is "normal"
font-style normal
italic
oblique
Optional. Defines how the font should be styled. Default is "normal"
font-weight normal
bold
100
200
300
400
500
600
700
800
900
Optional. Defines the boldness of the font. Default is "normal"
unicode-range unicode-range Optional. Defines the range of UNICODE characters the font supports. Default is "U+0-10FFFF"