DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS文字排版終極指南:從font屬性到CSS3新特性
編輯:CSS詳解     

改寫自英文文章《The Ultimate Guide to CSS Typography》。


排版是網頁設計裡最容易被低估的一環。視覺效果搶眼、動畫炫目,但如果文字讀起來吃力,一切就白搭了。好消息是,CSS 對排版的掌控力遠比多數人想像的強;壞消息是,我們長久以來被所謂「網頁安全字型」限制了想像,很少認真對待這件事。

以下把常用的排版技巧整理成一份可以反覆翻閱的清單。

一、Font 相關屬性

font-size:字級

最基礎也最常用,寫法可以是相對單位、像素或點數。

font-size: 1.2em;
font-size: 12px;
font-size: 10pt;

實際渲染出來的對比:

  1. 字級為 1.2em
  2. 字級為 12px
  3. 字級為 10pt

font-weight:字重

用來控制粗細。除了關鍵字,也可以直接用 100 到 900 的數值,數字越大越粗,其中 400 等同 normal、700 等同 bold。

font-weight: normal;
font-weight: bold;
font-weight: bolder;
font-weight: lighter;
  1. 設為 normal
  2. 設為 bold
  3. 設為 bolder
  4. 設為 lighter

text-transform:大小寫轉換

可以讓文字全部變大寫、全部變小寫,或把每個單字的首字母放大寫。要注意這個屬性對中文沒有作用。

text-transform: capitalize;
text-transform: uppercase;
text-transform: lowercase;
text-transform: inherit;
  1. Capitalizes The First Letter In Every Word
  2. allows your html to be lower case then converts it all to uppercase
  3. ALLOWS YOUR HTML TO BE UPPERCASE THEN CONVERTS IT TO LOWERCASE
  4. 繼承父元素的值

text-decoration:文字裝飾

底線、上線、刪除線都在這裡。順帶一提,blink 這個值實際上只有少數瀏覽器支援,正式專案不建議使用。

text-decoration: normal;
text-decoration: underline;
text-decoration: overline;
text-decoration: line-through;
text-decoration: blink;
  1. 設為 normal
  2. 設為 underline
  3. 設為 overline
  4. 設為 line-through
  5. 設為 blink(支援度極差)

font-variant:小型大寫字母

把小寫字母轉成尺寸較小的大寫字母,常用於標籤或英文短標題。

font-variant: normal;
font-variant: small-caps;
font-variant: inherit;
  1. 設為 normal
  2. 設為 small-caps
  3. 設為 inherit

letter-spacing 與 word-spacing

前者調整字母間距,後者調整單字間距。要注意 word-spacing 對中文無效,因為中文字之間本來就沒有單字概念的空格。

letter-spacing: normal;
letter-spacing: 2px;
letter-spacing: inherit;
  1. letter-spacing 設為 normal
  2. letter-spacing 設為 2px
  3. letter-spacing 設為 inherit

font 縮寫

上述屬性可以合併成一行,能有效縮短樣式表:

font: font-style font-variant font-weight font-size/line-height font-family;

順序不能亂。另外有個容易踩到的陷阱:使用縮寫時若沒寫 line-height,該元素的行高會被設為預設值,而不會從父元素繼承。同時,font-size 與 font-family 是必填,其餘可省略。

二、如何做出舒服的段落

排版名家 Robert Bringhurst 有個簡單的換算原則:把字級乘以 30,就是理想的可視寬度。以 12px 的字級計算,寬度約 360px,一行剛好容納 60 多個英文字元,閱讀起來最不費力。

寬度不足(每行字太少,眼睛來回跳)

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

寬度合適

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

line-height:行高

行高決定行之間的呼吸空間。實務上的經驗法則是:行高比字級大 6 到 7 像素。也就是說,12px 的字配 18px 的行高會相當剛好。

行高過小(文字擠成一團)

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

行高合適

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

三、把重點標記清楚

一個常被忽略的共識:底線應該保留給連結。如果在內文裡用底線來強調字詞,讀者會誤以為那是可點擊的連結。想強調時,斜體是更合適的選擇。

不建議:用粗體強調

Lorem Ipsum is simply dummy text, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

建議:改用斜體

Lorem Ipsum is simply dummy text, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

:first-letter 與 :first-line

顧名思義,前者選取首字母,後者選取第一行。這兩個是少數被所有主流瀏覽器穩定支援的 CSS 2.1 偽元素,可以套任何樣式,最常見的用法就是首字下沉。

#demo5 { width: 400px; }
#demo5:first-letter { font-size: 32px; color: green; }
#demo5:first-line { color: red; }

套用後的段落會呈現放大的首字母與變色的首行,雜誌感的版面幾乎都是靠這個技巧完成的。

四、CSS3 時代的新選項

@font-face

這個規則讓你能把字型檔放在自己的伺服器上,於樣式表中宣告後就能任意使用,等於徹底擺脫「網頁安全字型」的舊限制。不過有兩個現實問題:一是多數商用字型的授權並不允許這樣使用;二是中文字型檔動輒好幾 MB,載入時間會嚴重影響體驗。近年虽有子集化與 CDN 等解法,但成本仍需評估。

/* 宣告字型 */
@font-face { src: url(font/diavlo.otf); }
/* 套用 */
h1 { font-family: diavlo, Arial; }

text-shadow

以往要幫標題做陰影得開 Photoshop 輸出圖片,現在一行 CSS 就能搞定,而且能動態調整。

陰影範例一

p { font-size: 2em; font-weight: bold; color: #777; text-shadow: 1px 1px 1px #222; }

陰影範例二

p { text-shadow: 2px 2px 2px #222; }

陰影範例三

p { text-shadow: 1px 1px 5px #000; }

三個參數分別代表水平位移、垂直位移與模糊半徑,最後接顏色。若想做出更細膩的效果,可以改用 rgba 色彩,讓陰影邊緣自然融入背景。

結語

以上這些技巧只是入門。真正的排版功力來自不斷嘗試:同一段文字換不同的行高、寬度、字重去比較,你會慢慢建立自己的判斷。排版往往是使用者對一個網站最直接、也最長時間的接觸面,值得多花一點心思。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved