「用 CSS 做下拉選單」在 2000 年代中期是個熱門題目,因為它不需要 JavaScript,也就不會遇到「使用者禁用 JS 就整個導覽消失」的問題。這份範例的巧妙之處在於:它用了一段不合法的 HTML(把 <table> 放進 <a> 裡面)來換取 IE 的相容性,再用條件註解把兩套寫法分流。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.1//EN"
"https://www.w3.org/TR/xhtml11/DTD/xHtml11.dtd"
[ <!ELEMENT a (#PCDATA | table)* > ]>
<html XMLns="https://www.w3.org/1999/xHtml" XML:lang="en" >
<head>
<meta http-equiv="Content-Type" content="application/xHtml+XML; charset=UTF-8" />
<title>Drop Down Menu</title>
<style type="text/css">
body { font-size: 1%; color: #fff; } /* 避開 IE 會列印 doctype 結尾的 bug */
.menu { display: none; }
.holder {
color: #000; width: 90px; height: 18px; display: block;
background: #dca; border: 1px solid #000; margin-right: 1px;
text-align: center; float: left; text-decoration: none;
font-family: tahoma, vardana, arial, sans-serif;
font-size: 10px; line-height: 18px; overflow: hidden;
}
.holder:hover { height: auto; cursor: pointer; color: #fff; background: #000; }
a.inner, a.inner:visited {
display: block; width: 89px; height: 18px; border-bottom: 1px solid #000;
text-decoration: none; color: #000; background: #eee;
}
a.inner:hover { background: #add; }
p { color: #000; font-size: 16px; }
</style>
<!--[if IE]>
<style type="text/css">
.holder { display: none; }
.menu { display: block; }
a.outer, a.outer:visited {
color: #000; width: 90px; height: 18px; display: block;
background: #dca; border: 1px solid #000; margin-right: 1px;
text-align: center; float: left; text-decoration: none;
font-family: tahoma, vardana, arial, sans-serif;
font-size: 10px; line-height: 18px; overflow: hidden;
}
a.outer:hover { color: #fff; background: #000; overflow: visible; }
a.outer:hover table { display: block; background: #eee; border-collapse: collapse; }
a.inner, a.inner:visited {
display: block; width: 88px; height: 18px; border-bottom: 1px solid #000;
text-decoration: none; color: #000; text-align: center;
}
a.inner:hover { background: #add; }
</style>
<![endif]-->
</head>
<body>
<div class="menu">
<a class="outer" href="page1.html">MENU 1
<table><tr><td>
<a class="inner" href="page1a.html">Page 1a</a>
<a class="inner" href="page1b.html">Page 1b</a>
<a class="inner" href="page1c.html">Page 1c</a>
<a class="inner" href="page1d.html">Page 1d</a>
<a class="inner" href="page1e.html">Page 1e</a>
</td></tr></table>
</a>
<a class="outer" href="page2.html">MENU 2
<table><tr><td>
<a class="inner" href="page2a.html">Page 2a</a>
<a class="inner" href="page2b.html">Page 2b</a>
<a class="inner" href="page2c.html">Page 2c</a>
<a class="inner" href="page2d.html">Page 2d</a>
</td></tr></table>
</a>
</div>
<div class="holder">
MENU 1<br />
<a class="inner" href="page1a.html">Page 1a</a>
<a class="inner" href="page1b.html">Page 1b</a>
<a class="inner" href="page1c.html">Page 1c</a>
<a class="inner" href="page1d.html">Page 1d</a>
<a class="inner" href="page1e.html">Page 1e</a>
</div>
<div class="holder">
MENU 2<br />
<a class="inner" href="page2a.html">Page 2a</a>
<a class="inner" href="page2b.html">Page 2b</a>
<a class="inner" href="page2c.html">Page 2c</a>
<a class="inner" href="page2d.html">Page 2d</a>
</div>
</body>
</html>
標準瀏覽器版(.holder):容器高度固定 18px 並設 overflow: hidden,所以子連結被藏起來;滑過時 height: auto 搭配 overflow 解除,整串就攤開來。原理乾淨,缺點是 IE 6 不支援 :hover 於非 a 元素,也不會因內容變高而撐開固定高度的容器。
IE 版(.menu + a.outer):把 <table> 塞進 <a> 裡面,滑過時 overflow: visible 讓 table 溢出顯示。這招能動,是因為 IE 允許 a 裡面放任意內容,也允許 :hover 出現在 a 上。
而 DOCTYPE 裡那段 [ <!ELEMENT a (#PCDATA | table)* > ] 是關鍵:它在 DTD 內部子集重新宣告 a 可以包含 table,讓這份文件在 技術上通過 XHTML 1.1 驗證。這是一個非常聰明、也非常病態的做法——用改寫規則的方式,讓不合法的結構變成「合法」。
<a> 永遠是錯的。 選單項目本身是一個連結,子項目又是各自獨立的連結,把它們包在同一個 <a> 裡,等於連結包連結。瀏覽器解析時會自動把內層的 a 拆出來,DOM 結構跟你寫的完全不一樣,JavaScript 與樣式都會出現難以預料的結果。<!--[if IE]>,那段樣式完全不會被套用。:hover 只回應滑鼠。用 Tab 鍵移到選單上時什麼都不會發生,子項目永遠看不到。這是純 CSS 下拉選單最根本的無障礙障礙。font-size: 1% 那行 hack 是為了壓掉 IE 會在頁面頂端印出 DOCTYPE 結尾字元的 bug,現在只是讓文字變得極小。<nav>
<ul class="menu">
<li>
<a href="/p1">產品</a>
<ul class="submenu">
<li><a href="/p1a">項目 A</a></li>
<li><a href="/p1b">項目 B</a></li>
</ul>
</li>
</ul>
</nav>
<style>
.submenu { display: none; list-style: none; margin: 0; padding: 0; }
li:hover > .submenu,
li:focus-within > .submenu { display: block; } /* focus-within 讓鍵盤也能展開 */
</style>
:focus-within 是這裡的英雄:當焦點落在子選單裡的任何元素上時,父層也會維持展開,鍵盤使用者就能正常操作。若需要點擊切換(行動裝置上 hover 不存在),再補上幾行 JavaScript 控制 hidden 屬性即可。結構用巢狀 ul,輔具會自動朗讀成「產品,子選單,3 個項目」,這是任何 table hack 都給不了的。