以下範例示範如何製作滑鼠滑過時出現的提示框(tooltip)效果。它主要透過 CSS 設定提示框的外觀與背景圖,並利用 :hover 呈現浮動提示:
body{font: 76%/1.5 Arial,sans-serif;background: #FFF;color: #333}
div#container{width: 500px;margin:0 auto}
a{color: #F60;font-weight:bold}
a:hover{color: #F00}
.tooltip{ width: 200px; color:#000; font:lighter 11px/1.3 Arial,sans-serif; text-decoration:none;text-align:center}
.tooltip span.top{padding: 30px 8px 0; background: url(https://www.example.com/images/tooltip.gif) no-repeat top}
.tooltip b.bottom{padding:3px 8px 15px;color: #548912; background: url(https://www.example.com/images/tooltip.gif) no-repeat bottom}
透過為提示框的上下兩部分分別設定背景圖(top 與 bottom),再搭配適當的 padding,就能做出帶有對話框造型的提示效果。將 .tooltip 相關結構與滑鼠事件結合,即可在使用者把游標移到連結上時顯示說明文字,移開後自動隱藏。