Ever struggled with a website's menu that just won't cooperate on your phone or tablet? Creating a responsive navigation menu is the key to solving this puzzle. In this article, I'll break down the steps of crafting a responsive navigation menu, to ensure your website looks great and works smoothly on any device. Let's dive in!
Let the fun begin! We will start with making a general navigation menu. We will be using simple HTML, CSS, and JavaScript. So, let's go!
The menu that helps you navigate to products and services pages from
the top of a web page is a navigation Menu.
For example, here is the navigation menu of GitHub:
To help your clients navigate to your products and services pages. Make it responsive so it can serve its purpose better.
We list products and services on our navigation menu. Assume your
website name is XYZ. You sell product 1 and product 2.
Your website name will remain unchanged in responsive situations. But
the appearance of your products and services may change. So, always
use a different class for elements that may change in appearance. And
keeping all of them inside a div element like this:
See the Pen image1 by Ayan Mallik (@Ayan-Mallik) on CodePen.
looks ugly? I know. Let's add some CSS now to make it look decent enough to use on your website.
Look into the Code above.
".wholesection" div has two children div elements. Let's make it
display flex in the CSS. Also, the anchor tags (the nav links) need
some styling. I have set "pink" as the background color here. Feel
free to change it.
See the Pen image 2 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Looks better now! But there is a problem. The navigation menu is not responsive. On a mobile screen, all the elements will come too close to each other.
Responsiveness is one of the biggest concerns of web development. Always ensure that the menu items are visible in all screen sizes. That will improve your user experience score and improve your SEO ranking.
Smartphones have the smallest screen width (up to 430px or smaller).
Then, comes tablets and iPads (bigger than 430px and smaller than
1024px). Then, there is the desktop screen (generally bigger than
1024px and can be up to 3840px).
The most popular Smartphone screen size is 360px X 800px (width X
height). The most popular tablet screen size is 768px by 1024px. The
most popular desktop screen size is 1920px by 1080px. Check out the
screen resolution stats by StatCounter
for additional information.
We will use media queries to make it responsive. Media queries will trigger different CSS rules for different screen sizes.
Tablet and iPad screen width ranges between 431px and 1024px. Let's make one media query for tablet screen sizes. I will make bigger space in between the products. For that, I have to declare the minimum screen width and maximum screen width in the media queries. See how I write the media queries rule in the CSS.
See the Pen image 3 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Inside the CodePen preview section,
As you can see, The media query has overwritten the initial flex gap value of 10px with a 20px gap.
Desktop screen size typically ranges between 1025px to 3840px. That's a pretty long range. Thus, I suggest you use multiple media queries. For desktop screen, you may want to do the following:
Let's do that!
See the Pen image 4 by Ayan Mallik (@Ayan-Mallik) on CodePen.
I have used two media queries here.
When the media query triggers, it still shows flex.
But, media queries overwritten the gap value for the desktop screen.
Here comes the juicy part.
To make the navbar responsive on mobile devices; you will need to do
the following.
We will achieve all these functionalities with HTML, CSS, and JAVASCRIPT. So, let's go!
Inside the code,
the div element with the .menus class shows the
menus. Simply change the flex-direction from row to column. That will
make it appear as a mobile navbar. We will increase the width as well.
We will give a higher Z-index value. That will make it appear above
the other contents on the web page.
Before that, we need to give the div element a fixed position.
Why fixed position?
It's because we want to position it from the
position of the viewport. Giving a fixed position cuts the element
from normal HTML flow. So, we can customize the appearance of the div
element much more effectively.
It will be preciously a fixed pixel far from the viewport!
See the Pen image 5 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Check the "Result" section.
We have successfully transformed the navigation menu for a mobile
screen!
As you can see, the mobile menu bar is visible without having to click on any hamburger button. So, we will transform the mobile menu by 100% as per the X-axis. This will hide the mobile menu on the right side. (because I have set the HTML overflow-X to "hidden")
See the Pen image 6 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Now, the navbar is hidden on a mobile screen!
Now, we need a hamburger button to access the mobile navbar.
Just in case you are wondering, here is the hamburger button of GitHub
on a mobile screen:
The hamburger button generally has a fixed top-right position on the mobile viewport. In the next section, we will be creating one!
The hamburger button contains three div elements. They are placed column-wise with some gap in between them. We will use HTML and CSS to make it.
We will make a div called hamburger. We will create three child div elements inside of the parent div element.
See the code below:
See the Pen image 7 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Do you see the hamburger button?
It's on the right side now!
Now;
Up to now, we only have been able to place the hamburger button. However, four functionalities are missing. Those are:
Whenever we add functionality, we need some JavaScript. So, connect your HTML page with your empty external JavaScript page first. Now we will write some JavaScript code.
Write some attribute to those child div elements of the hamburger button. We want to change those attribute data with JavaScript for the functionality.
So, each time "click" happens, the attribute data will toggle between "on" and "off" with JavaScript.
Write CSS for the child div elements with [ rotation= "on"]. Because,
We will rotate the top and bottom child div elements. Also, write CSS
for the middle child div element with [ display= "off"]. Because, we
will hide the middle div once the "click" happens on the hamburger
button. This is how we will make that "X" sign.
Now check out my
code:
See the Pen image 8 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Now, click the hamburger button inside the "result" and see what
happens.
Do you see the "X" sign?
Good!
Now, the hamburger button is rotating. It looks fancy but does not show the mobile navigation menu. We will need to add this little functionality using a small JavaScript code.
The div element with the .menus class shows the navigation menu. Previously, we gave it the transform rule in the CSS. However, we want to undo the transform rule once we click the hamburger button. For that, we will give that div element a new attribute and attribute value as we did earlier.
I have saved that div element as a Constant named "navigationMenu" in
JavaScript.
Inside the " eventListener ",
after the "click" happens, we will change the attribute data of the
hamburger button. We will do that using the "setAttribute"
functionality.
We will write CSS rule of the ".menus" div with its new attribute data. In the CSS rule, we will undo the transform rule.
See the Pen image 9 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Now, try clicking on the hamburger button. That will bring the mobile
menu to the screen.
Did you notice?
I did not write additional JavaScript code to
close the mobile navigation menu. But it is working fine. That is
because I used the "if-else" statement in the JavaScript.
So, when the navigation menu is visible, it has the attribute data
for the "mobile view" set to "on". If you click again, the "if-else"
statement will change the attribute data to "off" again. Once it is
"off", the general CSS will be applicable.
A navigation bar with a fixed position as you scroll down the web page is called a "sticky" navigation bar. A "sticky" navigation bar remains visible on the screen during your web session. I give you my example. The navigation bar of my portfolio is the "sticky" one.
There are multiple ways to do that.
But the simplest way is by only using the CSS.
You just give your navigation bar the "fixed" position. And that's it!
Let's give the div element with the ".wholesection" class a fixed position. And the job is done!
See the Pen image 10 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Try scrolling the "Result" section down. The mobile navbar now has become "Sticky".
Note: if you have advertisements or any content above your navigation menu, consider making that element "sticky" as well. Otherwise, it may not be properly visible.
In that case, you have to avoid using fixed positioning of any HTML element. You can use absolute positioning instead. Absolute positioning is the positioning of an HTML element from its closest parent element that has relative positioning. Hence, it won't be positioned from the viewport anymore.
We will put the .hamburger div inside the ".wholesection" div that contains the navigation menu. Why? Because we will give .hamburger div absolute positioning to make it non-sticky.
No changes are required in JavaScript in this case.
Simply do this two things:
No longer it will be positioned as per the viewport.
See the Pen image 11 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Now, we have made a "non-sticky" navigation bar successfully.
There are some simple steps:
Give the respective CSS for the changed attribute data. Style how you want your categories and sub-categories to appear on a mobile screen once the attribute data is "on". Check the code below:
See the Pen image 12 by Ayan Mallik (@Ayan-Mallik) on CodePen.
Now, check the functionality of the drop-down button for both the
mobile and desktop screens.
Are they working ?
Sweet!
Now, we have a responsive navigation bar that shows all its sub-categories.
Now is the time to play around. Download my source code for the sticky version and non-sticky version of the navigation menus and play around. You learn more when you play around.
You can use either of them. No problem!
There is no hardcore rule for that. But try to keep 4-8 media queries for decent responsiveness of the page.
I prefer using Flexbox for styling the content.
Sure! You can use the icon instead of making one.
That is not possible! You cannot add functionalities with CSS, such as
opening the navigation menu with a click on the Hamburger button.
Photo by olia danilevich.