事实证明,可以有一个纯 CSS 的解决方案,它完全符合我的目标,即一个列表,当鼠标悬停在一个条目上时,它会使用一个子列表扩展该条目。我最糟糕的问题是我包含了“位置:绝对;” 在样式中,这会阻止包含元素扩展以容纳新显示的子列表。以下是一个完整的代码示例,已在 Chrome 和 Firefox 中测试并由 validome.org 验证:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<!-- DropDownExpandMenuExample2\TestWEMenus.html - 2012-09-01 -->
<head>
<title>Test of menu styles</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<meta name="author" content=" Bruce Bon "></meta>
<style type="text/css">
div.navcontainer{
font: 10pt/14pt 'Lucida Grande', Verdana, Helvetica, sans-serif;
background: transparent;
width: 220px;
margin-left: 0px;
/* position: absolute; top: 210px; left: 660px;*/
position: absolute; top: 20px; left: 660px;
padding-left: 10px;
padding-bottom: 20px;
color: #22657F;
z-index: 10; /* make sure in front */
border-left: 2px solid #BB77FF;
}
/* main topic styles */
ul#navmaintopiclist {
margin: 0;
margin-top: 15px;
padding: 0;
}
ul#navmaintopiclist li {
font-size: 14px;
padding-top: 5px;
padding-bottom: 5px;
font-weight: bold;
list-style: none;
}
ul#navmaintopiclist li a {
display: block;
text-decoration: none;
}
/* Add the link and hover effects */
ul#navmaintopiclist li:link {
color: #333388; text-decoration:none; }
ul#navmaintopiclist li:visited {
color: #6633AA; text-decoration:none; }
ul#navmaintopiclist li:hover {
color: #5555BB;
text-decoration: none;
}
ul#navmaintopiclist li:hover ul {
display: block;
}
/* Hide the subtopics, set subtopic size, etc */
ul.subtopiclist {
display: none;
background-color: #BBFFBB;
width: 180px; /* Width to help Opera out */
position: relative;
left: 0px;
}
ul#navmaintopiclist li ul li {
background: transparent;
list-style-type: none;
font-family: 'Lucida Grande', Verdana, Helvetica, sans-serif;
font-size: 14px;
letter-spacing: -1px;
font-weight: bold;
margin-left: -20px;
}
</style>
</head>
<body>
<div class="navcontainer">
<ul id='navmaintopiclist'>
<li><a href="index.html">Home</a></li>
<li><a href="index.html">Healthy Mind</a>
<ul class='subtopiclist'>
<li>Stress and Anxiety</li>
<li>Emotional Freedom</li>
<li>Self-Hypnosiss</li>
</ul>
</li>
<li><a href="index.html">Healthy Body</a>
<ul class='subtopiclist'>
<li>SubOne</li>
<li>SubOne</li>
<li>SubOne</li>
</ul>
</li>
</ul>
</div>
</body>
</html>