CSS中加載字體文件有多種格式,以下是一些常見的字體文件格式及其加載方法:
1、TrueType字體(.ttf):
- 使用@font-face
規(guī)則加載字體文件。
- 示例:
```css
@font-face {
font-family: 'MyFont';
src: url('path/to/myfont.ttf') format('truetype');
}
```
2、OpenType字體(.otf):
- 同樣使用@font-face
規(guī)則加載字體文件。
- 示例:
```css
@font-face {
font-family: 'MyFont';
src: url('path/to/myfont.otf') format('opentype');
}
```
3、Type 1字體(.pfb):
- 使用@font-face
規(guī)則加載字體文件。
- 示例:
```css
@font-face {
font-family: 'MyFont';
src: url('path/to/myfont.pfb') format('type1');
}
```
4、WOFF字體(.woff):
- 使用@font-face
規(guī)則加載字體文件。
- 示例:
```css
@font-face {
font-family: 'MyFont';
src: url('path/to/myfont.woff') format('woff');
}
```
5、WOFF2字體(.woff2):
- 使用@font-face
規(guī)則加載字體文件。
- 示例:
```css
@font-face {
font-family: 'MyFont';
src: url('path/to/myfont.woff2') format('woff2');
}
```
在加載字體文件時(shí),建議將字體文件存放在與CSS文件相同的目錄中,或者提供一個(gè)相對路徑,這樣瀏覽器可以更容易地找到并加載字體文件,為了兼容不同的瀏覽器,可以使用format()
函數(shù)來指定字體文件的格式,這樣可以確保字體文件在大多數(shù)瀏覽器上都能正確加載和顯示。