← CSS Text Ellipsis 文本溢出省略号 没有下一篇了 →

CSS 下拉菜单

原创 2025-09-10 CSS 已有人查阅

CSS下拉菜单指南:创建专业级导航体验

下拉菜单基础概念

下拉菜单是现代网站导航系统中不可或缺的组件,它能够有效地组织和展示多层级的导航选项。通过合理的CSS实现,可以创建出既美观又功能完善的下拉菜单,而无需依赖JavaScript。

基础下拉菜单实现

简单垂直下拉菜单

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基础垂直下拉菜单 - 代码号编程示例</title>
    <style>
        /* 重置样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f8f9fa;
            padding: 20px;
        }
        
        /* 导航容器 */
        .code-nav {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            border-radius: 8px;
            max-width: 800px;
            margin: 0 auto;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
        
        /* 导航列表 */
        .code-nav ul {
            list-style: none;
            position: relative;
        }
        
        /* 一级菜单项 */
        .code-nav > ul > li {
            display: inline-block;
        }
        
        /* 菜单链接样式 */
        .code-nav a {
            display: block;
            color: white;
            text-decoration: none;
            padding: 15px 25px;
            transition: all 0.3s ease;
        }
        
        /* 菜单项悬停效果 */
        .code-nav li:hover > a {
            background-color: rgba(255, 255, 255, 0.1);
        }
        
        /* 二级菜单 */
        .code-nav ul ul {
            position: absolute;
            background-color: white;
            border-radius: 0 0 8px 8px;
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
            min-width: 200px;
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
        }
        
        /* 二级菜单项 */
        .code-nav ul ul li {
            display: block;
            border-bottom: 1px solid #f1f1f1;
        }
        
        /* 二级菜单链接 */
        .code-nav ul ul a {
            color: #333;
            padding: 12px 20px;
        }
        
        /* 二级菜单链接悬停 */
        .code-nav ul ul a:hover {
            background-color: #f8f9fa;
            color: #2575fc;
        }
        
        /* 显示下拉菜单 */
        .code-nav li:hover > ul {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        
        /* 三级菜单定位 */
        .code-nav ul ul ul {
            left: 100%;
            top: 0;
            border-radius: 8px;
        }
        
        .page-title {
            text-align: center;
            margin: 30px 0;
            color: #2c3e50;
        }
        
        .page-description {
            text-align: center;
            margin-bottom: 40px;
            color: #7f8c8d;
        }
    </style>
</head>
<body>
    <h1 class="page-title">代码号编程导航菜单示例</h1>
    <p class="page-description">使用纯CSS实现的多级下拉菜单,无需JavaScript</p>
    
    <nav class="code-nav">
        <ul>
            <li><a href="#">首页</a></li>
            <li>
                <a href="#">前端教程</a>
                <ul>
                    <li><a href="#">HTML5基础</a></li>
                    <li><a href="#">CSS3进阶</a></li>
                    <li>
                        <a href="#">JavaScript实战</a>
                        <ul>
                            <li><a href="#">基础语法</a></li>
                            <li><a href="#">DOM操作</a></li>
                            <li><a href="#">异步编程</a></li>
                        </ul>
                    </li>
                    <li><a href="#">响应式设计</a></li>
                </ul>
            </li>
            <li>
                <a href="#">后端课程</a>
                <ul>
                    <li><a href="#">Node.js</a></li>
                    <li><a href="#">Python</a></li>
                    <li><a href="#">数据库</a></li>
                </ul>
            </li>
            <li><a href="#">项目实战</a></li>
            <li><a href="#">关于我们</a></li>
        </ul>
    </nav>
</body>
</html>

水平导航下拉菜单

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水平下拉菜单 - 代码号编程示例</title>
    <style>
        /* 基础样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
        }
        
        .code-header {
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }
        
        /* 水平导航菜单 */
        .horizontal-nav {
            display: flex;
            justify-content: center;
        }
        
        .horizontal-nav > li {
            position: relative;
        }
        
        .horizontal-nav a {
            display: block;
            padding: 18px 25px;
            color: #2c3e50;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .horizontal-nav li:hover > a {
            color: #3498db;
            background-color: #f8f9fa;
        }
        
        /* 下拉菜单 */
        .horizontal-nav ul {
            position: absolute;
            background: white;
            min-width: 200px;
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
            border-radius: 0 0 8px 8px;
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
            z-index: 1000;
        }
        
        .horizontal-nav li:hover > ul {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        
        .horizontal-nav ul li {
            display: block;
            border-bottom: 1px solid #f1f1f1;
        }
        
        .horizontal-nav ul a {
            padding: 12px 20px;
            color: #555;
        }
        
        .horizontal-nav ul a:hover {
            background-color: #3498db;
            color: white;
        }
        
        /* 三级菜单 */
        .horizontal-nav ul ul {
            left: 100%;
            top: 0;
            border-radius: 8px;
        }
        
        .demo-title {
            text-align: center;
            margin: 30px 0;
            color: #2c3e50;
        }
        
        .demo-description {
            text-align: center;
            margin-bottom: 40px;
            color: #7f8c8d;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="demo-title">代码号编程水平导航菜单</h1>
        <p class="demo-description">纯CSS实现的水平方向多级下拉菜单,适合网站主导航</p>
        
        <header class="code-header">
            <nav>
                <ul class="horizontal-nav">
                    <li><a href="#">首页</a></li>
                    <li>
                        <a href="#">学习路径</a>
                        <ul>
                            <li><a href="#">前端开发</a></li>
                            <li><a href="#">后端开发</a></li>
                            <li><a href="#">移动开发</a></li>
                            <li><a href="#">数据科学</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">技术栈</a>
                        <ul>
                            <li>
                                <a href="#">前端技术</a>
                                <ul>
                                    <li><a href="#">React教程</a></li>
                                    <li><a href="#">Vue.js指南</a></li>
                                    <li><a href="#">Angular实战</a></li>
                                </ul>
                            </li>
                            <li>
                                <a href="#">后端技术</a>
                                <ul>
                                    <li><a href="#">Node.js</a></li>
                                    <li><a href="#">Django</a></li>
                                    <li><a href="#">Spring Boot</a></li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                    <li><a href="#">项目实战</a></li>
                    <li><a href="#">社区</a></li>
                    <li><a href="#">关于</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>

高级下拉菜单效果

带图标的导航菜单

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图标下拉菜单 - 代码号编程示例</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .code-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            width: 100%;
            max-width: 900px;
            overflow: hidden;
        }
        
        /* 图标导航菜单 */
        .icon-nav {
            display: flex;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .icon-nav > li {
            position: relative;
        }
        
        .icon-nav a {
            display: flex;
            align-items: center;
            padding: 18px 25px;
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .icon-nav a i {
            margin-right: 10px;
            font-size: 18px;
        }
        
        .icon-nav li:hover > a {
            background-color: rgba(255, 255, 255, 0.15);
        }
        
        /* 下拉菜单 */
        .icon-nav ul {
            position: absolute;
            background: white;
            min-width: 220px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            border-radius: 0 0 8px 8px;
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
            z-index: 1000;
        }
        
        .icon-nav li:hover > ul {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        
        .icon-nav ul li {
            display: block;
            border-bottom: 1px solid #f1f1f1;
        }
        
        .icon-nav ul a {
            color: #555;
            padding: 14px 20px;
        }
        
        .icon-nav ul a:hover {
            background-color: #667eea;
            color: white;
        }
        
        .icon-nav ul a i {
            color: #667eea;
        }
        
        .icon-nav ul a:hover i {
            color: white;
        }
        
        /* 内容区域 */
        .content {
            padding: 40px;
            text-align: center;
        }
        
        .content h1 {
            color: #2c3e50;
            margin-bottom: 15px;
        }
        
        .content p {
            color: #7f8c8d;
            line-height: 1.6;
            max-width: 600px;
            margin: 0 auto;
        }
        
        .title {
            margin-bottom: 30px;
            text-align: center;
            color: #2c3e50;
        }
        
        .subtitle {
            color: #7f8c8d;
            margin-bottom: 40px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号编程图标导航菜单</h1>
    <p class="subtitle">带有Font Awesome图标的多级下拉菜单示例</p>
    
    <div class="code-card">
        <nav>
            <ul class="icon-nav">
                <li>
                    <a href="#"><i class="fas fa-home"></i>首页</a>
                </li>
                <li>
                    <a href="#"><i class="fas fa-book"></i>学习资源</a>
                    <ul>
                        <li>
                            <a href="#"><i class="fas fa-code"></i>前端开发</a>
                            <ul>
                                <li><a href="#"><i class="fab fa-html5"></i>HTML/CSS</a></li>
                                <li><a href="#"><i class="fab fa-js-square"></i>JavaScript</a></li>
                                <li><a href="#"><i class="fab fa-react"></i>React</a></li>
                            </ul>
                        </li>
                        <li>
                            <a href="#"><i class="fas fa-server"></i>后端开发</a>
                            <ul>
                                <li><a href="#"><i class="fab fa-node-js"></i>Node.js</a></li>
                                <li><a href="#"><i class="fab fa-python"></i>Python</a></li>
                                <li><a href="#"><i class="fas fa-database"></i>数据库</a></li>
                            </ul>
                        </li>
                        <li><a href="#"><i class="fas fa-mobile-alt"></i>移动开发</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fas fa-project-diagram"></i>实战项目</a>
                    <ul>
                        <li><a href="#"><i class="fas fa-laptop-code"></i>网页开发</a></li>
                        <li><a href="#"><i class="fas fa-mobile"></i>应用开发</a></li>
                        <li><a href="#"><i class="fas fa-gamepad"></i>游戏开发</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fas fa-users"></i>社区</a>
                </li>
                <li>
                    <a href="#"><i class="fas fa-info-circle"></i>关于</a>
                </li>
            </ul>
        </nav>
        
        <div class="content">
            <h1>欢迎来到代码号编程</h1>
            <p>探索前端开发的无限可能,学习最新的Web技术,构建出色的用户体验。</p>
        </div>
    </div>
</body>
</html>

响应式下拉菜单

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>响应式下拉菜单 - 代码号编程示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .page-header {
            text-align: center;
            margin: 30px 0;
            color: #2c3e50;
        }
        
        .page-description {
            text-align: center;
            margin-bottom: 40px;
            color: #7f8c8d;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
        
        /* 响应式导航 */
        .responsive-nav {
            background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
        
        .responsive-nav ul {
            list-style: none;
            display: flex;
            justify-content: center;
        }
        
        .responsive-nav > ul > li {
            position: relative;
        }
        
        .responsive-nav a {
            display: block;
            color: white;
            text-decoration: none;
            padding: 18px 25px;
            transition: all 0.3s ease;
        }
        
        .responsive-nav li:hover > a {
            background-color: rgba(255, 255, 255, 0.1);
        }
        
        /* 下拉菜单 */
        .responsive-nav ul ul {
            position: absolute;
            background-color: white;
            min-width: 200px;
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
            border-radius: 0 0 8px 8px;
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
            z-index: 1000;
            flex-direction: column;
        }
        
        .responsive-nav li:hover > ul {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        
        .responsive-nav ul ul li {
            display: block;
            border-bottom: 1px solid #f1f1f1;
        }
        
        .responsive-nav ul ul a {
            color: #555;
            padding: 14px 20px;
        }
        
        .responsive-nav ul ul a:hover {
            background-color: #43cea2;
            color: white;
        }
        
        /* 移动端样式 */
        @media (max-width: 768px) {
            .responsive-nav ul {
                flex-direction: column;
            }
            
            .responsive-nav > ul > li {
                width: 100%;
                border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            }
            
            .responsive-nav ul ul {
                position: static;
                opacity: 1;
                visibility: visible;
                transform: none;
                box-shadow: none;
                background-color: rgba(255, 255, 255, 0.1);
                display: none;
            }
            
            .responsive-nav li:hover > ul {
                display: block;
            }
            
            .responsive-nav ul ul a {
                padding-left: 40px;
            }
        }
        
        /* 演示内容 */
        .demo-content {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 40px;
        }
        
        .demo-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .demo-card h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }
        
        .demo-card p {
            color: #7f8c8d;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="page-header">代码号编程响应式下拉菜单</h1>
        <p class="page-description">适配桌面和移动设备的纯CSS下拉菜单解决方案</p>
        
        <nav class="responsive-nav">
            <ul>
                <li><a href="#">首页</a></li>
                <li>
                    <a href="#">前端开发</a>
                    <ul>
                        <li><a href="#">HTML5基础</a></li>
                        <li><a href="#">CSS3进阶</a></li>
                        <li><a href="#">JavaScript实战</a></li>
                        <li><a href="#">响应式设计</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#">后端技术</a>
                    <ul>
                        <li><a href="#">Node.js</a></li>
                        <li><a href="#">Python</a></li>
                        <li><a href="#">数据库</a></li>
                    </ul>
                </li>
                <li><a href="#">项目实战</a></li>
                <li><a href="#">关于我们</a></li>
            </ul>
        </nav>
        
        <div class="demo-content">
            <div class="demo-card">
                <h3>HTML5与CSS3</h3>
                <p>学习现代网页开发的基础技术,掌握语义化标签和高级样式技巧。</p>
            </div>
            <div class="demo-card">
                <h3>JavaScript编程</h3>
                <p>从基础语法到高级概念,详细掌握前端开发的核心语言。</p>
            </div>
            <div class="demo-card">
                <h3>响应式设计</h3>
                <p>创建适配各种设备的网页布局,提供一致的用户体验。</p>
            </div>
        </div>
    </div>
</body>
</html>

本节课程知识要点

  1. HTML结构基础:使用语义化标签构建菜单层次结构

  2. CSS定位技术:掌握absolute和relative定位实现下拉效果

  3. 过渡动画应用:使用transition属性创建平滑的显示/隐藏效果

  4. 选择器高级用法:利用子选择器和相邻选择器精确控制菜单行为

  5. 响应式设计:使用媒体查询适配不同屏幕尺寸

  6. 视觉层次设计:通过阴影、颜色和间距创建清晰的视觉层次

  7. 交互反馈:使用:hover状态提供用户操作反馈

  8. 可访问性考虑:确保菜单对键盘导航和屏幕阅读器友好

← CSS Text Ellipsis 文本溢出省略号 没有下一篇了 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号