CSS Fonts(字體)
CSS?字體
CSS字體屬性定義字體,加粗,大小,文字樣式。
在電腦螢?zāi)簧?,sans-serif字體被認(rèn)為是比serif字體容易閱讀
CSS字體
在CSS中,有兩種??類型的字體系列名稱:
通用字體系列?- 擁有相似外觀的字體系統(tǒng)組合(如"Serif" 或"Monospace")
特定字體系列?- 一個(gè)特定的字體系列(如"Times" 或"Courier")
Generic family | 字體系列 | 說明 |
---|---|---|
Serif | Times New Roman | ##Serif|
? ? ? ? ? ? | GeorgiaSerif字體中字元在行的末端擁有額外的裝飾 | #Sans-serif##Arial |
Verdana | #"Sans"是指無- 這些字體在末端沒有額外的裝飾 #Monospace |
字體系列
font-family 屬性設(shè)定文字的字體系列。
font-family 屬性應(yīng)該設(shè)定幾個(gè)字體名稱作為一種"後備"機(jī)制,如果瀏覽器不支援第一種字體,他將嘗試下一種字體。
注意: 如果字體系列的名稱超過一個(gè)字,它必須用引號(hào),如Font Family:"宋體"。
多個(gè)字體系列是用一個(gè)逗號(hào)分隔指明:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.serif{font-family:"Times New Roman",Times,serif;} p.sansserif{font-family:Arial,Helvetica,sans-serif;} </style> </head> <body> <h1>CSS font-family</h1> <p class="serif">這一段的字體是 Times New Roman </p> <p class="sansserif">這一段的字體是 Arial.</p> </body> </html>
運(yùn)行程式嘗試
字體樣式
主要是用來指定斜體文字的字體樣式屬性。
這個(gè)屬性有三個(gè)值:
#正常- 正常顯示文字
斜體- 以斜體字顯示的文字
傾斜的文字- 文字向一邊傾斜(和斜體非常類似,但不太支援)
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.normal {font-style:normal;} p.italic {font-style:italic;} p.oblique {font-style:oblique;} </style> </head> <body> <p class="normal">這是一段正常的字體 - </p> <p class="italic">這是一段正常斜體字傾斜的文字 </p> <p class="oblique">文字向一邊傾斜(和斜體非常類似,但不太支持)</p> </body> </html>
執(zhí)行程式嘗試
#字體大小
font-size 屬性設(shè)定文字的大小。
能否管理文字的大小,在網(wǎng)頁設(shè)計(jì)上是非常重要的。但是,你不能透過調(diào)整字體大小使段落看起來像標(biāo)題,或使標(biāo)題看起來像段落。
請務(wù)必使用正確的HTML標(biāo)籤,就<h1> - <h6>表示標(biāo)題和<p>表示段落:
#字體大小的值可以是絕對或相對的大小。
絕對大小:
#設(shè)定一個(gè)指定大小的文字
#不允許使用者在所有瀏覽器中改變文字大小
#確定了輸出的物理尺寸時(shí)絕對大小很有用
相對大?。?/strong>
#相對於周圍的元素來設(shè)定大小
允許使用者在瀏覽器中改變文字大小
#?注意:如果你不指定一個(gè)字體的大小,預(yù)設(shè)大小和普通文字段落一樣,是16像素(16px=1em)。
設(shè)定字型大小像素
#設(shè)定文字的大小與像素,讓您完全控製文字大?。?/span>
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1 {font-size:40px;} h2 {font-size:30px;} p {font-size:14px;} </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p> <p>Specifying the font-size in px allows Internet Explorer 9, Firefox, Chrome, Opera, and Safari to resize the text.</p> <p><b>注意:</b>這個(gè)例子在 IE9之前的版本不工作, prior version 9.</p> </body> </html>
運(yùn)行程式嘗試
##用em來設(shè)定字體大小
為了避免Internet Explorer 中無法調(diào)整文字的問題,許多開發(fā)者使用em 單位來取代像素。 em的尺寸單位由W3C建議。 1em和目前字體大小相等。在瀏覽器中預(yù)設(shè)的文字大小是16px。 因此,1em的預(yù)設(shè)大小是16px。可以透過下面這個(gè)公式將像素轉(zhuǎn)換為em:px/16=em#實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1 {font-size:2.5em;} /* 40px/16=2.5em */ h2 {font-size:1.875em;} /* 30px/16=1.875em */ p {font-size:0.875em;} /* 14px/16=0.875em */ </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p> <p>Specifying the font-size in em allows all major browsers to resize the text. Unfortunately, there is still a problem with older versions of IE. When resizing the text, it becomes larger/smaller than it should. </p> </body> </html>
運(yùn)行程式嘗試
在上面的例子,em的文字大小是與前面的例子中像素一樣。不過,如果使用 em 單位,則可以在所有瀏覽器中調(diào)整文字大小。
不幸的是,仍然是IE瀏覽器的問題。調(diào)整文字的大小時(shí),會(huì)比正常的尺寸更大或更小。
使用百分比和EM組合
#在所有瀏覽器的解決方案中,設(shè)定元素的預(yù)設(shè)字體大小的是百分比:
#實(shí)例##<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
<style>
body {font-size:100%;}
h1 {font-size:2.5em;}
h2 {font-size:1.875em;}
p {font-size:0.875em;}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>Specifying the font-size in percent and em displays the same size in all
major browsers, and allows all browsers to resize the text!</p>
</body>
</html>
實(shí)例#設(shè)定字型加粗
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.normal {font-weight:normal;} p.light {font-weight:lighter;} p.thick {font-weight:bold;} p.thicker {font-weight:900;} </style> </head> <body> <p class="normal">你要記得,紫檀未滅,我亦未去。</p> <p class="light">誰在歲月里長長嘆息。</p> <p class="thick">漢霄蒼茫,牽住繁華哀傷,彎眉間,命中注定,成為過往。</p> <p class="thicker">php中文網(wǎng)(php.cn)</p> </body> </html>
執(zhí)行程式嘗試
CSS字型屬性
#Property | 描述 |
---|---|
font | #在一個(gè)宣告中設(shè)定所有的字型屬性 |
#font-family | 指定文字的字體系列 |
#font-size | 指定文字的字體大小 |
font-style | 指定文字的字體樣式 |
#font-variant | 以小型大寫字體或正常字體顯示文字. |
font-weight | 指定字體的粗細(xì)。 |