?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
@font-face { font-family: <identifier>; src: <fontsrc> [, <fontsrc>]*;<font>; }
<fontsrc> = <url> [format(<string>)]
<identifier>:字體名稱
<url>:此值指的是你自定義的字體的存放路徑,可以是相對路徑也可以是絕路徑
<string>:此值指的是你自定義的字體的格式,主要用來幫助瀏覽器識別,其值主要有以下幾種類型:truetype, opentype, embedded-opentype, svg等
<font>:定義字體相關樣式
需要兼容當前的主流瀏覽器,需同時使用TureTpe(.ttf)、Web Open Font Format(.woff)、Embedded Open Type(.eot)、SVG(.svg)四種字體格式。
嵌入HTML文檔的字體是指將OpenType字體(壓縮的TrueType字體)文件映射到客戶端系統(tǒng),用來提供HTML文檔使用該字體,或取代客戶端系統(tǒng)已有的同名字體。即讓客戶端顯示客戶端所沒有安裝的字體。
.eot(Embedded Open Type)為IE的私有字體格式。Safari3.1開始支持.ttf(TrueType)和.otf(OpenType)。
未來.woff(Web Open Font Format)將會取代.ttf(TrueType)和.otf(OpenType)兩種字體格式。
示例:使用一個全瀏覽器兼容的自定義字體
代碼如下:
@font-face {
font-family: 'diyfont';
src: url('diyfont.eot'); /* IE9+ */
src: url('diyfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('diyfont.woff') format('woff'), /* chrome、firefox */
url('diyfont.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
url('diyfont.svg#fontname') format('svg'); /* iOS 4.1- */
}
你需要同時提供4種格式的字體
Values | IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|---|
Basic Support | 6.0+ | 2.0+ | 4.0+ | 3.1+ | 15.0+ | 3.2+ | 2.1+ | 18.0+ |
eot | 6.0+ | 2.0-38.0 | 4.0-43.0 | 3.1-8.1 | 5.0-28.0 | 3.2-8.1 | 2.1-4.4.4 | 18.0-40.0 |
ttf/otf | 6.0-8.0 | 2.0-3.0 | 4.0+ | 3.1+ | 15.0+ | 3.2-4.1 | 2.1 | 18.0+ |
9.0+ | 3.5+ | 4.3+ | 2.2+ | |||||
woff | 6.0-8.0 | 2.0-3.5 | 4.0 | 3.1-5.0 | 15.0+ | 3.2-4.3 | 2.1-4.3 | 18.0+ |
9.0+ | 3.6+ | 5.0+ | 5.1+ | 5.0+ | 4.4+ | |||
svg | 6.0-11.0 | 2.0-38.0 | 4.0-37.0 | 3.1 | 15.0-24.0 | 3.2+ | 2.1-2.3 | 18.0-37.0 |
38.0-43.0 #1 | 3.2+ | 25.0-28.0 #1 | 3.0+ | 38.0-43.0 #1 |
策略修改為只支持windows vista/xp上的svg fonts
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8" /> <title>@font-face_CSS參考手冊_web前端開發(fā)參考手冊系列</title> <meta name="author" content="Joy Du(飄零霧雨), dooyoe@gmail.com, www.doyoe.com" /> <style> @font-face { font-family: 'iconfont'; src: url('http://at.alicdn.com/t/font_1397098551_95441.eot'); src: url('http://at.alicdn.com/t/font_1397098551_95441.eot?#iefix') format('embedded-opentype'), url('http://at.alicdn.com/t/font_1397098552_0142624.woff') format('woff'), url('http://at.alicdn.com/t/font_1397098551_8732882.ttf') format('truetype'), url('http://at.alicdn.com/t/font_1397098552_0586202.svg#iconfont') format('svg'); } body { font-family: 'iconfont'; font-style: normal; font-size: 32px; line-height: 2; letter-spacing: .25em; -webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0.2px; -moz-osx-font-smoothing: grayscale; } </style> </head> <body> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </body> </html>
點擊 "運行實例" 按鈕查看在線實例