查看“︁HTML/Font”︁的源代码
来自康健生活
←
HTML/Font
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == 字体是字形的集合,且每个字形是一个描述了一个字母亦或符号的矢量图。字体文件的大小由两个因素决定: * 字形矢量路径的复杂程度。 * 字体所包含的字形数量。 暂时没有一种字体可以同时兼容新旧浏览器。 === 字体列表 === {| class="wikitable" |- | [[/MacOSX-FontList|Mac OS X 字体列表]] | [[/Windows-FontList|Windows 字体列表]] | [[/OpenSource-FontList|开源字体列表]] |} === 奇数字体和偶数字体 === 在传统的互联网网页中,奇数字体泛指 11、13、15 等,偶数则泛指 12、14、16 等。 ==== 等比关系 ==== 如果采用 css 预编译,比如 scss 等,在处理边距等问题上乘除结果一般为整数,奇数字体会小数居多,如 13*0.5 等。 == 浏览器 == <!-- ie6 会把奇数字体强制渲染为偶数,如 13px 等于 14px, -->现在 ie8 以下基本已经绝迹,所以不算问题。 另一个问题是平分字体,也就是说奇数字体的中线不在字体中间 === 字体间距 === === 字体像素宽度 === == 字族 == 一个字族需要满足指定字符集([https://unicode-table.com/en/ Unicode Table]) == 字体格式 == === 格式示例 === <blockquote> <syntaxhighlight lang="css"> @font-face { font-family: <font-name>; src: <local/url> <format>,[…]; font-weight: <font-weight>; font-style: <font-style>; font-stretch: <font-stretch>; unicode-range:<unicode-range>; } </syntaxhighlight></blockquote> 示例如下: <syntaxhighlight lang="css"> @font-face { font-family: 'Awesome Font'; font-style: normal; font-weight: 400; src: local('Awesome Font'), url('/fonts/awesome.woff2') format('woff2'), url('/fonts/awesome.woff') format('woff'), url('/fonts/awesome.ttf') format('ttf'), url('/fonts/awesome.eot') format('eot'); } </syntaxhighlight> === [http://www.w3.org/TR/css3-fonts/#descdef-unicode-range Unicode-range] === 可以将一个大的字体设置成小的子集(例如拉丁,希腊和斯拉夫字母等),并且只是加载这个子集来渲染页面上的字体。使用unicide range 子集并将文件分成字体不同样式的变体可以迅速和高效的下载用户“只需要”下载的子集和变体的字体。 指定一个逗号隔开的数据集合, 数据可以是下面三种形式中的任何一种: * Single codepoint (e.g. U+416) * Interval range (e.g. U+400-4ff): indicates the start and end codepoints of a range * Wildcard range (e.g. U+4??): ‘?’ characters indicate any hexadecimal digit <syntaxhighlight lang="css"> @font-face { font-family: 'Awesome Font'; font-style: normal; font-weight: 400; src: local('Awesome Font'), url('/fonts/awesome-l.woff2') format('woff2'), url('/fonts/awesome-l.woff') format('woff'), url('/fonts/awesome-l.ttf') format('ttf'), url('/fonts/awesome-l.eot') format('eot'); unicode-range: U+000-5FF; /* Latin glyphs */ }</syntaxhighlight> === [https://www.w3.org/TR/WOFF2/ WOFF2] === {| class="wikitable" |- | IE 浏览器 || || |- | 其他浏览器 || || |} === WOFF === {| class="wikitable" |- | IE 浏览器 || 旧的浏览器不支持 || |- | 其他浏览器 || 旧的浏览器不支持 || |} === EOT === {| class="wikitable" |- | IE 浏览器 || 支持 || IE (版本<9)使用 EOT 变体 |- | 其他浏览器 || 不支持 || |} === TTF/OTF === {| class="wikitable" |- | IE 浏览器 || 部分支持 || |- | 其他浏览器 || || |- | 安卓 || 支持 || 安卓(版本<4.4)使用 TTF 变体 |} === SVG === === 兼容 === https://caniuse.com/ == 字体压缩 == * EOT, 和 TTF 格式默认没有压缩:确保你的服务器使用了GZIP压缩 * WOFF 有内置的压缩 - 确保你的WOFF压缩器使用了最优化的设置。 * WOFF2 使用了用户预定义和压缩算法来减少相对于其他字体格式约30%的文件大小 == 字体选择和合成 == == 优化下载和渲染 == * 字体加载请求会在渲染树(the render tree)被创建之后,这可能导致字体渲染被延迟。 * 字体加载 API 同样也允许我们实现用户自定义的字体加载和渲染政策来重写和替代默认的字体延迟加载 * Font inlining 允许我们在旧的浏览器中重新书写字体加载形式 === 字体渲染方式 === 字体的渲染必须在浏览器创建了渲染树(就是DOM树和CSSOM树)之后。 浏览器执行如下操作: # 解析 HTML 文档 # 解析 HTML 响应和构造 DOM # CSS、JS、图片等资源分配请求 #:: CSS资源下载完成并与 DOM 树一起构建 CSSOM,在过程 DOM 树渲染过程中字体被分配请求 #::: {| class="wikitable" |+ 不同的浏览器在渲染的时候执行的操作 |- | Safari || 在文本下载全部完成之后才进行文本渲染 |- | Chrome、Firefox || 使用一个回退字体时,进行三秒钟的字体渲染, 并且一旦字体下载完成,浏览器用刚下载的字体再次进行渲染 |- | IE || 立即渲染回退字体,如果请求的字体不可获得,等待字体加载全部完成之后在进行再次渲染。 |} === 字体下载API来优化字体渲染 === [http://dev.w3.org/csswg/css-font-loading/ Font Loading API]、[https://github.com/bramstein/fontloader FontLoader polyfill]、[https://github.com/typekit/webfontloader webfontloader library] 提供了一个编程脚本来定义和操作CSS字体,追踪字体的下载进程和重写字体的懒加载行为(懒加载是指在DOM树和CSSOM树加载完成之后才进行字体加载渲染的行为)。 <syntaxhighlight lang="javascript"> var font = new FontFace("Awesome Font", "url(/fonts/awesome.woff2)", { style: 'normal', unicodeRange: 'U+000-5FF', weight: '400' }); font.load(); // don't wait for render tree, initiate immediate fetch! font.ready().then(function() { // apply the font (which may rerender text and cause a page reflow) // ... });</syntaxhighlight> === Inlining 来优化渲染字体 === * 在构建 CSSOM 对象的时候, CSS样式中匹配设备的查询将作为首选自动加载; * 将字体数据嵌入 CSS 样式表中将强制浏览器来加载字体,并且以高优先级进行不用等到渲染树的完成。 常见问题: # Inlining 不允许针对不同个页面自定义的定时器和渲染策略; #:: 可以将嵌入字体数据的 CSS 代码写在独立 CSS 文件中,在加载时设置 max-age 属性,避免更新 CSS 文件时再次下载字体。 # 浏览器通常都是在加载 DOM 和形成 CSSOM 对象之后,形成渲染树之后再渲染数据内容到屏幕。将字体数据放在 CSS 中,等于将分配请求放在 CSSOM 对象形成的时候。 === 使用 HTTP 缓存优化你的字体 === 使用 HTTP 缓存,为字体设置较长时间的 max-age 没有必要将字体存储在 localStorage 中,这会影响性能。 == 优化建议 == # 审查和监听你的字体: 每个网页中不要使用太多的字体,每个字体都要简化它们的变体数量。 # 使用字体资源中的子集: 没有必要下载整个字体资源,使用特定的字体子集。特别是对于亚裔字体而言,一定要加载子集。 # 对每个浏览器而言使用最优化的字体格式: 每个字体都应该提供 WOFF2, WOFF, EOT, 和 TTF的字体格式. 确保对 EOT 和 TTF进行GZIP压缩,因为默认它们是没有进行压缩的 # 使用缓存策略: 字体资源是静态的,所以记得使用max-age来设置http缓存 # 使用字体加载API来制定字体渲染方式: 默认的加载方式是字体最后渲染,字体加载API允许我们重写对于特定字体的渲染行为,对于不同的网页我们可以自定义渲染方式亦或设置一个定时器策略。对于旧的不支持字体加载API的浏览器,我们可以使用webfontloader的js库亦或使用css嵌入策略。 == 字体分类 == [https://en.wikipedia.org/wiki/Typeface Typeface] == 计算机字体分类 == === 衬线字体(serif) === {| class="wikitable" |+ Serif |- | Georgia,serif || <p style="font-family:Georgia,serif;">This is a heading<br>This is a paragraph</p> |- | 'Palatino Linotype','Book Antiqua',Palatino,serif || <p style="font-family:'Palatino Linotype','Book Antiqua',Palatino,serif;">This is a heading<br>This is a paragraph</p> |- | 'Times New Roman',Times,serif || <p style="font-family:'Times New Roman',Times,serif;">This is a heading<br>This is a paragraph</p> |- | 宋体,serif || <p style="font-family:宋体,Times,serif;">This is a heading<br>This is a paragraph</p> |- | Big Caslon,serif || <p style="font-family:Big Caslon,serif;">This is a heading<br>This is a paragraph</p> |} === 非衬线字体(sans-serif) === {| class="wikitable" |+ sans-serif |- | Arial,Helvetica,sans-serif || <p style="font-family:Arial,Helvetica,sans-serif;">This is a heading<br>This is a paragraph</p> |- | Arial Black,Gadget,sans-serif || <p style="font-family:Arial Black,Gadget,sans-serif;">This is a heading<br>This is a paragraph</p> |- | 'Comic Sans MS',cursive,sans-serif || <p style="font-family:'Comic Sans MS',cursive,sans-serif;">This is a heading<br>This is a paragraph</p> |- | Impact,Charcoal,sans-serif || <p style="font-family:Impact,Charcoal,sans-serif;">This is a heading<br>This is a paragraph</p> |- | 'Lucida Sans Unicode','Lucida Grande',sans-serif || <p style="font-family:'Lucida Sans Unicode','Lucida Grande',sans-serif;">This is a heading<br>This is a paragraph</p> |- | Tahoma,Geneva,sans-serif || <p style="font-family:Tahoma,Geneva,sans-serif;">This is a heading<br>This is a paragraph</p> |- | 'Trebuchet MS',Helvetica,sans-serif || <p style="font-family:'Trebuchet MS',Helvetica,sans-serif;">This is a heading<br>This is a paragraph</p> |- | Verdana,Geneva,sans-serif || <p style="font-family:Verdana,Geneva,sans-serif;">This is a heading<br>This is a paragraph</p> |- | Helvetica,sans-serif || <p style="font-family:Helvetica,sans-serif;">This is a heading<br>This is a paragraph</p> |- | 黑体,sans-serif || <p style="font-family:黑体,sans-serif;">This is a heading<br>This is a paragraph</p> |} === 等宽字体(monospace) === {| class="wikitable" |+ Monospace |- | 'Courier New',Courier,monospace || <p style="font-family:'Courier New',Courier,monospace;">This is a heading<br>This is a paragraph</p> |- | 'Lucida Console',Monaco,monospace || <p style="font-family:'Lucida Console',Monaco,monospace;">This is a heading<br>This is a paragraph</p> |- | Menlo,monospace || <p style="font-family:Menlo,monospace;">This is a heading<br>This is a paragraph</p> |} === 手写体(cursive ) === Comic Sans MS === 幻想体(fantasy ) === Bodoni Ornaments == Fallback == <blockquote><pre> (hasFont && isInUnicodeTable) ? 'Current Font' : 'Next Font'</pre></blockquote> 国际化的站点设置多语种的字体: <syntaxhighlight lang="css"> [lang="ja"] body{ font-family:"ヒラギノ角ゴ Pro","Hiragino Kaku Gothic Pro","メイリオ",Meiryo,Osaka,"MS Pゴシック","MS PGothic","MS Gothic”, "MS ゴシック","Helvetica Neue”,Helvetica,Arial, sans-serif !important } [lang="ko"] body{ font-family:"나눔 고딕","Nanum Gothic","맑은 고딕","Malgun Gothic”, "Apple Gothic","돋움",Dotum,"Helvetica Neue”,Helvetica, Arial,sans-serif !important } [lang^="zh"] body{ font-family:"Hiragino Sans GB”,"华文细黑","STHeiti","微软雅黑", "Microsoft YaHei",SimHei,"Helvetica Neue”,Helvetica,Arial, sans-serif !important }</syntaxhighlight> == 网络安全字体 == {| class="wikitable" |- | colspan=2| 无衬线字体 |- | Verdana || <p style="font-family:Verdana;">This is a heading<br>This is a paragraph</p> |- | Trebuchet MSe || <p style="font-family:Trebuchet MS;">This is a heading<br>This is a paragraph</p> |- | Arial || <p style="font-family:Arial;">This is a heading<br>This is a paragraph</p> |- | colspan=2| 衬线 |- | Georgia || <p style="font-family:Georgia a;">This is a heading<br>This is a paragraph</p> |- | Times New Roman || <p style="font-family:Times New Roman;">This is a heading<br>This is a paragraph</p> |- | colspan=2| 符号字体 |- | Wingdings || <p style="font-family:Wingdings;">This is a heading<br>This is a paragraph</p> |- | Webdings || <p style="font-family:Webdings;">This is a heading<br>This is a paragraph</p> |- | ITC Zapf Dingbats || <p style="font-family:'ITC Zapf Dingbats';">This is a heading<br>This is a paragraph</p> |} == 示例 ==
返回
HTML/Font
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。