New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Up and Running with Bootstrap 2
Illustration by

Making the nav bar responsive with JavaScript


From:

Up and Running with Bootstrap 2

with Jen Kramer

Video: Making the nav bar responsive with JavaScript

In the last chapter of this course, we created this navigation bar for our website. And it looks great while it's running here on a Desktop and my browser is maximized, but as I reduce the size of my web browser, this navigation bar wraps on to another line, which is fine for a little while, but when we get down to a browser size that would simulate a mobile phone size, you'll see here that the navigation bar can get quite long and fat, and that's probably not really an effect that you'd like to have.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Up and Running with Bootstrap 2
2h 38m Beginner Oct 12, 2012

Viewers: in countries Watching now:

Bootstrap is a free web development tool from Twitter that, with a little bit of CSS and JavaScript experience, makes building websites quick, intuitive, and fun. Author Jen Kramer explores its 12-column grid layout; typography and icon libraries; fully functional components like nav bars, buttons, and tabs; and much more. This course also shows how to add JavaScript extras like dropdown menus, modal windows, and photo carousels.

Topics include:
  • Understanding the difference between default and fluid grids
  • Nesting with fluid grids
  • Creating a thumbnail gallery
  • Adding block quotes and lists of text
  • Incorporating images and icons
  • Adding breadcrumb navigation and pagination
  • Using tabs and pills navigation
  • Making the nav bar responsive with JavaScript
  • Adding dropdown menus to the nav bar, tabs, and pill
Subjects:
Web Responsive Design Web Design Web Development
Software:
Bootstrap
Author:
Jen Kramer

Making the nav bar responsive with JavaScript

In the last chapter of this course, we created this navigation bar for our website. And it looks great while it's running here on a Desktop and my browser is maximized, but as I reduce the size of my web browser, this navigation bar wraps on to another line, which is fine for a little while, but when we get down to a browser size that would simulate a mobile phone size, you'll see here that the navigation bar can get quite long and fat, and that's probably not really an effect that you'd like to have.

It's possible to add some JavaScript to this to make the navigation bar a bit more responsive. It's not the only approach to making a navigation bar more responsive, many would argue. It isn't even the best approach using a JavaScript solution. But this is a great effect that comes with Bootstrap that will give you the option of having your navigation bar shortened as the screen sizes get a bit smaller. And I'm going to show you how that works now. So if you open up the Dreamweaver document inside your exercise files, this is the finish point for the last video from the last chapter.

We're going to add some additional code to this. Also inside your exercise files is a little text document here that contains some code. So this is the first little bit of code that we're going to add to our Dreamweaver page here. I'm just going to go ahead and highlight this bit of code here and say Edit > Copy. And then inside of my Dreamweaver page, just after the navigation tag but before the brand, so after this div right here but before that Roux Academy Conference line, I'm going to Ctrl+V or Command+V to Paste that in place.

And I'm going to give this just a little bit of formatting here, just so we can make it a little bit easier to read. There we go. So what is this little chunk of code that we just copied in here do? Well, first of all, you'll see these three lines, lines 20 through 22, these are calling the glyphicons. So there's an icon that's called the icon bar. It's just a little line, and we have three of them here stacked on top of each other. So, that's all that part is. The interesting part with a part that makes this interactive is what's inside of the a tag, the clickable link here.

So you'll notice that this has been assigned a few classes, both the button and btn-navbar, and then you see this JavaScript calls for toggle collapse and data-target of .nav collapse. So what these are doing is they're integrating the JavaScript into this web page to make this little icon expand when it's clicked inside of the bar. So if you copy in this code just like this, this is going to make your navbar responsive. You can certainly swap out these three lines here where you have the three icon bars.

You can take those out. You can put in a word like menu, for example, or you could put in a different icon if you wished so that there's something different that's clickable, and you would not change the functionality of what's happening here. The second part to what we have to do is show what's going to be hidden as the navigation bar collapses in size. And so we probably don't want to hide the brand, but we do want to hide all of this navigation right here. So what we're going to do is we're going to add another div tag here, and we're going to give it a class of nav-collapse.

We'll close immediately after the bulleted list. If you're working with a later version of Bootstrap than version 2.1 that I'm using here in this video, you may also need to add a class of collapse. So, line 26 will read div class = "nav-collapse collapse." So, this particular div is indicating this chunk of code will be hidden as the navigation bar gets smaller.

And the media query is going to trigger to display this navigation bar with just the brand, and this little button, because the rest of this which is inside this div of nav-collapse will be hidden when that transition happens. So go ahead and Save this page -- Ctrl+S or Command+S to Save. And if we put this into Firefox and I Refresh the page, immediately we don't see anything here with my browser maximized here on my Desktop computer. But if I make the page smaller, you can see that long around, actually quite wide, this icon now appears.

Remember, that's the glyphicon. We have the three bars that are stacked on top of each other. And as I make the page progressively smaller, then the navigation bar is resizing very nicely. When you click this button, you'll notice that we get the full menu comes on up here with things oriented in a vertical manner. Any content that you have on the page will be pushed down underneath this black box. So this is how you can go about making your site navigation bar responsive just by adding these two bits of code.

So we had a bit of code that triggered the JavaScript to cause the effect of this open and close right here on the web page as I click to open and click to close. And we added some markup that indicates what's supposed to show and hide when that button is clicked.

Find answers to the most frequently asked questions about Up and Running with Bootstrap 2.


Expand all | Collapse all
please wait ...
Q: I am trying to create the image carousels shown in this course, and they are not functioning properly. The custom.css file seems to be missing from the exercise files and I think this is the reason. Can you provide it?
A: custom.css isn't created until the carousel indicators are added. However, these styles have to do with the way the indicators look, and nothinghas to do with the way the carousel functions.
 
Are you sure you're working with Bootstrap 2.x? You download that from here: 
http://getbootstrap.com/2.3.2/. The carousel changed radically between Bootstrap 2.x and Bootstrap 3, and this course might not work with version 3.
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with Bootstrap 2.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.