常有朋友問:如何讓超連結文字不出現下劃線?如何讓滑鼠移到連結上時產生變色效果?用傳統方法難以辦到,但透過 CSS 卻能輕鬆實現,而且頁面代碼不會臃腫。
只要在 <head> 與 </head> 之間加入以下 CSS:
<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 }。
同時產生大寫、顏色與刪除線效果:b { text-decoration: line-through; text-transform: uppercase; color: #66FFFF}。此外,lowercase 可讓字母全小寫,capitalize 可讓每個單字的首字母大寫。
若為連結的不同狀態設定不同顏色:
<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 },就能讓連結在滑鼠移經時不僅變色,字體也會放大,增添動感。
最後提醒:並非所有瀏覽器都支援全部屬性,建議在多重瀏覽器上測試。