善用 CSS 樣式表,只需修改單一檔案即可同步更新數百頁面外觀,極大提升維護效率。以下分享 8 個實戰技巧,助你發揮 CSS 最大效能。
瀏覽器依優先序處理:內聯 style > <style> 區塊 > @import > <link> 外部檔案。善用層疊特性,可在同一頁面靈活組合多種引入方式而不衝突。
使用 Dreamweaver 等視覺化工具邊寫邊測,完成後將 <head> 內的樣式區塊複製到獨立 .css 檔,再以 <link> 引入,即可快速產出可維護的外部樣式表。
body {
background: purple url(bg.jpg);
background-repeat: repeat-y;
background-attachment: fixed; /* 關鍵屬性 */
}
設定 background-attachment: fixed 即可讓背景圖固定在視窗,內容捲動時背景不動。
.indent { text-indent: 2em; } /* em 為相對單位,2em = 兩個字元寬度 */
套用於 <p> 或 <div>,<br> 不生效(僅換行非分段)。
定義類別 .bg-color { background: #cc00bb; } 或 .bg-img { background-image: url(test.gif); },在編輯器中選取文字套用類別即可,無需表格包裹。
.border-box {
border: solid;
border-width: thin 0 0 medium; /* 上 右 下 左 */
border-color: #0000FF black black #00FF00;
}
記得在 Style 選擇線型(預設 none 不顯示)。
a:link { text-decoration: none; color: blue; }
a:visited { color: red; text-decoration: line-through; }
a:active { color: white; text-decoration: underline; }
a:hover { text-decoration: none; color: #FF0000; background-color: black; }
依視覺需求調整,強化品牌辨識度。
同技巧 5,定義 .txt-bg { background-image: url(test.gif); } 套用於需區隔的文字段落。
這 8 招涵蓋版面、排版、互動、列印等實務場景,熟練掌握即能大幅提升 CSS 開發效率與網站維護彈性。