The Celebration Mall, Amritsar

Responsive navigation bar in html css and javascript

A simple way to design a responsive navigation bar for mobile in HTML and CSS with show hide bars in JavaScript. Best and simple javascript show hide code. Languages- HTML, CSS, JavaScript.

<html>
    <head>
        <title>My Page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    </head>
    <body>
        <a onclick="nav()"><i class=" navBtn fa fa-bars"></i></a>
        <nav id="nav">
            <a>Home</a>
            <a>News</a>
            <a>Contact</a>
            <a>About</a>
        </nav>
    </body>
</html>
<style>
    body{
        margin: 0px auto;
    }
    nav{
        width: 100%;
        margin: 0px auto;
        background-color: #046ec9;
        height: 35px;
        text-align: center;
        }
    nav a{
            background-color: white;
            color: black;
            width: 23%;
            float: left;
            margin: 0% 1%;
            margin-top: 9px;
            }
    nav a:hover{
            background-color: black;
            color: white;}
    .navBtn{display:none;}
    @media only screen and (max-width:600px){
    nav a{
        width: 98%;
        }
    nav {
        height: 117px;
        }
    .navBtn{display:block;}
    #nav{display:none;}
    }
</style>
<script>
function nav(){
    var x = document.getElementById("nav");
    if(x.style.display === "block"){
        x.style.display = "none";
    }
    else{
        x.style.display = "block";
    }
}
</script>

Leave feedback about this

  • Quality
  • Price
  • Service
Choose Image