這是我參與更文挑戰的第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字體):