使用纯 CSS 创建多级下拉菜单
使用纯 CSS 实现多级下拉菜单已经发展至今您提到的早期解决方案。这是一种现代方法,提供了时尚且可维护的实现:
此方法利用嵌套无序列表 (
以下代码片段提供了菜单的 CSS 样式:
.third-level-menu { position: absolute; top: 0; right: -150px; width: 150px; list-style: none; padding: 0; margin: 0; display: none; } .third-level-menu > li { height: 30px; background: #999999; } .third-level-menu > li:hover { background: #CCCCCC; } .second-level-menu { position: absolute; top: 30px; left: 0; width: 150px; list-style: none; padding: 0; margin: 0; display: none; } .second-level-menu > li { position: relative; height: 30px; background: #999999; } .second-level-menu > li:hover { background: #CCCCCC; } .top-level-menu { list-style: none; padding: 0; margin: 0; } .top-level-menu > li { position: relative; float: left; height: 30px; width: 150px; background: #999999; } .top-level-menu > li:hover { background: #CCCCCC; } .top-level-menu li:hover > ul { /* On hover, display the next level's menu */ display: inline; } .top-level-menu a /* Apply to all links inside the multi-level menu */ { font: bold 14px Arial, Helvetica, sans-serif; color: #FFFFFF; text-decoration: none; padding: 0 0 0 10px; /* Make the link cover the entire list item-container */ display: block; line-height: 30px; } .top-level-menu a:hover { color: #000000; }
菜单的 HTML 结构如下:
<ul class="top-level-menu"> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li> <a href="#">Offices</a> <ul class="second-level-menu"> <li><a href="#">Chicago</a></li> <li><a href="#">Los Angeles</a></li> <li> <a href="#">New York</a> <ul class="third-level-menu"> <li><a href="#">Information</a></li> <li><a href="#">Book a Meeting</a></li> <li><a href="#">Testimonials</a></li> <li><a href="#">Jobs</a></li> </ul> </li> <li><a href="#">Seattle</a></li> </ul> </li> <li><a href="#">Contact</a></li> </ul>
此方法可确保菜单保持响应并适应各种屏幕尺寸和设备方向。它还消除了对 JavaScript 的需求,使其与所有浏览器兼容。
以上是如何仅使用 CSS 创建多级下拉菜单?的详细内容。更多信息请关注PHP中文网其他相关文章!