CSS(Cascading Style Sheets,層疊樣式表)讓 HTML 結構與表現分離,少量代碼即可賦予網頁豐富視覺效果。本文以經典範例快速掌握核心語法。
選擇器 {
屬性1: 值1;
屬性2: 值2;
...
}
/* 1. 文字基本樣式 */
p { font-family: "微軟正黑體"; font-size: 9pt; line-height: 12pt; color: #000; }
td { font-family: "微軟正黑體"; font-size: 9pt; }
/* 2. 連結四狀態(LVHA 順序不可亂) */
a:link { color: #000; text-decoration: none; } /* 未訪問 */
a:visited { color: gray; text-decoration: none; } /* 已訪問 */
a:hover { background: #ffccff; color: #0080ff; text-decoration: underline; } /* 滑鼠懸停 */
a:active { color: green; text-decoration: none; } /* 點擊瞬間 */
| 屬性 | 說明 | 常用值 |
|---|---|---|
| font-family | 字體系列,落後備選 | "微軟正黑體", sans-serif |
| font-size | 字號 | 12px, 0.875rem, 9pt |
| line-height | 行高 | 1.5, 150%, 24px |
| color | 前景色(文字色) | #333, rgb(51,51,51) |
| text-decoration | 文字修飾線 | none, underline, overline, line-through |
| background-color | 背景色 | #ffccff, transparent |
Link → Visited → Hover → Active。順序錯誤會導致狀態被覆蓋(如 :hover 寫在 :link 前,未訪問連結永不顯示 hover 樣式)。
<!-- 外部檔案,可快取、多頁共用 -->
<link rel="stylesheet" href="style.css">
<!-- 頁面內嵌,僅限單頁特殊樣式 -->
<style> p { color: #333; } </style>
<!-- 內聯樣式,優先級最高,慎用 -->
<p style="color:red;">紅字</p>
. 開頭(.box),ID 以 # 開頭(#header)。font-family: "Microsoft JhengHei";/* 註釋內容 */。