Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Bootstrap does ship with a Navbar that you can use with your webpages. The Navbar comes with many, many options for customization and styling. You can read all about the Navbar here at the getbootstrap.com website. If you go to components and then click on Navbar. You'll find all of the code is here along with various customizations that you can make and tons of styling options. And, I can't possibly go through all of it in one video but I'm going to give you the high points for how to put together a fully responsive Navigational bar inside of Bootstrap in this video.
And then in the next chapter, I'll show you how to attach drop down menus to it, as well. So, what we're going to do is we're going to start with 04_03. Comes from your Exercise Files folder. Copy that from the Exercise folder, and paste it into the Bootstrap folder. And, we're starting here down at row 1 for navigation. So, we're around line 16. The very first thing I'm going to do is change this nav tag that I had associated with the row, back to a div. And that's because I'm going to move the nav tag elsewhere inside of my code.
Where I feel it's a little bit more semantic, which is actually inside of the row tag here. I'm going to add a nav tag right here. And this is going to have a class of both navbar and navbar-default. And we're also going to add a role of navigation. The role attribute that we're adding here is helpful for accessibility purposes. It helps screen readers to know that this is in fact navigation that we're going to add. Then inside of this navtag which I should be sure to close, so I'm going to close that on down here /nav.
And indent my list here. Inside of this nav tag here now in line 18, I am going to add a div with a class of navbar-header. So this is going to be responsible for some bits and pieces of functionality that are responsible for the responsive side of this bar, including the button that you click on in order to get navigation as the screen gets narrower. So I'm going to open that div and I'm going to close it. And then inside of this div with the Navbar of header.
I'm going to include some other things. First of all, I'm going to start with a button with a type of button.Type = button. And a class of navbar-toggle. And then I'm going to add two attributes with which you may not be familiar, data- toggle = collapse. And data-target = #collapse. Alright. So, any time you see data-anything. An attribute inside of HTML 5.
Any time you see the class of sr-only inside of Bootstrap. This is for screen readers. That's what that class means, for screen readers only. This is something that you're going to show, and you're going to see these words toggle navigation and /span. That's because the button here that we're going to put in isn't necessarily the most accessible. But this particular span class sr only. If they click this toggle navigation, then that will in fact work. Okay, then we're going to add a span with a class of icon-bar/span.
And we're going to put that in three times. So icon bar, icon bar, icon bar. And then at that point we'll end our button /button. So all of this is inside of the button. So the whole entire unit here is going to become something that's clickable. That's what is going on here inside of this div with a class of navbar header. And as your screen gets narrower and your navigation narrows down to just that button that remains, you click the button to get your navigation. That is what's responsible for the Navbar header here. Then were going to add, after the /div, we're going to add another div.
This is going to be a div with a class of the following: collapse, navbar-collapse and it's also going to have an ID of collapse. And that /div is going to happen here after the /ul. And I'm going to go ahead and indent my list just to make things a little bit easier to read. Okay. So, what's going on here? Well this particular div is controlling our main navigation. So this is the navigation that's going to show on the screen in the bar going horizontally to start with.
And then, as the screen gets smaller, then that navigation is going to disappear or collapse and that's going to turn into the button that we've specified up here at the top. So up here on the top where we have this data target of #collapse, that goes with the ID down here of collapse. In other words, what we've said is when this button is clicked. When it's present and it's showing on the screen at these lower resolutions and we click that button. Then whatever is inside of this div will become visible.That is what we're talking about.
And we've tied those two things together between this idea of collapse. And our data target of #collapse. You don't have to use an ID here. You could have used a class just as easily, and tied the class to our data target up here. If you did a class, it would start with a period instead of with a pound sign. Then the last thing we need to do here is on this UL where we had a class of nav, nav pills, nav justified. Well that's not what it is anymore. We're going to leave the nav, but we're going to change it to navbar-nav. And once you have those things in place go ahead and save your webpage.
And we can put this inside of our browser. And lo and behold. We have a Navbar going across the top of our page. How cool is that? And we're on the Services page. Because that's designated our active page. And we can scroll on down the page. And things are good. And we can make this page smaller, and as I make this page smaller and smaller, finally we wind up with the button here up on the top. And this button has been called many things, this button with three bars, and I've started to collect names. So some of my favorites are the Hamburger button.
The Pancake button. Or the Adidas button. When you click that button, this is the navigation that you get that appears here on the page. Alright. So by default, the Navbar that you get is this sort of pale, sort of whitish color and it doesn't stretch the full width of the page. So something that you might want to do is change the color of this Navigation bar and/or make it stretch the whole width of the page. So there's some additional classes that allow for that. So, one of the things that we can add then here to our nav tag on line 17. We're going to add navbar-inverse.
And that will change the color of the bar from whitish to, of course, a very, very dark grey, almost black. So if you refresh your webpage, you'll now see that we have a black bar instead, which I think is a little bit more attractive. And one other thing that we can do, is we can make this Navigation bar stretch fully across the screen. No matter what width it is. And fixed to the top of the screen. So that as we scroll down, the Navbar on the top will stay in place. We actually see that in place here in the Bootstrap top Navigation bar. You'll see that that stretches all the way across the page.
And as I scroll down that top Navigation is always there and present. And, of course, this will turn into its little button here, as the screen gets smaller. So, in order to make that happen, here in this navtab also, again on line 17 we're going to add a class of navbar-fixed-top. And that's going to fix the navbar to the top of the screen. So if you save that and you refresh your webpage, there we go, now our Navbar is stretching all the way across the screen, no matter how wide we make it.
But notice the other little problem that happened here. The heads of our animals all got cut off. And it will tell you in the Bootstrap documentation that this will happen when you use the navbar fix to the top. And it actually gives you the fix as well, which is a quick little style that we need to add. So if you open up your custom.css. And up here, I'm just going to put it as the very first style in my style sheet here, on line 2. We're going to add a style for body, and we're going to tell it to give it a padding-top of 70 pixels.
Save that, and refresh your web page. And now, your heads of your dogs are no longer cut off. The Navbar is fixed up here on the top of the screen. As you make your screen narrower, it does, in fact, collapse down to the button here. Which will still open and show you all of your navigation. So, that is Bootstrap's Responsive Navigation bar in a nutshell.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 60095 Viewers
61 Video lessons · 92736 Viewers
82 Video lessons · 104198 Viewers
56 Video lessons · 107197 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.