Twitter Bootstrap happens to be the easiest and the best CSS framework on the Internet today. It allows developers with no CSS knowledge to build basic templates without any efforts. But this doesn’t stop designers from using Bootstrap.
Let’s get started!
After, you have downloaded Bootstrap 3, copy the contents of the dist folder and paste it inside a new work-space.
A modal is a dialog prompt just like a traditional alert. It comes with advanced features like modal title, modal body, modal footer, close button and a close symbol at the top right corner. It can be used as a confirmation window in many applications such as before making a payment, or deleting an account, etc.
<!-- Button trigger modal --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> <h1>Hello World!</h1> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
Bootstrap Modal has three sections: header, body and footer. You can decide what to place in each of them.
Creating a drop down menu in Twitter Bootstrap 3 gets extremely easy. You just have to understand right markup required. You can use this DropDown in a navigation bar or inside any div you wish.
<div class="dropdown"> <a data-toggle="dropdown" href="#">Show Links <b class="caret"></b></a> <ul class="dropdown-menu" role="menu"> <li><a href="#">First Link</a></li> <li><a href="#">Second Link</a></li> <li role="presentation" class="divider"></li> <li><a href="#">Third Link</a></li> </ul> </div>
First, you have to give the class “dropdown” to any parent element that you want to treat as a drop down element. In my case, I have used a div element. You can even make an li element as “dropdown”. Then you have to place an “<a>” tag immediately inside the dropdown element. Add a new attribute “data-toggle” to the link tag and give the value as “dropdown”. Finally add a ul list below the link tag. You have to add class as “dropdown-menu” to the ul tag.
To add a separator between li elements, add a new emplty li element with class as “divider” to the list.
if you are not comfortable with the data-* attributes then you can even trigger drop down using jQuery. Give a unique id to the link element and call the dropdown method as below:
To use the ScrollSpy feature you have to add data-spy=”scroll” and data-target=”#top-navigation” attribute to the body element. Here #top-navigation is the id of my navigation bar. Make sure the links in the navigation bar are internal links. See the demo page for a live example.
Twitter Bootstrap 3’s tabs take inspiration from traditional jQuery tabs. They both look and function alike. To use Bootstrap Tabs you need to define two separate sections: the tabs navigation and tab areas.
The markup goes like below:
<!-- Nav tabs --> <ul class="nav nav-tabs"> <li class="active"><a href="#home" data-toggle="tab">Home</a></li> <li><a href="#profile" data-toggle="tab">Profile</a></li> <li><a href="#messages" data-toggle="tab">Messages</a></li> <li><a href="#settings" data-toggle="tab">Settings</a></li> </ul> <!-- Tab panes --> <div class="tab-content"> <div class="tab-pane active" id="home">...</div> <div class="tab-pane" id="profile">...</div> <div class="tab-pane" id="messages">...</div> <div class="tab-pane" id="settings">...</div> </div>
Coming to the tabs area, it consists of a set of div elements. The parent div should have a class as “tab-content” and the child divs should have a class “tab-pane”. Each tab-pane must have an id corresponding to the internal links defined in the tabs navigation. In the above example, I have set a class of the first tab-pane as active. This makes it visible by default.
To use ToolTip, the markup goes like this:
<button id="myButton" type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="" data-original-title="Tooltip on left">Tooltip on left</button>
The above markup displays a button with the tooltip feature. Attribute “data-toggle” is used by Bootstrap to identify on which element it has to display the tooltip. Attribute “data-original” is used to define what goes inside the tooltip. Attribute “data-placement” is used to help bootstrap where to show the tooltip.
For performance reasons, Bootstrap will not initialize the ToolTip and Popover components by default. You have to initialize them manually by using the following jQuery:
If you have ever been a hardcore iBook reader, then you would understand what popovers are. They are the extended version of ToolTip with some more functionalities. You can display more HTML elements like img tags, links, additional divs, etc inside Popovers.
<button type="button" class="btn btn-default" data-toggle="popover" data-placement="left" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus." data-original-title="" title=""> Popover on left </button>
The HTML snippet displays a button with popover functionality. It also has set of custom data- attributes that you must necessarily understand.
Attribute “data-toggle” identifies which element must control the popover.
Attribute “data-content” contains the data that should be displayed inside the popover.
Attribute “data-placement” tells on which side should the popover appear.
In the above case, the data must be plain text only. If you want to display HTML content inside the popover, then you have to add the additional attribute “data-html” as true. The HTML data-content must go inside the double quotes with escaped characters wherever necessary.
The markup for the HTML data contents inside the popover should be like below:
<button id="myPopover" type="button" class="btn btn-default" data-toggle="popover" data-placement="left" data-html="true" data-content="<a href=\"http://www.google.com\">Go to google</a>" data-original-title="" title=""> Popover on left </button>
Use the below jQuery to initialize popovers:
Alerts by Twitter Bootstrap are not like window popups. They are a set of divs with predefined background colors and a dismiss button. The markup goes like below:
<div class="alert alert-warning fade in"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> <strong>Holy guacamole!</strong> Best check yo self, you're not looking too good. </div>
The above alert has a pale yellow background, since it is a warning message. You can change the color to red by changing the class of the alert to “alert-danger”. Every alert div must have a close button with a set of data- attributes as defined above. Attribute “data-dismiss” hides the alert div when clicked.
It has become a framework not only for the developers with zero CSS knowledge but also for designers to enhance their design turnover time. In my next tutorial, I will explain how to create sliders using Twitter Bootstrap 3’s Carousel component.
Hopefully, you have enjoyed reading my post. If you have any queries feel free to leave them in the comment section. I will be happy to help.
If you are new to Twitter Bootstrap then visit my author’s page on Sitepoint and read my previous Twitter Bootstrap Tutorials.
The Principles of Beautiful Web Design, 4th Edition
Docker for Web Developers
Learn PHP in One Day and Learn It Well