CSS/字体:修订间差异
来自康健生活
| (未显示同一用户的14个中间版本) | |||
| 第1行: | 第1行: | ||
== 字号 == | |||
浏览器限制字号最小 12px,但是可以通过设置 <syntaxhighlight lang="css" inline> | |||
transform: scale(0.7); | |||
</syntaxhighlight> 进行更小化处理。 | |||
== 格式 == | |||
=== .otf === | |||
=== .eot === | |||
=== .svg === | |||
=== WOFF === | |||
桌面浏览器 | |||
=== TTF === | |||
旧版Safari,Android和iOS浏览器的后备版 | |||
== onlinefontconverter == | |||
== 组合方案 == | |||
=== 字体命名 === | |||
在中文系统中,字体拥有两种命名,如:<code>"Microsoft YaHei", "微软雅黑"</code>。所以为了避免用户在客户端的某些设置导致中文命名失效,要保守使用: | |||
<syntaxhighlight lang="css"> | |||
font-family: "Microsoft YaHei", "微软雅黑";</syntaxhighlight> | |||
=== 中英区分 === | |||
绝大多数中文字体内包含英文和数字,但展示效果不如人意,所以需要“渐进”处理,即先声明英文再声明中文。如: | |||
<syntaxhighlight lang="css"> | |||
font-family: Arial, "Microsoft YaHei"; | |||
</syntaxhighlight> | |||
=== 客户端系统 === | |||
不同的客户端系统,如 windows、linux、mac 等。 | |||
<syntaxhighlight lang="css"> | |||
font-family: Helvetica, Tahoma, Arial;</syntaxhighlight> | |||
=== 向下兼容 === | |||
旧版操作系统存在的字体。 | |||
=== 补充字体族名称 === | |||
使用字体族名称作为操作系统最后选择字体的方向。如,使用 sans-serif(非衬线体): | |||
<syntaxhighlight lang="css"> | |||
font-family: Helvetica, Tahoma, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "MicrosoftYaHei", "WenQuanYi Micro Hei", sans-serif;</syntaxhighlight> | |||
== 特殊字体解决方案 == | |||
=== 字体压缩 === | |||
* https://www.font-spider.org/ | |||
* http://ecomfe.github.io/fontmin/#banner | |||
=== 转换 === | |||
* https://github.com/nfroidure/ttf2woff2 | |||
* http://www.fontsquirrel.com/tools/webfont-generator | |||
* http://everythingfonts.com/ttf-to-woff2 | |||
* https://www.fontke.com/tool/convfont/ | |||
=== 转为 js === | |||
* http://cufon.shoqolate.com/generate/ | |||
* https://github.com/sorccu/cufon | |||
=== 异步加载 === | |||
== 判断客户端字体 == | |||
<syntaxhighlight lang="javascript"> | |||
var isSupportFontFamily=function(f){if(typeof f!="string"){return false}var h="Arial";if(f.toLowerCase()==h.toLowerCase()){return true}var e="a";var d=100;var a=100,i=100;var c=document.createElement("canvas");var b=c.getContext("2d");c.width=a;c.height=i;b.textAlign="center";b.fillStyle="black";b.textBaseline="middle";var g=function(j){b.clearRect(0,0,a,i);b.font=d+"px "+j+", "+h;b.fillText(e,a/2,i/2);var k=b.getImageData(0,0,a,i).data;return[].slice.call(k).filter(function(l){return l!=0})};return g(h).join("")!==g(f).join("")};</syntaxhighlight> | |||
<syntaxhighlight lang="javascript"> | |||
isSupportFontFamily(fontFamily);</syntaxhighlight> | |||
== 字体大小 == | |||
<syntaxhighlight lang="css"> | |||
font: 12px/1.5 Arial; | |||
font-family:Arial; | |||
font-size:12px; | |||
line-height:1.5em; | |||
</syntaxhighlight> | |||
== 引用字体 == | |||
=== 引用字体跨域 === | |||
==== base64 编码 ==== | |||
=== 动态引入 === | |||
方法一 | |||
<syntaxhighlight lang="css"> | |||
@font-face { /* 有多少个字体要写多少个“@font-face {}”块 */ | |||
font-family:; | |||
src: url('Coda.eot'); /* IE9 */ | |||
src: url('Coda.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ | |||
url('Coda.woff') format('woff'), /* Modern Browsers */ | |||
url('Coda.ttf') format('truetype'), /* Safari, Android, iOS */ | |||
url('Coda.svg#Coda') format('svg'); /* Legacy iOS */ | |||
}</syntaxhighlight> | |||
方法二 | |||
==== document.fonts API ==== | |||
<syntaxhighlight lang="javascript"> | |||
// 浏览器是否支持 | |||
if (!document.fonts) { | |||
alert('抱歉,当前浏览器兼容性较差,会影响操作体验,请更换最新版谷歌浏览器进行操作'); | |||
} | |||
// 检验字体是否已经安装 | |||
if (document.fonts.check(`16px Noto Serif CJK SC`, '上')) { | |||
return Promise.resolve(true); | |||
} | |||
// 加载字体 | |||
const fontface = new FontFace(name, `url(xxx)`); | |||
document.fonts.add(fontface); | |||
fontface.load(); | |||
fontface.loaded.then(() => { | |||
// 字体加载完毕,可使用 | |||
}).catch(err => { | |||
// 字体加载失败 | |||
});</syntaxhighlight> | |||
vue 的写法: | |||
<syntaxhighlight lang="javascript"> | |||
//加载字体文件 | |||
obj格式,cssValue为自定义字体的名字,url为自定义字体的文件路径 | |||
loadFont(obj){ | |||
if(document.fonts&&!this.checkFont(obj.cssValue)) | |||
{ | |||
let that=this; | |||
let fontFamily=obj.cssValue; | |||
console.log(obj); | |||
let fontFace = new FontFace(obj.cssValue, `local('${obj.cssValue}'),url('${obj.url}') format('ttf'),url('${obj.url}')`); | |||
fontFace.load().then(function(loadedFontFace) { | |||
document.fonts.add(loadedFontFace); | |||
that.canvasDemo.updateTextFontFamily(fontFamily); | |||
}); | |||
} | |||
}, | |||
//检测字体文件是否已加载 | |||
checkFont(name){ | |||
let values=document.fonts.values(); | |||
let isHave=false; | |||
let item=values.next(); | |||
while(!item.done&&!isHave) | |||
{ | |||
let fontFace=item.value; | |||
if(fontFace.family==name) | |||
{ | |||
isHave=true; | |||
} | |||
item=values.next(); | |||
} | |||
return isHave; | |||
} | |||
</syntaxhighlight> | |||
{{Unknown}} | |||
== 字体预加载 == | |||
<blockquote><pre> | |||
font-display: swap;</pre></blockquote> | |||
* font-display: swap: | |||
*: 浏览器直接使用 font-family 中最先匹配到的已经能够使用的字体,然后当 font-family 中更靠前的字体成功载入时,切换到更靠前的字体。 | |||
* font-display: fallback: | |||
*: 浏览器会先等待最靠前的字体加载,如果没加载到就不显示任何东西,过程大约持续100ms,之后按照顺序显示已经成功载入的字体。在此之后有大约3s的时间来提供切换到加载完毕的更靠前的字体。 | |||
* font-display: optional: | |||
*: 浏览器会先等待最靠前的字体加载,如果没加载到就不显示任何东西,过程大约持续100ms,之后字体就不会再更改(第一次打开某页面的时候都会使用fallabck字体,字体被下载但是没被使用,之后打开时会使用缓存中的字体)。 | |||
== 单位 == | == 单位 == | ||
=== px === | === px === | ||
== pt == | === pt === | ||
计算公式 | 计算公式 | ||
<blockquote><pre> | <blockquote><pre> | ||
| 第32行: | 第175行: | ||
安装read-edid软件包并<code>sudo get-edid | parse-edid | grep DisplaySize</code>在终端中运行命令。它的输出将为您提供显示器的实际大小(以毫米为单位)。如果不是,请使用标尺。 | 安装read-edid软件包并<code>sudo get-edid | parse-edid | grep DisplaySize</code>在终端中运行命令。它的输出将为您提供显示器的实际大小(以毫米为单位)。如果不是,请使用标尺。 | ||
== fallback 机制 == | |||
== 字体子集化 == | |||
* 腾讯:font-spider | |||
* 百度:font-min | |||
* 阿里巴巴:icon-font font-carrier | |||
== error == | |||
=== 垂直居中偏移问题 === | |||
==== line-height ==== | |||
* 可能是使用了奇数字号。 | |||
* 可能是字号小于12px。 | |||
==== align-items ==== | |||
==== inline-block段落间有缝隙 ==== | |||
== 参考 == | |||
* http://randsco.com/index.php/2009/07/04/p680 | |||
* https://alistapart.com/blog/topic/typography-web-fonts/ | |||