这是我参与更文挑战的第15天,活动详情查看: 更文挑战javascript
工做比较忙,为了避免断更,只能发之前的了。css
字体成比例,并且有上下短线。若是字体中的全部字符根据其不一样大小有不一样的宽度,则称为字体是成比例的。例如:小写的i和m的宽度就不一样。上下短线指的是每一个字符笔画末端的装饰,如大写两条腿底部的短线。包括Times\Georgia\New century Schoolbook。
html
字体成比例,且没有上下短线(无衬线字体),包括Helvetica\Geneva\Verdana\Arial\Univers
java
字体不成比例,等宽字体,包括Courier\Courier New\Andale Mono
web
手写体,包括Zapf Chancery\Author\Comic Sans
浏览器
没法归类的字体,包括Western\Woodblock\Klingon
服务器
请看示例:markdown
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style> html { width: 100%; height: 100%; font-size: 20px; } div { margin: 20px; background-color: gray; } .serif { font-family: "Times"; } .sans-serif { font-family: "Arial"; } .Monospace { font-family: "Courier"; } .Cursive { font-family: "Comic Sans"; } .Fantasy { font-family: "Western"; } </style>
</head>
<body>
<div class='serif'>serif字体: y l A<font style="background-color: red">i</font><font style="background-color: yellow">m</font></div>
<div class='sans-serif'>sans-serif字体: y l A<font style="background-color: red">i</font><font style="background-color: yellow">m</font></div>
<div class='Monospace'>Monospace字体: y l A<font style="background-color: red">i</font><font style="background-color: yellow">m</font></div>
<div class='Cursive'>Cursive字体: y l A<font style="background-color: red">i</font><font style="background-color: yellow">m</font></div>
<div class='Fantasy'>Fantasy字体: y l A<font style="background-color: red">i</font><font style="background-color: yellow">m</font></div>
</body>
</html>
复制代码
这里只介绍CSS 字体
serif字体,能够看到字母A是具备上下短线的,并且字母i和m的宽度不等,因此字体成比例。
svg
font-family:[ <family-name> | <generic-family> ] #
复制代码
字体中文名 字体英文名
宋体 SimSun
黑体 SimHei
微软雅黑 Microsoft Yahei
微软正黑体 Microsoft JhengHei
楷体 KaiTi
新宋体 NSimSun
仿宋 FangSong
更多详情访问文章
函数
font-weight:normal | bold | bolder | lighter | <integer>
复制代码
取值:
<integer>
:用数字表示文本字体粗细。取值范围:100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900首先咱们会获得从父元素继承而来的font-weight。当子元素设置成bolder的时候。会有一种规则(测试而来):以下,
.parent1 {
font-weight: normal; /*400*/
}
.child1 {
font-weight: bolder; /*700*/
}
.parent2 {
font-weight: normal; /*100*/
}
.child2 {
font-weight: bolder; /*400*/
}
.parent3 {
font-weight: normal; /*700*/
}
.child3 {
font-weight: bolder; /*900*/
}
复制代码
首先咱们会获得从父元素继承而来的font-weight。当子元素设置成lighter的时候。会有一种规则(测试而来):以下
font-size:<absolute-size> | <relative-size> | <length> | <percentage>
复制代码
<absolute-size>
= xx-small | x-small | small | medium | large | x-large | xx-large<relative-size>
= smaller | larger<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style> html { width: 100%; height: 100%; } .xx-small { font-size: xx-small; } .x-small { font-size: x-small; } .small { font-size: small; } .medium { font-size: medium ; } .large { font-size: large ; } .x-large { font-size: x-large; } .xx-large { font-size: xx-large; } </style>
</head>
<body>
<div>
normal text
</div>
<div class="xx-small">
xx-small text
</div>
<div class="x-small">
x-small text
</div>
<div class="small">
small text
</div>
<div class="medium">
medium text
</div>
<div class="large">
large text
</div>
<div class="x-large">
xlarge text
</div>
<div class="xx-large">
xx-large text
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style> html { width: 100%; height: 100%; } .child { font-size: larger; } .xx-small { font-size: xx-small; } .x-small { font-size: x-small; } .small { font-size: small; } .medium { font-size: medium ; } .large { font-size: large ; } .x-large { font-size: x-large; } .xx-large { font-size: xx-large; } </style>
</head>
<body>
<div class="xx-small">
<div class="child">
xx-small parent
</div>
</div>
<div class="x-small">
<div class="child">
x-small parent
</div>
</div>
<div class="small">
<div class="child">
small parent
</div>
</div>
<div class="medium">
<div class="child">
medium parent
</div>
</div>
<div class="large">
<div class="child">
large parent
</div>
</div>
<div class="x-large">
<div class="child">
x-large parent
</div>
</div>
<div class="xx-large">
<div class="child">
xx-large parent
</div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style> html { width: 100%; height: 100%; } .child { font-size: smaller; } .xx-small { font-size: xx-small; } .x-small { font-size: x-small; } .small { font-size: small; } .medium { font-size: medium ; } .large { font-size: large ; } .x-large { font-size: x-large; } .xx-large { font-size: xx-large; } </style>
</head>
<body>
<div class="xx-small">
<div class="child">
xx-small parent
</div>
</div>
<div class="x-small">
<div class="child">
x-small parent
</div>
</div>
<div class="small">
<div class="child">
small parent
</div>
</div>
<div class="medium">
<div class="child">
medium parent
</div>
</div>
<div class="large">
<div class="child">
large parent
</div>
</div>
<div class="x-large">
<div class="child">
x-large parent
</div>
</div>
<div class="xx-large">
<div class="child">
xx-large parent
</div>
</div>
</body>
</html>
复制代码
.italic {
font-style: italic;
}
.oblique {
font-style: oblique;
}
复制代码
font-variant:normal | small-caps
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> .normal { font-variant: normal; } .small-caps { font-variant: small-caps; } </style>
</head>
<body>
<div class="normal">
NORMAL 文字
</div>
<div class="small-caps">
LARGE WORDS small-caps 文字
</div>
</body>
</html>
复制代码
font-stretch:normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded
复制代码
font-size-adjust:none | <number>
复制代码
<number>
定义字体的 aspect 值**font:[ [ <' font-style '> || <' font-variant '> || <' font-weight '> ]? <' font-size '> [ / <' line-height '> ]? <' font-family '> ] | caption | icon | menu | message-box | small-caption | status-bar
复制代码
在 CSS3 以前,web 设计师必须使用已在用户计算机上安装好的字体。经过 CSS3,web 设计师可使用他们喜欢的任意字体。
当找到或购买到但愿使用的字体时,可将该字体文件存放到 web 服务器上,它会在须要时被自动下载到用户的计算机上。字体是在 CSS3 @font-face 规则中定义的。可是若是咱们下载的字体太大,会存在性能问题,不过好在如今的字体服务商给的文件都不大。
@font-face {
font-family: <YourWebFontName>;
src: <source> [<format>][,<source> [<format>]]*;
[font-weight: <weight>];
[font-style: <style>];
[unicode-range: <unicode-range>];
}
复制代码
@font-face {
font-family: YourWebFontName;
src: url(../font/test.eot);
src: url(../font/test.eot?#iefix) format("embedded-opentype"),
url(../font/test.woff) format("woff"),
url(../font/test.ttf) format("truetype"),
url(../font/test.svg#jq) format("svg");
}
复制代码
@font-face规则中有一个很是重要的参数,就是src。其值主要是用于链接到实际的字体文件。此外,能够声明多个来源,若是浏览器未能找到第一个来源,他回一次寸照下面的字体来源。
上面声明了四中字体:EOT, WOFF, TTF和SVG。
这里以国内的网站有字库为例。
而后获得对应的font-face代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> @font-face { font-family: 'ChannelSlanted2'; src: url('http://cdn.webfont.youziku.com/webfonts/nomal/118923/45817/5b0d6df4f629d91b10cd3bc2.gif?r=76000373532'); src: url('http://cdn.webfont.youziku.com/webfonts/nomal/118923/45817/5b0d6df4f629d91b10cd3bc2.gif?r=76000373532?#iefix') format('embedded-opentype'), url('http://cdn.webfont.youziku.com/webfonts/nomal/118923/45817/5b0d6df4f629d91b10cd3bc2.png?r=76000373532') format('woff2'), url('http://cdn.webfont.youziku.com/webfonts/nomal/118923/45817/5b0d6df4f629d91b10cd3bc2.bmp?r=76000373532') format('woff'); font-weight: normal; font-style: normal; } .ChannelSlanted2 { font-family: "ChannelSlanted2" } </style>
</head>
<body>
<div class="ChannelSlanted2">
LARGE WORDS small-caps 文字
</div>
</body>
</html>
复制代码
而后获得咱们的图字效果。
还能够到网站(www.fontsquirrel.com/)去下载一些字体的格式。而后若是咱们的到只是其中一种格式的话,由于前面给出的网站下载的都是.otf格式,因此咱们还须要转换成其余格式。可使用下面的网站进行格式转换。
@font-face 规则中的src 描述符还能够接受local()函数,用于指定本地字体的名称。当不须要用到任何外部的Web 字体,就能够直接在字体队列中指定一款本地字体:
@font-face {
font-family: Ampersand;
src: local('Baskerville'),
local('Goudy Old Style'),
local('Garamond'),
local('Palatino');
}
复制代码
举例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style> @font-face { font-family: Ampersand; src: local('Comic Sans MS'); } .icon { font-family: Ampersand } </style>
</head>
<body>
<div class="icon">
HTML & CSS
</div>
</body>
</html>
复制代码
当咱们想要控制字符应用的范围,须要一个描述符来声明咱们想用这几款本地字体来显示哪些字符。
unicode-range 描述符只在@font-face 规则内部生效(所以这里用了描述符这个术语;它并非一个CSS 属性),它能够把字体做用的字符范围限制在一个子集内。它对本地字体和远程字体都是有效
这个unicode-range 在实践中很是实用,但在语法上却很是晦涩。它的语法是基于“Unicode 码位”的,而不是基于字符的字面形态。
例如:咱们想是上面的示例中HTML & CSS中的&应用字体,其余的不变化。
"&".charCodeAt(0).toString(16); // 返回26
复制代码
须要在码位前面加上U+ 做为前缀
unicode-range: U+26;
复制代码
*若是你想指定一个字符区间,仍是要加上U+ 前缀,好比U+400-4FF。实际上对于这个区间来讲,你还可使用通配符,以这样的方式来写:U+4??。同时指定多个字符或多个区间也是容许的,把它们用逗号隔开便可,好比U+26, U+4??, U+2665-2670。
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> @font-face { font-family: Ampersand; src: local('Comic Sans MS'); unicode-range: U+26; } .icon { font-family: Ampersand } </style> </head> <body> <div class="icon"> HTML & CSS </div> </body> </html> 复制代码
而后咱们看一下如今的样式以及和以前的样式对比(红色的是带有unicode-range的html,能够看到当前只有&仍是和以前相同,其余的不在应用Ampersand字体):