Create 3 Different Navbar Layouts With Flexbox

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
super B work
Thanks ๐
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

Hey everyone, in this short tutorial I'm gonna show you how to create 3 common navbar layouts using HTML, CSS and Flexbox.
Lets get right into it!

This is the first navbar we will be building. It is a very common layout used by many websites out there. It has the brand name on the left side and the navigation links on the right side.
The HTML markup for this navbar is fairly straight forward:
<nav class="navbar">
<h2 class="nav-brand">My Website</h2>
<ul class="nav-links">
<li class="nav-item">
<a href="#" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Shop</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
</ul>
</nav>
We have a nav element wrapping all of the content with a class of navbar. We then have a h2 which contains the website's title/brand. We then have an unordered list with a li for each navigation link that includes an a tag.
To style this navbar, we will first need some basic styling for the content within. This includes changing the color of the content to match the purple background and displaying the links horizontally:
.nav-links {
display: flex;
list-style: none;
}
.nav-item:not(:first-of-type) {
margin-left: 1.5em;
}
.nav-link {
color: #fff;
text-decoration: none;
font-weight: 500;
}
We can now add some styling to the navbar to change its size, colors and spacing:
.navbar {
width: 100vw;
height: 7.5vh;
background: #855FF6;
color: #fff;
padding: 0 5vw;
}
Obviously, the height, colors and padding can be adjusted for this navbar to suit your circumstances.
Now let's use flexbox to align the content:
.navbar {
display: flex;
align-items: center;
justify-content: space-between;
}
A quick explanation of the above:
display: flex - sets the display of the navbar to flex so the items will now be displayed in a row instead of on top of each other.
align-items: center - aligns the items (h2 and ul) to the center vertically. This means whatever the height of the navbar is, the content will be in the center.
justify-content: space-between - puts space between the items so the h2 will stay to the left and the ul will be positioned on the far right.

The second navbar is another reasonably common layout. It has got the title/brand on the left side, the links in the middle and button on the right side.
The markup is very similar to the previous navbar with the addition of the button after the .nav-links list:
<nav class="navbar">
<h2 class="nav-brand">My Website</h2>
<ul class="nav-links">
<li class="nav-item">
<a href="#" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Shop</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
</ul>
<button class="nav-btn">Create Account</button>
</nav>
Interestingly, the exact same styling used in the previous navbar will make this navbar layout. The reason why is because justify-content: space-between; with three children instead of two children positions the middle child element (in this case, the links) in the center with the adjacent elements on the far left (the h2) and far right (the button).
You will however need to add styling to the button:
.nav-btn {
height: fit-content;
padding: 1em 1.5em;
background-color: transparent;
border: 1px solid #fff;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
Now, lets move on to the final navbar layout

The third and final layout we will be creating is a simple navbar with the links in the middle and no brand nor button.
The markup is quite simple yet again:
<nav class="navbar">
<ul class="nav-links">
<li class="nav-item">
<a href="#" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Shop</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
</ul>
</nav>
Same markup without the h2 or button.
The styling is the same again however set justify-content to center rather than space-between. This will align the links to the center of the navbar horizontally.
Today, I've shown you how to create 3 different common navbar layouts. I hope you've learned something useful and can start using these layouts in your websites.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake Yeboah
Twitter: Blake Yeboah
You can also show your support by buying me a coffee ๐