MODAL DIALOGS
Modal dialogs are the ultimate way to get the user’s attention, as the user can’t interact with the rest of the page without first closing the modal dialog. Modal dialogs can contain whatever HTML you need to present, including status messages, videos, or forms.
JAVASCRIPT AND BOOTSTRAP
In response to several questions I’ve seen in the discussion forums, I wanted to spend a little time talking about jQuery/JavaScript and Bootstrap. Now, if you’re not familiar with JavaScript don’t worry about this section. It is, as the title implies, bonus content.
One thing that you’ve probably noticed is Bootstrap does a lot of work for you, allowing you to avoid writing JavaScript in many cases. However, there is an API that is available you can use for certain operations, such as closing a modal dialog box. But even for actions that don’t have Bootstrap API calls, such as toggling validation, you’ll notice that you can accomplish the desired result by simply adding and removing the appropriate classes, which jQuery makes very simple.
<!DOCTYPE html>
<html>
<head>
<title>Confirm !</title>
<meta charset=”utf-8″ />
<link href=”Styles/bootstrap.css” rel=”stylesheet” />
</head>
<body>
BOOTSTRAP FORMS, BUTTONS, VALIDATION
Bootstrap offers great support for collecting information from users, as well as providing feedback. As with everything else in Bootstrap, you create forms by adding the appropriate classes and HTML structure to your pages.
CREATING HORIZONTAL FORMS
By default, forms in Bootstrap are displayed vertically, meaning that the prompt for an input control, such as a textbox, is placed above the input control. Quite frequently, you want the prompt next to the input control. In order to do this, a little bit of structure is required for the form, and a couple of classes need to be added.
Creating the form element
In order for a form to use horizontal display, the form-horizontal class must be added to the form element.
- <formclass=”form-horizontal”>
- <!– form content here –>
- </form>
Creating the rows for the form content
Horizontal forms use the same grid system we’ve already covered. However, you won’t create normal rows like you did in the past. Instead, you will create form groups. Form groups tell Bootstrap that the controls inside of that section are, as the name implies, a group of controls for the form. In addition, the form-group class also creates a row, so you do not need to add in the row class.
Adding the label
Adding labels to forms is a best practice. By adding a label, you’ve made your form more accessible for screen readers, as well as to touch displays, as clicking on the label will place the form on the associated control. The label element supports sizing by just adding the appropriate col-*-* class to the element. In order for the label to be recognized by Bootstrap as a form label, the control-label class must also be added.
- <labelfor=”address” class=”control-label col-md-3″>Address:</label>
Adding the input control
The last item to add is the input control. In order for it to be recognized by Bootstrap, it must be decorated with the form-control class; if you forget that class the formatting won’t be correct. In addition, the form control itself cannot be sized by using col-*-*; in order to size the form control, you place it inside of a div tag decorated with the appropriate sizing class.
FULL HTML AND SCREENSHOT
- <formclass=”form-horizontal”>
-
- Address:
- </div>
- </form>
ENHANCING CONTROLS
HTML5 offers several improvements over the classic HTML controls, including the ability to add placeholders, and support for new data-types, such as email addresses. However, you’ll notice that textboxes still look like textboxes. Bootstrap offers the ability to enhance textboxes, providing more inline support and guidance as to the type of data you expect from a user.
ADDING GLYPHICONS TO FORM CONTROLS
By adding a glyphicon to a form control, you can provide additional context to the user. Bootstrap provides a host of glyphicons, and makes it straight forward to add them to a form control.
Creating the input group
In order to add a glyphicon to a form control, you need to create an input group. The input group will contain the components Bootstrap will use to construct the UI.
Creating the input group addon
Whatever content will be added on to the control must be added as an input group addon, which is accomplished by creating a container with the input-group-addon class.
Adding the glyphicon
Inside of the input group addon container, place whatever item it is you wish to be added to the control, such as a dollar sign, an at sign, or a glyphicon.
- <spanclass=”glyphicon glyphicon-user”></span>
FULL HTML AND SCREENSHOT
- <formclass=”form-horizontal”>
-
- Username:
- <input type=”text” class=”form-control” name=”username” id=”username” />
- </div>
- </div>
- </div>
- </form>
BUTTONS
Buttons are a part of the user interface that a user can click on. However, buttons don’t necessarily need be actual HTML buttons. The advantage to making another control, such as a hyperlink, appear as a button is it becomes clearer to the user that they are able to click on that item. Bootstrap offers the ability to make most HTML controls that someone would click on, including both hyperlinks and buttons, appear as buttons.
CREATING BUTTONS
To create a button interface, you decorate the component with the appropriate classes. The first class tells Bootstrap the control should display as a button, and the second will be the modifier, indicating the color scheme to use.
Adding a button
To add a button, decorate the item in question, such as a hyperlink or button, with the btn class, and the appropriate modifier.
- <buttontype=”submit” class=”btn btn-default col-md-offset-3 col-md-4″>Submit form</button>
BUTTON GROUPS
Button groups allow you to make radio buttons or checkboxes have the same UI as buttons. The advantage to doing this, besides the fact that it just looks better, it is also more touch friendly than the default radio buttons or checkboxes.
Creating the group
The first step to converting radio buttons or checkboxes to look like buttons is to add the button group. The button group must also have the data-toggle=”buttons” attribute set.
-
- </div>
Adding radio button (or checkbox)
The second step is to add in the radio button or checkbox. The radio button or checkbox will be surrounded by a label. The label will provide the button interface.
- <labelclass=”btn btn-primary”>
- <input type=”radio” id=”first” name=”sample” />
- First
- </label>
DEFAULT BUTTON COLORS
The color of the button changes based on the modifier. All button modifiers have a prefix of btn-
Modifier Default color Default White Primary Dark blue Success Green Info Light blue Warning Yellow Danger Red FULL HTML AND SCREENSHOT
- <formclass=”form-horizontal”>
-
- First
- Second
- </div>
- Submit form
- </form>
VALIDATION
If there’s anything consistent about dealing with user input, it’s that at some point someone is going to submit data that’s incorrect. This might be because of an incorrect data type, a typo, or potentially something malicious. Regardless of the reason, you want to provide the user with feedback as to their mistake.
Bootstrap offers several potential ways to do this, including inline validation. The advantage to inline validation is the user sees their error message in the same location as the error itself.
ENABLING INLINE VALIDATION
In order to enable inline validation, a couple of classes and items need to be added to the HTML.
Add validation state to the form-group
To use Bootstrap to set the color for the current validation state, add one of three classes to the form-group container:
- has-success (green)
- has-warning (yellow)
- has-error (red)
Add icon to the textbox
To add a glyphicon to the textbox to indicate status, first update the form-group container to include the has-feedback attribute.
Then, after the textbox, or input-group, add a span element for the glyphicon. Ensure that the span element is decorated with the form-control-feedback class.
- <spanclass=”glyphicon glyphicon-remove form-control-feedback”></span>
FULL HTML AND SCREENSHOT
- <formclass=”form-horizontal”>
-
- Address:
- </div>
- <!– Input group –>
- Username:
- <input type=”text” class=”form-control” name=”username” id=”username” />
- </div>
- <!– Glyphicon is placed after input group–>
- <span class=”glyphicon glyphicon-remove form-control-feedback”></span>
- </div>
- </div>
- </form>
ALERTS
In addition to providing inline feedback on the controls themselves, you sometimes need to do a little more to get the user’s attention. This might be a generic error message letting the user know they need to fix a couple of items, or it might be to let them know about the status of a background call that was made on behalf of the user, such as an Ajax call. This is where alerts come into play.
Alerts are colorized sections of a page designed to get the user’s attention. You can create an alert by creating a container with the correct Bootstrap classes. Keep in mind that alerts are not dynamic, but you can make them dynamic very easily by toggling the visible/hidden classes.
CREATING ALERTS
To create an alert, you need to add in a container with the appropriate Bootstrap classes, and then configure it as needed.
Alert container
The alert container is a standard div element, with the alert class added.
Modifiers
You can also add in the appropriate color modifier. Keep in mind that the modifiers only impact the color and have no other impact on the page.
Modifier Default color alert-success Green alert-info Light blue alert-warning Yellow alert-danger Red Dismissible alerts
To make an alert dismissible, or closable, add the alert-dismissible class to the container, and add a button to allow the user to dismiss the alert. The button needs to be configured with the data-dismiss=’alert’ attribute.
- ×
Alert content
Alerts can contain whatever content you need to let the user know the current status. This can include header elements or other HTML.
FULL HTML
-
- ×
Warning
It appears that something has gone wrong- </div>
- </div>
SCROLLSPY, ACCORDION, CAROUSEL
SCROLLSPY
Scrollspy is a data display that allows a user to jump directly to the data they wish to see, but also enables scrolling. Like tabs, scrollspy provides navigation tabs. But unlike tabs, scrollspy allows a user to scroll through content as well.
SCROLLSPY SAMPLE
Navigation
The first step to adding scrollspy to your page is to add a nav element. The nav element needs the classes navbar (to mark it as a navbar), navbar-default (to enable the color scheme), and navbar-static (to allow it to scroll with the page). In addition, you will want to add the role=’navigation’ attribute for accessibility.
In addition, you will need to give the nav element an ID. When scrolling through content, the tab that represents the data that is currently being viewed should be active; Bootstrap handles this by requiring you to point the container for the data back to the navigation.
- <navclass=”navbar navbar-default navbar-static” role=”navigation” id=”navbar-spy”>
- <!– links –>
- </nav>
Inside of the nav element, you’ll add an unordered list. The ul element needs to have both nav and navbar-nav classes added to it. Inside of there you will add the links that will act as the navigation links to the scrollspy content. Each link will point to the ID of the content’s container, again making sure to use CSS selector syntax. The link for the first data to be displayed should have the active class added.
- <ulclass=”nav navbar-nav”>
- <li class=”active”><a href=”#scroll-first”>First</a></li>
- <li><a href=”#scroll-second”>Second</a></li>
- </ul>
Content
The content that the user will navigate through using scrollspy needs to be inside of a container decorated with data-spy=’scroll’, and its data-target attribute set to the ID set on the nav element (again using CSS selector syntax). If you want to use scrollspy for the navigation of the entire page, say for a frequently asked questions (FAQ) page, you can use those attributes on the body element. Finally, the style of the containing element needs to have position:relative in order to ensure the tabs highlight correctly as the user scrolls.
In the example below, I use a div as the container, with the height sent to a small value, to reduce the vertical space for my demo, and then overflow-y set to scroll to enable scrolling.
Each block of content needs to be inside of a container with an ID. The ID will be referenced by the links in the nav element.
HTML and screenshot
- <h2>Scrollspy</h2>
-
- First
- Second
-
- </div>
- </div>
ACCORDION
The accordion display is similar to tabs, in that a user can only see one piece of data at any given time. However, unlike tabs, the display is vertical, and each pane is a collapsible region.
ACCORDION SYNTAX
Accordion container
The accordion is built using the panel capabilities of Bootstrap. The container will be decorated with the panel-group class. It will also need an ID to represent the accordion container.
Panel header
Each collapsible section will be a panel inside of the panel group. Each panel will be decorated with the paneland panel-default (for color) classes.
Each panel will need a heading and a section for the content. The heading is created by adding a container, decorated with the panel-heading class, followed by a container for the title, typically a header element, with the panel-title class.
Finally, the header needs a link to enable the collapsing for the panel. The link needs to be decorated with data-toggle=’collapse’. It also needs to have data-parent set to the ID of the panel container you created earlier. And finally the href attribute needs to be set to the of the content panel, which you’ll create shortly, again using the CSS selector syntax.
- <adata-toggle=”collapse” data-parent=”#accordion-sample” href=”#accordion-sample-one”>
- <!– heading text –>
- </div>
Panel content
Panel content needs two containers. The outer container will have the ID that will be referenced by the link, and two classes – panel-collapse and collapse. In addition, the first panel to be open needs to have the in class.
Inside of that container, you will create one more, with a class of panel-body.