How to Create Responsive Navbar in CSS?
Overview
Responsive design is essentially a technique for building websites that automatically adjust their features and information to fit the size of the screen. The responsive navbar offers links to relevant pages, which facilitates navigation. We can develop responsive designs by using CSS media queries. We can create a variety of navbars, including those with dropdown menus, hamburger menus, and navigation for mobile devices. In this article, we'll learn how to make responsive navbars.
Pre-requisites
Make sure you understand the following fundamental ideas of HTML & CSS before continuing :
- Flex-box
- pseudo-elements
- CSS media queries
- Selectors in CSS
What is a Responsive Navbar?
The responsive navbar is a UI element on the website that contains links to appropriate sections or pages. It makes it easier for the user to navigate the necessary documents without having to type in the URL of a particular webpage. The responsive navigation bar works perfectly fine regardless of the device. It adapts itself to fit any device. We need to design a navbar that enriches the user's experience and leaves a lasting impression.
For building an ideal navbar, it should follow three key elements:
- Simple : It needs to be simple to read and crystal clear. We should focus on the larger sections of your site rather than overcrowding the navbar with links to every page. Then, if necessary, we may add a dropdown sub-menu to the main menu.
- Noticeable: Simple does not equate to boring. We should use contrast, color schemes, and other visual cues to draw attention to the clickable components in the navigation bar.
- Responsive : Our website must have responsive mobile navigation since a significant portion of the global population accesses the internet using a mobile device.
How to Create a Responsive Navbar?
Let's begin by creating the responsive navbar now that we have a firm understanding of the design concepts. Step 1 : First of all, let's give structure to our navbar.
HTML :
Here, we have
- a div container having a class nav acts as a container for our navbar.
- four links using anchor tags .
- a heading and a paragraph element.
- An active class to highlight the active link.
STEP 2: Styling the navbar container and the links.
STEP 3 : Using media queries to make it responsive.
Output:
How to Create a Responsive Navbar with Dropdown?
A drop-down menu is a way to display a long list of options since just one option is visible at first until the user activates the drop-down box.
The following example describes a responsive Navbar with a dropdown :
Step 1 : Adding HTML
Step 2 : Styling the Navbar and The Dropdown Menu
Step 3 : Responsive Navbar Using CSS Media Queries
Dropdown Menu
How to Create a Responsive Navbar with Hamburger Menu?
A hamburger menu turns the navigation bar on and off by hiding it behind the button or displaying it. Three lines that mimic a stack of hamburgers make up a hamburger icon.
The following example describes how to create a hamburger menu.
Step 1. Structure of a Responsive Hamburger Menu (HTML)
- We have the main navigation menu and a container for creating the hamburger icon.
- The navigation menu consists of a few links.
- Since we are using pure CSS, we will control the state of the icon, i.e., whether to show and hide the navbar using the HTML checkbox.
- We will specify an id for the checkbox and associate it with the label using the for attribute. Clicking the <label> will select and deselect the checkbox. Then we will toggle the checkbox using CSS.
Step 2. Styling the Navbar Using CSS Flexbox
- We will style the main menu nav and the hamburger menu-button-container using the CSS Flexbox.
- To draw the three lines for the hamburger icon, we'll use the ::before and ::after pseudo-elements. The CSS for this is:
Step 3. Adding Functionality to the Hamburger Menu with CSS
- We will move the top and bottom bars or lines of the icon to the position of the middle line.
- We will hide the middle line.
- Lastly, we will rotate the top and bottom lines 45 degrees in opposite directions to create a cut/cross icon.
Step 4. Responsive Navbar Using CSS Media Queries
- We will add the CSS media queries to make our hamburger navbar responsive.
- We must select a screen width that will toggle a full-width menu and the responsive CSS hamburger menu.
OUTPUT
How to Create Responsive Mobile Menu?
The following example describes how to create a responsive mobile menu :
Step 1: Adding HTML
Step 2. Styling the Navigation Menu
Step 3. Styling the Hamburger Menu
Step 4. Styling the Hamburger Icon
Step 5. Adding Functionality to The Hamburger Menu With CSS
6. Using Media Queries to Make It Responsive
OUTPUT:
Conclusion
- The responsive navbar is a UI element on the website that contains links to appropriate sections.
- The responsive navigation bar works perfectly fine regardless of the device.
- We can develop responsive designs by using CSS media queries.
- A dropdown is a list of choices that appears when the user interacts with the menu.
- A hamburger menu turns the navigation bar on and off by hiding it behind the button or displaying it.