bootstrap navbar example

Bootstrap Tutorial – Creating a Responsive Navbar (Video)

In the second installment of our Bootstrap 3 tutorial, we build a responsive navbar with dropdown menus. On smaller mobile devices and tablets, the navbar collapses appropriately for optimal viewing.

Source files available on GitHub.

Video Transcript

Hey everybody this is Christopher Gimmer from BootstrapBay.com, and welcome to the second installment of our Bootstrap 3 tutorial. Today we’re going to be building a responsive navbar with dropdown menus.

In our first tutorial, we showed you guys how to install Bootstrap which is the source code that you’re looking at right now. If you haven’t already done so, make sure to check out our first video so you can follow along with us here.

First thing I’m going to do is get rid of this h1 tag that we’re not going to need to build our navbar.

Let’s get started right now.

There’s different kinds of stylings available. For the navbar-default, you’ll see that it’s kind of like a light grey colour. It might even be hard to see, and we can actually change that. If we wanted to go black, we can go navbar-inverse and you’ll notice that it changes to black. There’s different options available, but we’re going to leave this as default.

You also have the option of fixed or static. If you want the navbar to remain fixed at the top of the screen as you scroll down, you would leave it fixed, or you can change this to static. What that will do is, as you scroll down, the navbar won’t remain fixed at the top of the screen. We’re going to go ahead and leave this as fixed.

Next:

This keeps everything inside the Bootstrap grid that is needed for the responsive design. So to illustrate that, if you type container here (within the container class), the text starts off here (within the grid). In contrast, if you type some text before the container class, you’ll see the text shows up all the way to the left. So you want to make sure you’re keeping everything within the container.

Finally:

Now we have all the default styling for the navbar.

First thing we’re going to do is build the button that we need for our responsive design. What that’s gonna do is, as we collapse our browser, the button will show up, and that will enable us to expand our header for mobile and tablet devices.

We’re going to create that button right now. So we’re going to go:

We’re actually going to reference this down below which you’re going to see.

So now if we go to our preview, you’ll notice that when we shrink our browser, after a certain point, the button is now showing up. If you come across other responsive designs, you’ll probably notice that there are three icon bars that usually show up in this button. We’re going to go ahead and actually create that as well.

First thing we’re actually going to do is go:

This line of code is basically to make it accessible friendly for screen readers. You can look this up in the Bootstrap documentation, but it’s recommended you add tags for these screen readers.

And then we’re actually going to add the icon bars.

So now if we go back to our preview, when we shrink our browser, we now have our three icon bars and our button is ready to go.

So now let’s go ahead and start building our navbar.

Underneath this button, I’m going to go:

This is going to style this as your brand up here in the left of your navbar.

So now we’re going to go ahead and start building the right side of our header.

Underneath the navbar-header div tag, we’re going to start adding the contents of the right hand side of the navbar.

We’re going to go:

Up here you remember, we set the data-toggle to collapse and data-target as .navbar-collapse. This is what we’re referencing down here, the navbar-collapse. So basically what that’s going to do is when we collapse our browser, and the the jQuery kicks in, it’s going to reference the contents that we include in this div.

Let’s go ahead and start adding to this right now. We’re going to go:

What that’s going to do is align everything properly on the right side. Now we’re going to build our list.

So now you’ll see we have our home on the right side, and you’ll see this box around it. That’s the active class. If we were to get rid of this you’ll see that the grey box disappears. We’re going to leave this as active. Usually people add this class for the menu that people are landing on.

Let’s continue to build off this now. We’re going to go:

Now we have our About. You’ll notice that this isn’t active so it doesn’t have the grey box around it.

So now for the fun part, let’s go ahead and create our drop-downs.

So now when we preview, we have our themes here. If you click on it, you’ll notice that a little grey box stays around the theme indicating that it’s a dropdown. The issue is when you’re not clicking on it, it’s not evident that this is a dropdown menu. So what we’re going to do is add that little triangle beside it which is called a caret.

So right after Themes we’re going to go:

So now a little triangle appears beside Themes and it’s very evident that it is a dropdown menu. So now we’re going to go ahead and build on that.

So now when you click on our Themes dropdown, you’ll see the Admin & Dashboard header. Now we’re going to build the links.

Now we have our Themes drop-down, we have our Admin & Dashboard header, and we have our Admin 1 & Admin 2 themes.

So now I’m just going to copy this and I’m just going to change this to:

So now we go back to our preview, we have our Themes drop-down, our Admin & Dashboard header with our Admin 1 and 2 themes. We also have our Portfolio header with our Portfolio 1 and 2 themes.

We’re pretty much almost done here. All we’re going to do is add a contact header.

So now once we shrink our browser, our button kicks in, we expand on that and we have all our header items. We can expand again on the drop-down and there are all our menu items ready to go.

So that’s it for our tutorial on building a responsive navbar with dropdown menus. We’ll be posting the source code available for download in the link in the description. Make sure to subscribe to our channel as we’ll be creating many more Bootstrap tutorials.

In our next video, we’ll be creating a footer with social media icons. We’ll see you in the next video.

Update: Part 2 – A Text Guide for Responsive Navbar

In this guide, you will learn how to build a responsive navbar with Bootstrap from scratch on a single web page. We are going to use some amazing features and functionalities.

A list of the tools we’re going to use in this guide:

1. Browser: Chrome

A nice feature of this browser is that we have access to Dev Tools (source code, console and so on). Also, you can toggle different devices so you’re able to see how a website will look on any device. You can literally simulate a mobile device and get a really good feel & look regarding how the website will be.

2. Text Editor: Atom

Atom is a text editor that’s modern, approachable, yet hackable to the core—a tool you can customize to do anything but also use productively without ever touching a config file.

3. Framework: Bootstrap

Bootstrap is a front-end framework and allows you to build a solid HTML structure and easily customize it. It gives you the ability to build really quick and, once you are used to every class that is available, it really makes web development easier.

Quick Overview of Bootstrap

Essentially, Bootstrap is a CSS and Javascript library. When you build using this front-end framework, you link to this library and you bring in beautiful pre-built classes. Here is the GitHub repository where you can find out more about Bootstrap.

Here you can find a section of amazing sites built with Bootstrap. You will see one of the main reasons why Bootstrap is very popular now: it makes it really easy to build websites.

So, in order to start with Bootstrap, we need to link to this CSS library. There are several ways to bring this library to our code. We can download it, we can get the Sass, compile it from source code. We chose to use the CDN links.

Bootstrap CDN

This is the quickest way to get going. CDN is a shared library that we access. The idea here is that multiple sites are utilizing or accessing this shared library to load quicker. We need to link to jQuery library before we link the Bootstrap Javascript because it is based on jQuery . You can also  link to an optional theme (this is a pre-built styling). For more information on how to install Bootstrap, you can check the official documentation here.

We are going to start with the basic  HTML template:

Copy this code and paste it over in the Atom text editor and get rid of the comments because we don’t need to refer to that. The next thing we should do is to refer to the Bootstrap library. Instead of downloading it, I’m going to link to CDN.

Of course, if you’re not using an internet connection the best idea would be to download it.

We are going to replace the link with the CDN for CSS and Javascript because we are using an internet connected device. The Atom editor will look like:

Bootstrap Guide
Starting point. Simple HTML with links replaced from Bootstrap CDN

Open the Index.html with Chrome and you should see a web page with Hello, world! This is basically our starting point. We don’t see any Bootstrap yet because we haven’t applied any styling classes.

I’m going to give you a quick run of Bootstrap classes to get a better idea on how to apply Bootstrap and how we can start building our structure.

How Bootstrap Works

Grid System, in essence, is the basic structure of our website. Previously, when we were building websites before CSS, we built them as tables. Then, CSS was used to position things within the website. By using CSS you have to add a lot of styling, but it’s not really ideal. Sometimes it is difficult to get items to the position you need them, especially when you optimize for mobile and resizing. It takes a lot of time in some cases to develop the exact website structure you want.

Here comes the Bootstrap grid system which gives us the ability to isolate out all those different structural pieces. We are using divs in order to structure the webpage by rows and columns and it’s all contained within a .container or .container-fluid. The difference between .container and .container-fluid is that .container has a fixed width and .container-fluid is full width, no matter what.

Let’s create our grid system in our project. In order to do that, erase

and create a div with the class container.

If you check the index file, it should look like the screenshot from below:

Grid System

Add some styling to see where the <divs> are breaking. As a temporary solution, add the following code above the head tag.

Website structure

Currently, we have our basic Bootstrap layout, we are linking out to the Bootstrap CDN so we are pulling in the Bootstrap CSS library into our web project. We are also linking to jQuerry and pulling in the Bootstrap jQuerry library into the project.

Also, we are going to create a separate link to get to a style sheet to add additional style to our web project.

It looks we are ready to build our navigation bar. So first we are going to add the navbar and position it and add a container to it.

Let’s say we are going to use this navbar for a single page website. Let’s add some sections to the website in the body.

So now, we have 4 main sections – Home, about, products and contact. We did this because we want to link to these sections from our navigation menu from the top of the page. We will have to set ids for these sections so we can target them by clicking a link.

The full code for our basic layout:

Now we should go to the CSS file we linked in our <head>. If you haven’t created a file named style.css now is the time.

I want a minimum height of 1000px. I want to set overflow to hidden (this will hide any content that is hanging off my available visual space. We are going to do position “relative” and add some padding at the top of each section of 80px.

Also, in order to see the difference between the sections (because we have no content there) we should add some color:

Let’s build our navigation bar:

The first thing we should do here is to link to Font Awesome. Font Awesome gives us a library of nice and usable icons. They require no Javascript. You can check their website, select an icon and they give you a link to insert in your code.

Fontawesome for navigation bar

We are going to use bars. This is something that you will typically see when your website is resizing. To implement Font Awesome we have to link to their CDN:

The code for bars icon is:

We have to add the class “navbar-header” and a button.  The class of the button is “navbar-toggle” that allows us to toggle the navbar. After that, we add ‘data-toggle=”collapse ‘. This is all information that is picked up by Bootstrap. Lastly, we have to add “data-target=’#top-navbar’ ” that allows us to specify where we want the collapse to happen.

Now, let’s build our navigation bar. Add a div making sure you are within the navbar header. You should then make an unordered list ul and add items with links to sections from the body.

The navbar source code:

The whole HTML code

The CSS Stylesheet Code from style.css

Result

Responsive Navbar

 

Christopher Gimmer

Christopher is an entrepreneur & the co-founder of BootstrapBay. He helps web designers, developers and entrepreneurs utilize the power of Bootstrap. Connect with him on Twitter.