什么是@font-face,以及在css当中如何使用

html-css017

@font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中。

@font-face的语法规则:

@font-face {

font-family: <YourWebFontName>

src: <source>[<format>][,<source>[<format>]]*

[font-weight: <weight>]

[font-style: <style>]

}

取值说明

1、YourWebFontName:此值指的就是你自定义的字体名称,最好是使用你下载的默认字体,他将被引用到你的Web元素中的font-family。如“font-family:"YourWebFontName"”

2、source:此值指的是你自定义的字体的存放路径,可以是相对路径也可以是绝路径;

3、format:此值指的是你自定义的字体的格式,主要用来帮助浏览器识别,其值主要有以下几种类型:truetype,opentype,truetype-aat,embedded-opentype,avg等;

4、weight和style:这两个值大家一定很熟悉,weight定义字体是否为粗体,style主要定义字体样式,如斜体。

样式是不能改变字体本身的形状的。这种使用的是webfont。

使用方法:

下载对应字体的webfont文件,然后放在网站目录(根据需要随意),然后在css里使用 @font-face 添加这个字体,最后body { font-family: "webfont"} 即可。

class{

font-family: 'hakuyoxingshu000Regular'

src: url('000.eot')

src: local('hakuyoxingshu000 Regular'), local('hakuyoxingshu7000'), url('000.ttf') format('truetype'), url('000.svg#hakuyoxingshu000') format('svg')

}

其中的“000”是你的字体名称,有个缺点就是要考虑到兼容性,以上三种写法分别对应的是:

.TTF或.OTF,适用于Firefox 3.5、Safari、Opera

.EOT,适用于Internet Explorer 4.0+

.SVG,适用于Chrome、IPhone。