@font-face(css3屬性)實現在網頁中嵌入任意字型,@font-facecss3
@font-face文法規則
@font-face { font-family: <YourWebFontName>; src: <source> [<format>][,<source> [<format>]]*; [font-weight: <weight>]; [font-style: <style>]; }
說明:
-
YourWebFontName:此值指的就是你自訂的字型名稱,最好是使用你下載的預設字型,他將被引用到你的Web元素中的font-family。如“font-family:"YourWebFontName";”
-
source:此值指的是你自訂的字型的存放路徑,可以是相對路徑也可以是絕路徑;
format:此值指的是你自訂的字型的格式,主要用來協助瀏覽器識別,其值主要有以下幾種類型:truetype,opentype,truetype-aat,embedded-opentype,avg等;weight和style:這兩個值大家一定很熟悉,weight定義字型是否為粗體,style主要定義字型樣式,如斜體。
執行個體:(以SingleMaltaRegular為例)
-
下載所需要的字型。
-
獲得@font-face所需的.eot,.woff,.ttf,.svg字型格式字型。可以在這個網站上搞定。點擊進入 (fontsquirrel)
-
Font Squirrel下載檔案到本地的電腦上了,解壓。即可得到字型。
-
在項目中建立檔案夾fonts,講下載字型放進去。
樣式書寫格式:
@font-face { font-family: 'SingleMaltaRegular'; src: url('../fonts/singlemalta-webfont.eot'); src: url('../fonts/singlemalta-webfont.eot?#iefix') format('embedded-opentype'), url('../fonts/singlemalta-webfont.woff') format('woff'), url('../fonts/singlemalta-webfont.ttf') format('truetype'), url('../fonts/singlemaltawebfont.svg#SingleMaltaRegular') format('svg'); font-weight: normal; font-style: normal;}
最後即可引用的字型:
body{ font-family: 'SingleMaltaRegular'}
註:通過網站下載的檔案裡,會自動產生樣式,從而直接拷貝即可