Bootstrap includes a huge collection of javascript plugins namely collapsibles, accordions, tab panels, modals, popovers, tooltips, toasts and scrollspy which are made easy to add in webpages by simply foloowing their structures and adding their classes.
Bootstrap collapse plugin is used to show and hide contents on click of a button or a link. Create a collapsible container by adding class collapse to it. Add a unique id to the container. Add a link or a button and add attribute data-toggle="collapse" and add another attribute data-target="uniqueid" and it's done.
<button class="btn btn-primary" data-toggle="collapse" data-target="#collapseExample">Toggle Collapse</button> <div class="collapse" id="collapseExample"> <h4>Collapse Title</h4> <p>This is a collapse content.</p> </div>
This is a collapse content.
Collapse can be used to toggle multiple elements at once. Since, W3CValidator doesn't validate a webpage with same id in multiple elements, it is a good idea to use classes.
Collapse content element 1.
Collapse content element 2.
Grouping multiple collapsibles and forcing only one open at a time is a good idea to optimize user experience as well as page length. Wrap the group of collapsibles with a class accordion and give an id. Add attribute data-parent="accordionid" to all the collapse containers to achieve it. Add class show to one of the collapse container to open it by default.
<div class="accordion" id="accordionExample"> <div class="card"> <div class="card-header"> <a href="#collapse1" class="btn btn-link" data-toggle="collapse">Link to open collapse 1</a> </div> <div class="collapse show" data-parent="#accordionExample" id="collapse1"> <div class="card-body"> This is demo content for collapse 1. </div> </div> </div> <div class="card"> <div class="card-header"> <a href="#collapse2" class="btn btn-link" data-toggle="collapse">Link to open collapse 2</a> </div> <div class="collapse" data-parent="#accordionExample" id="collapse2"> <div class="card-body"> This is demo content for collapse 2. </div> </div> </div> <div class="card"> <div class="card-header"> <a href="#collapse3" class="btn btn-link" data-toggle="collapse">Link to open collapse 3</a> </div> <div class="collapse" data-parent="#accordionExample" id="collapse3"> <div class="card-body"> This is demo content for collapse 3. </div> </div> </div> </div>
Bootstrap tab plugin is used to create tabbable panes quite similar to accordions. Create a tab list with list group and then create a container with class tab-pane and a unique id to refer to each links in the list group. Wrap all tab panes with a class tab-content. Add class fade to each tab pane to hide them and add some transition. Add class show active to one of the tab panes to show it by default.
<div class="row"> <div class="col-sm-4"> <div class="list-group"> <a class="list-group-item list-group-item-action active" href="#tab1" data-toggle="list">Tab 1</a> <a class="list-group-item list-group-item-action" href="#tab2" data-toggle="list">Tab 2</a> <a class="list-group-item list-group-item-action" href="#tab3" data-toggle="list">Tab 3</a> </div> </div> <div class="col-sm-8"> <div class="tab-content"> <div class="tab-pane fade show active" id="tab1"> <h4>Tab 1 Title</h4> <p>This is a demo content for tab 1.</p> </div> <div class="tab-pane fade" id="tab2"> <h4>Tab 2 Title</h4> <p>This is a demo content for tab 2.</p> </div> <div class="tab-pane fade" id="tab3"> <h4>Tab 3 Title</h4> <p>This is a demo content for tab 3.</p> </div> </div> </div> </div>
This is a demo content for tab 1.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This is a demo content for tab 2.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This is a demo content for tab 3.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This is a demo content for tab 4.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Use nav classes nav-tab or nav-pills to display the list items horizontally and make them tabbable links. Add attribute data-toggle="tab" for nav-tabs and data-toggle="pill" for nav-pills. Each support both attributes though.
<ul class="nav nav-tabs"> <li class="nav-item"><a href="#navtab1" data-toggle="tab" class="nav-link active">Nav Tab 1</a></li> <li class="nav-item"><a href="#navtab2" data-toggle="tab" class="nav-link">Nav Tab 2</a></li> <li class="nav-item"><a href="#navtab3" data-toggle="tab" class="nav-link">Nav Tab 3</a></li> </ul> <div class="tab-content mb-2"> <div class="tab-pane fade show active" id="navtab1"> <h4 class="mt-2">Nav Tab 1</h4> <p>This is a demo content for nav tab 1.</p> </div> <div class="tab-pane fade" id="navtab2"> <h4 class="mt-2">Nav Tab 2</h4> <p>This is a demo content for nav tab 2.</p> </div> <div class="tab-pane fade" id="navtab3"> <h4 class="mt-2">Nav Tab 3</h4> <p>This is a demo content for nav tab 3.</p> </div> </div>
Bootstrap modal plugin is used to add popup dialog boxes to the webpage which are loaded on the webpage once the trigger is activated. The trigger might be any user interaction like click, hover, select, etc or simply the page loading event.
<button class="btn btn-info" data-toggle="modal" data-target="#modalExample">Open Modal</button> <div class="modal fade" tabindex="-1" id="modalExample"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <a href="#" class="close" data-dismiss="modal">×</a> </div> <div class="modal-body"> <p>Main content of the modal is added here.</p> </div> <div class="modal-footer"> <button class="btn btn-primary" data-dismiss="modal">Close</button> </div> </div> </div> </div>
Bootstrap tooltip plugin is basically a different styling to the default tooltip of browsers that display the content inside the title attribute of any element. For performance reasons, tooltip is not initialized by default, so you must add the following javascript code in your page to activate them.
<script> $('[data-toggle="tooltip"]').tooltip() </script>
Add attribute data-toggle="tooltip" to any element with their title attribute defined and you'll see the bootstrap tooltip appear. You can also define the placement of the tooltip with attribute data-placement="" where the available values are top, right, bottom and left. The default data placement is top.
<a href="#" title="This is a demo tooltip" data-toggle="tooltip" data-placement="right">Tooltip Example (Hover over me)</a>
Bootstrap popover plugin is quite similar to tooltip plugin but it is activated on focus by default. You can activate it on hover by adding attribute data-trigger="hover". Moreover, it allows you to add title and a long description too.
Popovers are also not initialized by default for performance reason. Add the following script on your webpage to initialize them.
<script> $('[data-toggle="popover"]').popover() </script>
<a href="#" title="Popover Example" data-description="This is a popover example content" data-trigger="hover">Popover Example (Hover over me)</a>
Bootstrap toasts plugin is a light weight alert message similar to push notifications widely used by mobile and desktop apps. Toasts are stacked vertically in a readable manner.
Toasts also need to be initialized. Add the following javascript code to initialize all toasts.
<script> $('.toast').toast('show'); </script>
Toasts have an attribute data-autohide="true" which makes them quickly disappear from the page, so, you need to either add attribute data-delay="" where the value should be in miliseconds or override attribute data-autohide="true" by adding attribure data-autohide="false" which will force the toasts be displayed unless the close button is clicked. Make sure that you have added the data-dismiss button.
<div class="toast" data-autohide="false"> <div class="toast-header"> <strong class="mr-auto">Bootstrap</strong> <small>11 mins ago</small> <a href="#" class="ml-2 mb-1 close" data-dismiss="toast">×</a> </div> <div class="toast-body"> This is a toast alert example. </div> </div>
Bootstrap scrollspy plugin is highly useful specially on single page websites as it detects which section of the page is being displayed on the viewport and turns the link on the navbar for the same section active.
If you are using scrollspy on any element other than the <body> itself, you must define its position as relative with a height set and overflow-y set as scroll. It works fine with nested navigation too.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<nav id="scrollspyExample" class="navbar navbar-light bg-light"> <a class="navbar-brand" href="#">Scrollspy Example</a> <ul class="nav nav-pills"> <a class="nav-item nav-link" href="#section1">Section 1</a> <a class="nav-item nav-link" href="#section2">Section 2</a> <a class="nav-item nav-link" href="#section3">Section 3</a> </ul> </nav> <div data-spy="scroll" data-target="#scrollspyExample" data-offset="0" class="position-relative"> <div id="section1" class="my-4"> <h4>Section 1 Title</h4> <p>.....</p> </div> <div id="section2" class="my-4"> <h4>Section 2 Title</h4> <p>.....</p> </div> <div id="section3" class="my-4"> <h4>Section 3 Title</h4> <p>.....</p> </div> </div>
0 Comment
Leave a comment