Responsive navbar CSS and JS

Last updated: June 6, 2025

What is response navbar?

A responsive navbar is a navigation menu on a website that automatically adjusts and adapts its layout and appearance based on the screen size and device being used. It is designed to provide an optimal user experience, ensuring that the navigation remains easily accessible and usable on different devices, such as desktops, tablets, and mobile phones. The responsive navbar utilizes CSS and JavaScript techniques to dynamically rearrange and resize the menu items, making it easier for users to navigate the website regardless of the device they are using.

 

Method of responsive navbar:

  1. CSS Media Queries: Utilize CSS media queries to define different styles and layout rules for different screen sizes. Adjust the navbar's appearance and behavior based on breakpoints to ensure responsiveness.
  2. Flexbox: Utilize CSS Flexbox properties to create a flexible and responsive navbar layout. Flexbox allows you to easily distribute and align navbar items, making them adapt to different screen sizes.
  3. CSS Grid: Implement CSS Grid to create a responsive grid-based navbar. CSS Grid enables you to create multiple columns and rows, allowing you to organize navbar elements effectively and responsively.
  4. JavaScript DOM Manipulation: Use JavaScript to dynamically manipulate the navbar elements based on the screen size. You can add event listeners, modify CSS classes, or change styles to make the navbar responsive.

 

In this article, we will create a responsive navbar using CSS and JavaScript. CSS Flexbox and JavaScript were used to create a responsive navbar. Flexbox will be used for responsiveness, and JavaScript will be used for handling click events on the hamburger menu.

 

Output:

Responsive navbar CSS and JS computer view
Responsive navbar CSS and JS mobile view

Getting start from html:

Here use fontawesome is used for icons.

<!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">
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
		integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
		crossorigin="anonymous" referrerpolicy="no-referrer" />
	<title>Responsive Navbar</title>

</head>

<body>
	<header>
		<div class="navbar">
			<div class="header-button">
				<label for="toggle_input" id="toggle_label"><i class="fa-solid fa-bars"></i></label>
                <input type="checkbox" id="toggle_input" value="false" onchange="toggle_button()">
                <img src="favicon.ico" alt="">
				<h1>OpenResume.com</h1>
			</div>
			<ul id="nav_ul">
				<li><a href="#">Home</a></li>
				<li><a href="#">Contact</a></li>
				<li><a href="#">Login</a></li>
                <form>
                    <input type="text" placeholder="Search..." name="search">
                </form>
			</ul>

		</div>
	</header>


</body>

</html>
*{
    margin: 0px;
    box-sizing: border-box;
    font-size: 20px;
}
header {
    padding: 0.5rem;
    background: #23384E;
}
header .navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
header .navbar .header-button {
    display: flex;
    align-items: center;
}

header .navbar h1 {
    color: white;
    font-size: 1.3rem;
    padding-left: 0.1rem;
}
header .navbar img {
    color: white;
    font-size: 1.3rem;
    padding-left: 0.3rem;
}
#nav_ul {
    display: flex;
    list-style: none;
}

header .navbar label {
    color: white;
    display: none;
}

.header-button input {
    display: none;
}

#nav_ul a {
    text-decoration: none;
    color: white;
    padding: 0.5rem;
}


#nav_ul form {
    margin-top: 0.3rem;
}
@media (max-width: 992px) {
    header .navbar {
        flex-direction: column;
        align-items: flex-start;
    }

    #nav_ul {
        flex-direction: column;
        padding-top: 0.3rem;
        display: none;
    }

    #nav_ul li {
        padding-top: 0.5rem;
    }

    header .navbar label {
        display: block;
    }
}
function toggle_button() {
			var checkbox = document.getElementById("toggle_input").checked
			console.log(checkbox)
			if (checkbox == true) {
				document.getElementById("toggle_label").innerHTML = '<i class="fa-solid fa-chevron-down"></i>'
				document.getElementById("nav_ul").style.display = "flex"
			} else {
				document.getElementById("toggle_label").innerHTML = '<i class="fa-solid fa-bars"></i>'
				document.getElementById("nav_ul").style.display = "none"
			}
}
window.onresize = (event) => {
			if (document.getElementsByTagName("body")[0].clientWidth > 992) {
				document.getElementById("toggle_input").checked = false
				document.getElementById("toggle_label").innerHTML = '<i class="fa-solid fa-bars"></i>'
				document.getElementById("nav_ul").style.display = "flex"
			}
			else {
				toggle_button()
			}
}

103 comments

By Online_pnka
Jan. 23, 2025, 12:16 a.m.
Explore the best online schools in Idaho, without leaving your house. Earn your diploma with online schools in Idaho, and open up new opportunities. Customize your education with online schools in Idaho, to fit your busy life. Connect with instructors and classmates through online schools in Idaho, to enhance your learning experience. Experience the advantages of online schools in Idaho, and thrive in your courses. Online Schools in Idaho [url=https://onlineschoolid6.com/]Online Schools in Idaho[/url] .
By 3D_grMr
Feb. 3, 2025, 8:29 a.m.
Удивительные образовательные средства с применением 3D-печати, для эффективного обучения. Как 3D-печать меняет образование: новые инструменты для учителей. Успешная реализация 3D-печати в образовательных целях: ключевые аспекты. Инновационные подходы в учебном процессе: 3D-печать и образовательные инструменты. 3D-печать как творческий инструмент в образовании: возможности и преимущества. Трансформация образовательной среды с помощью 3D-печати: новые горизонты обучения. Готовимся к будущему: обучение с использованием 3D-печати. Преобразование образовательных процессов с помощью 3D-технологий. Учебные пособия будущего: инновации с использованием 3D-печати. Практические примеры использования 3D-печати в образовании. 3D-печать в школьной программе: новые возможности для педагогов. 3D printing educational tools [url=http://habr.com/ru/companies/top3dshopinc/articles/792132/]http://habr.com/ru/companies/top3dshopinc/articles/792132/[/url] .
By Online_soet
March 7, 2025, 4:18 p.m.
Online schools in Illinois: the best options, features of. Best Online Schools in Illinois, without leaving home. Online education for children in Illinois, important points. What to pay attention to when choosing an online school, in Illinois, paying attention to. Advantages of learning in online schools in Illinois, affordable programs. All about virtual classrooms in Illinois, at your fingertips. Benefits of Online Education in Illinois, in modern times. How Online Schools are Changing Education in Illinois, for a successful life. Learning in online schools: process and results, interactive lessons. Review of online schools: pros and cons, for successful learning. What parents think about online education in Illinois, consider. Online School Programs in Illinois, for different levels of study. Tips for improving academic performance in online schools, for schoolchildren. Online courses as part of the Illinois Curriculum, pros and cons. How to Choose an Online School for High School Students in Illinois, prospects. Frequently Asked Questions about Online Schools in Illinois, for parents and students. How to choose the best online education in Illinois, for a successful career. Courses and programs for adults in online schools in Illinois, for a new start. Prospects for online schools in Illinois, and the education system / in the light of new technologies. Online Schools in Illinois [url=https://neools-illin.com]https://neools-illin.com[/url] .

Create new comment