The only thing better than a Bootstrap-styled user interface is a wizard written using Bootstrap.js, and Vincent Gabriel’s oh-so-capable Twitter Bootstrap Wizard Plugin lets create one quickly and easily. What I wanted was a step-by-step wizard with a lot of features:

  • Customizable Previous and Next buttons
  • A Customizable Finish button
  • Support for an arbitrary number of steps
  • A progress bar

Here’s what the sequence should look like visually:

Twitter Bootstrap Wizard Example Step 1.

Twitter Bootstrap Tutorial Screen Shot 1

 

Note how in Step 1 the First and Previous buttons are disabled, as they should be. You can use class and style  attributes to modify default behavior.

Twitter Bootstrap Wizard Example Step 2.

Twitter Bootstrap Tutorial Screen Shot 2

In Step 2 the First and Previous buttons have automatically become active.

Twitter Bootstrap Wizard Example Step 3.

Twitter Bootstrap Tutorial Screen Shot 3

In Step 3 the Finish button has automatically been replaced with a Done button. The default behavior for the Done button is to appear as disabled, but that behavior is easily modified using a bit of jQuery.

I think Bootstrap has changed in such a way that Vince’s code no longer works as written with the current version. At least I couldn’t get it to work, but I’m pretty sure that’s more a reflection on me than Vince. Not being a Bootstrap expert I had to go back to first principles and understand their tab markup, then add Vince’s good work on top of that.

By the way, I try to include running code here through the use of CDNs, but the Twitter Bootstrap Wizard Plugin isn’t available on cdnjs, even though it appears to be.

Here’s the complete running program.

Assumptions for the Twitter Bootstrap Wizard tutorial:

Since I don’t have a demo, here’s the complete code listing before I get into the explanation.

 

Step by step Markup Tutorial for a Bootstrap Wizard With Progress Bar

 

0. Create a surrounding div

The whole thing must be surrounded by a simple div with an ID you assign the name of the wizard, rootwizard in this example:

 

1. Tab markup: Unordered list

      • The markup for the tabs themselves is an unordered list with class="nav nav-tabs" role="tablist" in the ul
      • Each tab is in an li tag with a unique named anchor on the same page, for example, a href="#step2"
      • The li tag must use data-toggle="tab" attributes. FWIW I was able to omit the role="tab" but that seems like a bad thing for forward compatibility.

2. Progress bar markup: div of class “progress” and nested div of class “progress-bar”

  • Create a div of class “progress”. It doesn’t need an ID
  • Inside it nest a div of class “progress-bar”. It requires an ID, in this case, I used progressBar. jQuery will be used to update the div dynamically as the user clicks Previous and Next buttons and it needs the ID to know which div to work on.

The “progress-bar-striped” attribute is optional. It looks more dynamic and gives the user a feeling of real progress while filling out a form.

I put the progress bar just below the tabs but it can go anywhere. For example, it looks just as good if you switch steps 2 and 3.

3. Tab pane markup: div of class “tab-content” with nested divs of class “tab-pane”

The tab panes are where content goes. You can only see one at a time, even though they’re all in a single piece of markup.

      • Create a div of class “tab-content” to enclose all tab pane divs. It doesn’t need an ID.
      • The tab panes are separate divs of class “tab-pane” nested inside the class “tab-content” div. Each tab pane has a separate ID, which must match the named anchors in the li declarations in Step 1.
      • The first tab tab pane has “active” in its class attribute. That selects the tab on page load.

4. Previous/Next Buttons: Unordered list of class “page wizard”

The First, Next, Previous, and Done buttons make clever use of the unordered list markup.

  • The ul must include the attribute class="pager wizard"

They do not need to be named First, Next, Previous, and Done because their function and position are both determined by a class attribute:

  • Each li tag contains an a link as the target.

As a bonus I’ve included access key attributes on the a links, which have nothing to do with the wizard. I just like keyboard shortcuts. Access keys let you use a letter key in conjunction with a system-defined key or key set to activate a control. For example, the Next button uses a href="#" accesskey="n". On a PC you’d press Alt+N key as the equivalent of the Next button, and on a Mac it would be Control+Option+N

5. Load the jquery.bootstrap.wizard.min.js in a script tag

The jquery.bootstrap.wizard.min.js file should be included as far down in the body as as possible, because sometimes Javascript files can page rendering.

6. Add Javascript code in a script tag

The magic in Vince’s progress bar code is in using jQuery to redraw the Bootstrap-provided progress bar div according to which tab is selected:

I added a few lines of optional code. For example, this section makes sure the Done button appears only when at the last tab, and that it’s active:

In Vince’s code, the Next button appears on the last tab, although it’s disabled. In my version, it’s removed on the last tab only:

Here’s what all the Javascript looks like.

Here’s a step by step recipe with all the code you need to see how to create a tabbed interface in Twitter Bootstrap in a single HTML file.

The result will look something like this:

Bootstrap.js tab tutorial

Try it out and grab the source for yourself here

Markup for the tabs

Bootstrap Tabs use an unordered list in conjunction with tab-content divs that contain a nested tab-pane for each div‘s content.

In this example the tab’s text is Sell. It could be any text but the most important attribute is the #sell attribute. It must match a div ID in the upcoming tab-pane div:

One of the tabs should appear as selected. Use the active attribute:

Here’s the full declaration for the tabs.

Next are the tab panes. The ID of each pane must match the href attribute but without the pound sign:

Here’s all the code in context. Save this as an HTML file and it will create the same kind of interface shown in the screen shot

This minimal HTML5 app template shows Bootstrap and JQuery working together. It’s big enough to show off features of both Bootstrap and JQuery but small enough to hollow out and use for your own purposes.

A lot of the Bootstrap examples I’ve seen are far more complicated than they need to be. Copy and paste this code to knock together an attractive, feature-packed website as prototypes or as a starting point for production apps.

It has a little behavior built in:

Your Copy-and-paste Bootstrap HTML app using JQuery to toggle a div

A preformatted area called a “well” in bootstrap terminology is hidden using a standard CSS

display: none;attribute until you click the Show button.

bootstrap-starter-cdn-2-second

At the same time the well is displayed (using JQuery’s toggle() feature, the button’s text gets changed to Hide:

Here’s what it looks like after you click.

bootstrap-starter-cdn-2-second

Try it live here.

The full, commented copy-and-paste code is here:

Learn how to use Bootstrap from a single file-no CSS or Javascript directory needed because they are loaded from a public website (called a CDN)

This quick tutorial shows how to use Bootstrap from a CDN, or content delivery network. You know how normally you create a CSS directory for stylesheets and a JSS (or whatever you call it) for Javascript assets? You can pull these files off someone else’s server instead (the CD).

I like this for quick prototyping tests on code that uses resources such as Jquery and Bootstrap because I can do a test in one file.

In production, it may be better to host Bootstrap and Jquery yourself. You have more control over them and the performance may be better, but that all depends on the CDN.

How to use Bootstrap with a CDN

It’s easy to use Bootstrap with a CDN.

1. Instead of using a local file reference, just include an href to the stylesheet somewhere between the <head> and </head> tags:

2. Link to the CDNs for Jquery and Bootstrap between the <body> and </body> tags:

Here’s everything you need to put up a Bootstrap site, all in a single file. Simply copy and paste into a file called index.html or whatever you wish to name it. It will work fine on your own machine or on a server, unchanged. And you don’t have to remember to create Bootstrap or JQuery directories.

The site will look like this:

Screen shot: Bootstrap Starter Template All in One File Using CDN

How I found the Bootstrap CDN

If you want to know whether a library you’re interested in is available on a CDN somewhere, just search for bootstrap cdn or whtaever. That’s how I found bootstrapcdn.com

Advantages to using a CDN

        • Great for trying experiments quickly because you can code from a single directory without creating separate directories for CSS or Javascript assets
        • Someone else maintains these files for you and often can automatically upgrade to the latest version

Disadvantages to using a CDN

        • If the CDN goes out of business, you have to re-host on your own website
        • If the CDN performance is bad, it reflects badly on your website
        • If you use something like jquery_latest (shown below) to obtain the most recent version, it’s possible that new, incompatible code in the library may break your existing code.
        • It doesn’t work offline, so you can’t develop without a net connection.
        • You can’t edit the CSS or Javascript on the CDN (big security risk, and of course it would change every one else’s copy)
        • It’s easy to forget to upgrade a library if versions change. The JQuery CDN avoids this neatly by using  jquery-latest.js