How to make CSS Responsive Navigation bar with Logo?

A responsive navigation bar (navbar) with a logo is a crucial element of any modern website. It not only helps users navigate your site but also reinforces your brand identity.

In this step-by-step guide, we’ll walk you through the process of creating a responsive navbar that includes a logo.

We’ll use HTML, CSS, and a bit of JavaScript to achieve this. By the end, you’ll have a stylish and functional navbar that adjusts to different screen sizes.

Prerequisites:

Before we start, make sure you have a basic understanding of HTML, CSS, and JavaScript. You can create this project using a simple text editor, but using an integrated development environment (IDE) like Visual Studio Code can streamline the process.

Making of the Responsive Navbar that Includes a Logo

Click the following button to see the demo of the responsive navbar that we are going to build –

Step 1: Setting Up the HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Responsive Navigation Bar - Devbabu.Com</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <header>
        <div class="wrapper">
            <div class="logo">
                <a href="#"><img src="logo.png" alt="DevBabu.com"></a>
            </div>
            <div class="navbar">
                <div class="close-nav"><button>×</button></div>
                <nav>
                    <ul>
                        <li><a href="#">Home</a></li>
                        <li><a href="#">Blog</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </nav>
            </div>
            <div class="menu-bar">
                <button><i></i></button>
            </div>
        </div>
    </header>
    <div class="container">
        <!-- Content -->
    </div>
    <script src="./script.js"></script>
</body>
</html>

In this HTML structure:

  • We include the necessary metadata, links to CSS and JavaScript files, and a title in the <head> section.
  • Inside the <div calss="wrapper"> element, we have a container with your logo, navigation links (<nav> » <ul>), and a menu toggle button.

Step 2: Styling with CSS

Let’s style the navbar and make it responsive using CSS. Create a file named style.css and add the following styles:

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap");
*,
*::before,
*::after {
    box-sizing: border-box;
    line-height: 1.5em;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    font-family: "Open Sans", sans-serif;
    background-color: 
#f7f7f7;
}

header {
    background-color: 
#ffffff;
    box-shadow: 0 10px 15px -3px 
rgba(0, 0, 0, 0.1),
        0 4px 6px -4px 
rgba(0, 0, 0, 0.1);
    padding: 10px 20px;
}
header .wrapper {
    max-width: 1000px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
}
header .logo img {
    height: 50px;
    display: block;
}

.navbar {
    position: fixed;
    top: 0;
    left: 100%;
    margin: 0;
    width: 100%;
    height: 100%;
    background-color: 
white;
    padding: 20px;
    transition: left 0.3s;
}

.navbar.show {
    left: 0 !important;
}

.hide-scroll {
    overflow: hidden;
}

.navbar ul {
    all: unset;
    list-style-type: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 25px;
}
.navbar ul a {
    all: unset;
    color: 
#444444;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: bold;
    font-size: 28px;
}
.navbar ul a:hover {
    color: 
#111111;
    text-decoration: underline overline;
    text-decoration-thickness: 3px;
}

.close-nav {
    text-align: right;
    margin-bottom: 20px;
}
.close-nav button {
    all: unset;
    background: 
#f7f7f7;
    font-size: 42px;
    cursor: pointer;
    border: 1px solid 
rgba(0, 0, 0, 0.2);
    padding: 15px;
    border-radius: 3px;
    color: 
#444444;
}
.close-nav button:hover {
    color: 
#222222;
    background: 
white;
}

.menu-bar button {
    border: 1px solid 
rgba(0, 0, 0, 0.1);
    background: 
#f7f7f7;
    height: 50px;
    width: 50px;
    padding: 5px 10px;
    cursor: pointer;
    border-radius: 3px;
}
.menu-bar i {
    display: block;
    border-top: 3px solid 
#444444;
    border-bottom: 3px solid 
#444444;
}
.menu-bar i::after {
    display: block;
    content: "";
    border-top: 3px solid 
#444444;
    margin: 6px 0;
}
.menu-bar button:hover {
    background: 
white;
}
.menu-bar button:hover i {
    border-color: 
#222222;
}

.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 30px;
}

@media (min-width: 550px) {
    .navbar {
        all: unset;
        display: block;
    }
    .navbar ul {
        flex-direction: row;
        gap: 20px;
    }
    .navbar ul a {
        font-size: inherit;
    }
    .close-nav,
    .menu-bar {
        display: none;
    }
}

In this CSS code:

  • We style the navbar’s background, text, and layout.
  • The logo and navigation links are styled, and we added a hover effect for links.
  • For smaller screens (550px width or less), we use a media query to hide the navigation links by default (display: none) and display them in a column when the menu toggle button is clicked (display: block).

Step 3: Implementing Responsive Behavior with JavaScript

To toggle the navigation links on smaller screens, we’ll use JavaScript. Create a file named script.js and add the following code:

const theBody = document.querySelector('body');
const openNav = document.querySelector('.menu-bar button');
const closeNav = document.querySelector('.close-nav button');
const Navbar = document.querySelector('.navbar');

// Hiding body scroll when mobile navigation menu opens
function bodyScroll() {
    if (Navbar.classList.contains('show')) {
        theBody.classList.add('hide-scroll');
    } else if (theBody.classList.contains('hide-scroll')) {
        theBody.classList.remove('hide-scroll');
    }
}

function showHide() {
    Navbar.classList.toggle('show');
    bodyScroll();
}

openNav.onclick = showHide;
closeNav.onclick = showHide;

Congratulations! You’ve successfully created a responsive navbar with a logo for your website. You can further customize the styles and functionality to match your website’s design and requirements.