「怎樣讓連結不要有底線?」「滑鼠移上去能不能變色?」這兩個問題幾乎每個新手都問過。用傳統 HTML 屬性做不到,但換成 CSS,幾行就解決,而且頁面碼不會變得臃腫。
在 <head> 與 </head> 之間寫上:
<style type="text/css">
<!--
a:link { text-decoration: none }
a:active { text-decoration: none }
a:visited { text-decoration: none }
-->
</style>
瀏覽器讀到這段就明白了:a:link 是尚未造訪過的連結,a:active 是正在被按下的那一瞬間,a:visited 則是已經去過的。
其中 text-decoration 就是文字裝飾效果,而 none 表示不加底線。這個屬性還有幾個兄弟值值得記住:
underline 加底線。overline 在文字上方加線。line-through 刪除線。blink 閃爍(支援度極差,現在請直接忘掉它)。範例:閃爍的粗體文字(此處以刪除線呈現)。程式碼只有一行:
B { text-decoration: line-through }
B { text-transform: uppercase }
與它相對的是 lowercase(全部小寫)與 capitalize(每個單字的首字母大寫)。這一組屬性的好處是:原始資料不必被改動,呈現交給樣式決定。
範例:既大寫、又有顏色、又帶刪除線的文字。
b { text-decoration: line-through; text-transform: uppercase; color: #66FFFF }
多個屬性之間以分號隔開,順序不拘。
先給三種狀態各自上色:
<style type="text/css">
<!--
a:link { text-decoration: none; color: green }
a:active { text-decoration: none; color: yellow }
a:visited { text-decoration: none; color: red }
-->
</style>
但這三個狀態都不會在「游標移上去」時觸發。要補上這塊,得動用第四個狀態 hover:
a:hover { text-decoration: none; color: yellow } 游標移入時去掉底線、變黃色。
a:hover { text-decoration: underline; color: green } 游標移入時出現底線、變綠色。
a:link { text-decoration: none; color: yellow; font-size: 9pt }
a:hover { text-decoration: underline; color: green; font-size: 12pt }
平時是黃色、無底線、9pt 的小字;游標一移上去,底線冒出、轉為綠色,字級跳到 12pt。因為尺寸變了,畫面上會有一種「忽然被拉近」的動感。這正是多屬性搭配的效果——單看每一行都很平凡,合起來就有了層次。
最後提醒:並非所有瀏覽器都完整支援上述屬性,上線前請在自己的目標瀏覽器實測一遍。