CSS Fonts(字體)
CSS?字體
CSS字體屬性定義字體,加粗,大小,文字樣式。
在計算機(jī)屏幕上,sans-serif字體被認(rèn)為是比serif字體容易閱讀
CSS字型
在CSS中,有兩種類型的字體系列名稱:
通用字體系列?- 擁有相似外觀的字體系統(tǒng)組合(如 "Serif" 或 "Monospace")
特定字體系列?- 一個特定的字體系列(如 "Times" 或 "Courier")
Generic family | 字體系列 | 說明 |
---|---|---|
Serif | Times New Roman ? ? ? ? ? ?Georgia | Serif字體中字符在行的末端擁有額外的裝飾 |
Sans-serif | Arial ? ? ? ? ? ?Verdana | "Sans"是指無 - 這些字體在末端沒有額外的裝飾 |
Monospace | Courier New ? ? ? ? ? ?Lucida Console | 所有的等寬字符具有相同的寬度 |
字體系列
font-family 屬性設(shè)置文本的字體系列。
font-family 屬性應(yīng)該設(shè)置幾個字體名稱作為一種"后備"機(jī)制,如果瀏覽器不支持第一種字體,他將嘗試下一種字體。
注意: 如果字體系列的名稱超過一個字,它必須用引號,如Font Family:"宋體"。
多個字體系列是用一個逗號分隔指明:
<!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)行程序嘗試一下
字體樣式
主要是用于指定斜體文字的字體樣式屬性。
這個屬性有三個值:
正常 - 正常顯示文本
斜體 - 以斜體字顯示的文字
傾斜的文字 - 文字向一邊傾斜(和斜體非常類似,但不太支持)
實(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>
運(yùn)行程序嘗試一下
字體大小
font-size 屬性設(shè)置文本的大小。
能否管理文字的大小,在網(wǎng)頁設(shè)計中是非常重要的。但是,你不能通過調(diào)整字體大小使段落看上去像標(biāo)題,或者使標(biāo)題看上去像段落。
請務(wù)必使用正確的HTML標(biāo)簽,就<h1> - <h6>表示標(biāo)題和<p>表示段落:
字體大小的值可以是絕對或相對的大小。
絕對大?。?/strong>
設(shè)置一個指定大小的文本
不允許用戶在所有瀏覽器中改變文本大小
確定了輸出的物理尺寸時絕對大小很有用
相對大小:
相對于周圍的元素來設(shè)置大小
允許用戶在瀏覽器中改變文字大小
?注意:如果你不指定一個字體的大小,默認(rèn)大小和普通文本段落一樣,是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>這個例子在 IE9之前的版本不工作, prior version 9.</p> </body> </html>
運(yùn)行程序嘗試一下
用em來設(shè)置字體大小
為了避免Internet Explorer 中無法調(diào)整文本的問題,許多開發(fā)者使用 em 單位代替像素。
em的尺寸單位由W3C建議。
1em和當(dāng)前字體大小相等。在瀏覽器中默認(rèn)的文字大小是16px。
因此,1em的默認(rèn)大小是16px??梢酝ㄟ^下面這個公式將像素轉(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)整文本的大小時,會比正常的尺寸更大或更小。
使用百分比和EM組合
在所有瀏覽器的解決方案中,設(shè)置元素的默認(rèn)字體大小的是百分比:
實(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>
運(yùn)行程序嘗試一下
實(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>
運(yùn)行程序嘗試一下
CSS字體屬性
Property | 描述 |
---|---|
font | 在一個聲明中設(shè)置所有的字體屬性 |
font-family | 指定文本的字體系列 |
font-size | 指定文本的字體大小 |
font-style | 指定文本的字體樣式 |
font-variant | 以小型大寫字體或者正常字體顯示文本。 |
font-weight | 指定字體的粗細(xì)。 |