0
点赞
收藏
分享

微信扫一扫

自适应导航栏

A邱凌 2024-10-17 阅读 19

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Navbar</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            background-color: #f0f2f5;
        }
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #333;
            padding: 10px 20px;
            color: white;
        }
        .navbar a {
            color: white;
            text-decoration: none;
            padding: 10px;
            font-size: 1em;
        }
        .navbar a:hover {
            background-color: #575757;
            border-radius: 5px;
        }
        .menu {
            display: flex;
            gap: 15px;
        }
        .menu-toggle {
            display: none;
            cursor: pointer;
            font-size: 1.5em;
        }
        @media (max-width: 768px) {
            .menu {
                display: none;
                flex-direction: column;
                width: 100%;
                background-color: #333;
                position: absolute;
                top: 60px;
                left: 0;
                padding: 10px 20px;
                gap: 0;
            }
            .menu-toggle {
                display: block;
            }
            .menu.show {
                display: flex;
            }
        }
    </style>
</head>
<body>
    <header class="navbar">
        <div class="brand">MySite</div>
        <div class="menu-toggle" onclick="toggleMenu()">☰</div>
        <nav class="menu" id="menu">
            <a href="#">Home</a>
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Contact</a>
        </nav>
    </header>

    <script>
        function toggleMenu() {
            const menu = document.getElementById('menu');
            menu.classList.toggle('show');
        }
    </script>
</body>
</html>

举报

相关推荐

0 条评论