Navigation BarTutorials

How to create a responsive mobile navigation bar

How to create a responsive mobile navigation bar

How to create a responsive mobile navigation bar

Before We Start

A navigation bar is one of the main components of a website,It contains links to the other sections and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website is the navigation bar and it links to the other main parts.

In a website the navigation bar will be an important part and it helps the users to find the pages quickly If you have ever seen a website which doesn’t has a navigation bar, you may have found it’s not easy to locate the exact page what you need. So if you are a person who wishes to develop a website, you have to know how to create a navigation bar properly.

How to create a responsive mobile navigation bar

How to create a mobile responsive navigation bar ? We are here to solve your problem.
In this article we discuss how to create a responsive navigation bar. Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<html>
<body>
     <header id="main-header">
		<div class="container">
			<h1 id="logo"><a href="index.html">W3School <span>Web</span></a></h1>
			<input type="checkbox" id="nav-toggle" class="nav-toggle">
			<nav id="menu">
				<ul>
					<li><a href="#">Start</a></li>
					<li><a href="#">About</a></li>
          <li><a href="#">News</a></li>
          <li><a href="#">Portfolio</a></li>
					<li><a href="#">Contact</a></li>
				</ul>
			</nav>
			<label for="nav-toggle" class="nav-toggle-label">
				<span></span>
			</label>
		</div>
	</header>
</body>
</html>

Step 2 – Add CSS

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
  
*{
	margin: 0;
	padding: 0;
}

/* Just adding a font to all content */
body {
  height: 980px; 
  background: url(https://images.pexels.com/photos/169573/pexels-photo-169573.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
  background-position: bottom left;
  background-size: cover;
  background-repeat: no-repeat;
  font: 16px/1.2 'Montserrat', sans-serif;
}

/* CONTAINER */
.container {
	width: 80%; 
	margin: auto;
	overflow: hidden;
}

/* HEADER */
#main-header {
	background: #333;
	text-align: center;
	position: fixed; /* sticks to the top of the page, regardless of where you scroll */
	margin-bottom: 50px;
	width: 100%;
	z-index: 999; /* puts itself on top of all other elements on the page */
	padding: 20px 0;
}

/* LOGO - the h1 */
#logo {
	color: #fff;
	align-self: center;
}

#logo span {
	color: rgb(255, 0, 170);
}

#logo a{
	text-decoration: none;
	color: #fff;
}

#logo a:hover {
	color: coral;
}


/* hiding the checkbox */
.nav-toggle {
	display: none;
}

/* STYLING/POSITIONING THE HAMBURGER MENU */
.nav-toggle-label {
	position: absolute;
	top: 0;
	right: 0;
	margin-right: 1em;
	height: 100%;
	display: flex;
	align-items: center;
}

.nav-toggle-label span,
.nav-toggle-label span::before,
.nav-toggle-label span::after {
	display: block;
	background: #fff;
	height: 3px;
	width: 30px;
	border-radius: 2px;
	position: relative;
	transition: all .5s ease-in-out;
}

.nav-toggle-label span::before,
.nav-toggle-label span::after {
	content: '';
	position: absolute;
}

.nav-toggle-label span::before {
	bottom: 10px;
}
.nav-toggle-label span::after {
	top: 10px;
}

#nav-toggle:checked ~ .nav-toggle-label span {
	background: #333; 
}

/* transforming the before and after span into an X */
#nav-toggle:checked ~ .nav-toggle-label span::before {
	transform: rotate(45deg);
	bottom: 0;
	background: #fff;
}

#nav-toggle:checked ~ .nav-toggle-label span::after {
	transform: rotate(-45deg);
	top: 0;
	background: #fff;
}

#menu {
	position: absolute;
	background: #333;
	text-align: right;
	top: 100%; /* puts itself directly under the preceeding relative positioned element */
	left: 0;
	width: 100%;
	transform: scale(1, 0);
	transform-origin: top; /* menu appears from the top instead if center, try and comment this out and see for yourself */
	transition: transform .4s ease-in-out;
}

#menu ul {
	list-style: none;
}

#menu li {
	padding: 20px 10px;
}

#menu a {
	text-decoration: none;
	color: #fff;
	font-size: 18px;
	text-transform: uppercase;
	opacity: 0;
	transition: opacity .15s ease-in-out;
}

#menu a:hover {
	color: coral;
}

#nav-toggle:checked + #menu {
	transform: scale(1, 1);
}
#nav-toggle:checked ~ #menu a {
	opacity: 1;
	transition: opacity .25s ease-in-out .35s;
}
    </style>

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.

How to create a responsive mobile navigation bar

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button