Home  • Programming • CSS

How to make responsive nav drawer using CSS and JavaScript.

doctype-html-lang-en
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
        }

        .container {
            width: 100%;
        }

        .row {
            display: flex;
            justify-content: center;
            width: 100%;
        }

        .row-1{
            box-shadow: 0 0 10px 10px rgba(0, 0, 0, .2);
        }

        .bar{          
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 1200px;
        }

        .search {
            padding: 5px;
            border-radius: 20px;
            border: 1px solid gray;
            width: 30%;
        }

        .drawer {}

        .menu {
            padding: 0;
            margin: 0;
            list-style: none;
            display: flex;
            align-items: center;
        }

        .menu li {
            position: relative;
        }

        .menu li a {
            text-decoration: none;
            display: block;
            padding: 8px;
        }


        .submenu {
            padding: 0;
            margin: 0;
            list-style: none;
            position: absolute;
            visibility: hidden;
            opacity: 0;
            transition: .2s ease-in-out;
            width: 200px;
            background-color: #fff;
            box-shadow: 0 0 1px 1px rgba(0, 0, 0, .2);

        }

        .menu li:hover .submenu {
            visibility: visible;
            opacity: 1;
        }

        .hamburger {
            display: none;
            position: absolute;  /*you can off it if you use close button on drawer*/          
        }

        .show {
            left: 0 !important;
            opacity: 1 !important;
        }

        /* Extra small devices (phones, 600px and down) */
        @media only screen and (max-width: 600px) {

            .search {
                display: inline;
                margin: 0px auto;
                width: 70%;
            }

            .drawer {
                position: fixed;
                display: block;
                background-color: #fff;
                width: 200px;
                left: -200px;
                top: 0;
                height: 100%;
                transition: .2s ease-in-out;
                opacity: 0;
                box-shadow: 1px 10px 5px 5px rgba(0, 0, 0, .2);
            }

            .menu {
                display: block;
                margin-top: 40px;
            }

            .hamburger {
                display: block;
                cursor: pointer;
                margin-left: 10px;
            }

            .menu li:hover .submenu {
                position: relative;
                /*important*/
                visibility: visible;
                opacity: 1;
            }

        }

        /* Small devices (portrait tablets and large phones, 600px and up) */
        @media only screen and (min-width: 600px) {}

        /* Medium devices (landscape tablets, 768px and up) */
        @media only screen and (min-width: 768px) {}

        /* Large devices (laptops/desktops, 992px and up) */
        @media only screen and (min-width: 992px) {}

        /* Extra large devices (large laptops and desktops, 1200px and up) */
        @media only screen and (min-width: 1200px) {}
    </style>
</head>

<body>
    <div class="container">
        <div class="row row-1">
            <div class="bar">                
                <div class="drawer">
                    <ul class="menu">
                        <li><a href="#">Home</a> </li>
                        <li><a href="#">Service</a>
                            <ul class="submenu">
                                <li><a href="#">Data Migration</a></li>
                                <li><a href="#">Software Support</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Product</a> </li>
                        <li><a href="#">About Us</a> </li>
                    </ul>
                </div>
                <span class="hamburger">&#x2630;</span>
                <input type="text" class="search" placeholder="Search..." />
            </div>
        </div>
    </div>
    <script>
        let ham = document.querySelector(".hamburger");
        let menu = document.querySelector(".drawer");

        ham.addEventListener("click", (e) => {
            menu.classList.toggle("show");
        });

        document.addEventListener('click', function (event) {
            const outsideClick = !menu.contains(event.target);
            const isHam = !ham.contains(event.target);
            if (outsideClick && isHam) {
                menu.classList.remove("show");
            }
        });

    </script>
</body>

</html>

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd