使用特殊font字体
1、使用css引入字体文件
目录结构
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> @font-face { font-family: "AlibabaPuHuiTiR" ; src: url( "./font/Alibaba-PuHuiTi-Regular.otf" ) format( "truetype" ); } @font-face { font-family: "PingFangSC-Regular" ; src: url( "./PingFang Regular.otf" ) format( "truetype" ); } @font-face { font-family: "PingFang SC" ; src: url( "./font/PingFang Medium.otf" ) format( "truetype" ); } </style> </head> <body> <p style= "font-family: 'AlibabaPuHuiTiR'" >阿里巴巴普惠字体Regular测试</p> <p style= "font-family: 'PingFangSC-Regular'" >平方字体Regular测试</p> <p style= "font-family: 'PingFang SC'" >平方字体测试</p> </body> </html> |
格式
internet Explorer 9, Firefox, Opera,Chrome, 和 Safari支持@font-face 规则.
但是, Internet Explorer 9 只支持 .eot 类型的字体, Firefox, Chrome, Safari, 和 Opera 支持 .ttf 与.otf 两种类型字体.
注意: Internet Explorer 8 及更早IE版本不支持@font-face 规则.
2、特殊情况配置webpack
// webpack打包字体文件问题 config.module .rule('font-ttf') // .test(/\.(woff2?|eot|ttf|otf|ttc)(\?.*)?$/) .test(/\.(ttc)(\?.*)?$/) .use('url-loader') .loader('url-loader') .options({ limit: 20000, name: 'fonts/[name]-[hash:7].[ext]', //`fonts/[name]-[hash].[ext]`, outputPath: 'fonts/', esModule: false }) .end();
1、此处.ttc文件使用url-loader加载,
2、其他类型字体文件(woff、eot、otf、ttf)webpack内置有loader,使用url-loader加载会破坏字体文件格式,导致浏览器解码失败,字体文件不生效。
2、其他类型字体文件(woff、eot、otf、ttf)webpack内置有loader,使用url-loader加载会破坏字体文件格式,导致浏览器解码失败,字体文件不生效。
vue-cli默认配置 路径node_modules/@vue/cli-service/lib/config/base.js
webpackConfig.module .rule('fonts') .test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i) .use('url-loader') .loader(require.resolve('url-loader')) .options(genUrlLoaderOptions('fonts'))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通