Table of Contents for
Bootstrap 4 – Responsive Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Bootstrap 4 – Responsive Web Design by Jason Marah Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. Bootstrap 4 – Responsive Web Design
  4. Bootstrap 4 – Responsive Web Design
  5. Credits
  6. Preface
  7. What you need for this learning path
  8. Who this learning path is for
  9. Reader feedback
  10. Customer support
  11. 1. Module 1
  12. 1. Getting Started
  13. Setting up the framework
  14. Building our first Bootstrap example
  15. Optionally using the CDN setup
  16. Community activity
  17. Bootstrap and web applications
  18. Browser compatibility
  19. Summary
  20. 2. Creating a Solid Scaffolding
  21. Building our scaffolding
  22. Fluid container
  23. We need some style!
  24. Manipulating tables
  25. Like a boss!
  26. Final thoughts
  27. Summary
  28. 3. Yes, You Should Go Mobile First
  29. Bootstrap and the mobile-first design
  30. How to debug different viewports at the browser
  31. Cleaning up the mess
  32. Creating the landing page for different devices
  33. Summary
  34. 4. Applying the Bootstrap Style
  35. Summary
  36. 5. Making It Fancy
  37. Paying attention to your navigation
  38. Dropping it down
  39. Making an input grouping
  40. Getting ready for flexbox!
  41. Summary
  42. 6. Can You Build a Web App?
  43. Adding the navigation
  44. Do a grid again
  45. Playing the cards
  46. Implementing the main content
  47. Creating breadcrumbs
  48. Finishing with the right-hand-side content
  49. Summary
  50. 7. Of Course, You Can Build a Web App!
  51. Waiting for the progress bar
  52. Creating a settings page
  53. Summary
  54. 8. Working with JavaScript
  55. Awesome Bootstrap modals
  56. Creating our custom modal
  57. A tool for your tip
  58. Pop it all over
  59. Making the menu affix
  60. Finishing the web app
  61. Summary
  62. 9. Entering in the Advanced Mode
  63. The last navigation bar with flexbox
  64. Filling the main fluid content
  65. Filling the main content
  66. Overhead loading
  67. Fixing the toggle button for mobile
  68. Summary
  69. 10. Bringing Components to Life
  70. Fixing the mobile viewport
  71. Learning more advanced plugins
  72. Summary
  73. 11. Making It Your Taste
  74. Working with plugin customization
  75. The additional Bootstrap plugins
  76. Creating our Bootstrap plugin
  77. Defining the plugin methods
  78. Creating additional plugin methods
  79. Summary
  80. 2. Module 2
  81. 1. Introducing Bootstrap 4
  82. Summary
  83. 2. Using Bootstrap Build Tools
  84. Download the Bootstrap source files
  85. Setting up the blog project
  86. Setting up the JSON files
  87. Creating our first page template
  88. Summary
  89. 3. Jumping into Flexbox
  90. Ordering your Flexbox
  91. Wrapping your Flexbox
  92. Setting up the Bootstrap Flexbox layout grid
  93. Setting up a Flexbox project
  94. Designing a single blog post
  95. Summary
  96. 4. Working with Layouts
  97. Inserting rows into your layout
  98. Adding columns to your layout
  99. Choosing a column class
  100. Creating a simple three-column layout
  101. Mixing column classes for different devices
  102. Coding the blog home page
  103. Using responsive utility classes
  104. Summary
  105. 5. Working with Content
  106. Learning to use typography
  107. Customizing headings
  108. How to style images
  109. Coding tables
  110. Summary
  111. 6. Playing with Components
  112. Basic button examples
  113. Creating outlined buttons
  114. Checkbox and radio buttons
  115. Coding forms in Bootstrap 4
  116. Creating an inline form
  117. Adding validation to inputs
  118. Using the Jumbotron component
  119. Adding the Label component
  120. Using the Alerts component
  121. Using Cards for layout
  122. Updating the Blog index page
  123. How to use the Navs component
  124. Adding Breadcrumbs to a page
  125. Using the Pagination component
  126. How to use the List Group component
  127. Summary
  128. 7. Extending Bootstrap with JavaScript Plugins
  129. Coding Tooltips
  130. Avoiding collisions with our components
  131. Using Popover components
  132. Using the Collapse component
  133. Coding an Accordion with the Collapse component
  134. Coding a Bootstrap Carousel
  135. Summary
  136. 8. Throwing in Some Sass
  137. Using Sass in the blog project
  138. Importing partials in Sass
  139. Creating a collection of variables
  140. Customizing components
  141. Writing a theme
  142. Summary
  143. 9. Migrating from Version 3
  144. Big changes in version 4
  145. Updating your variables
  146. Additional global changes
  147. Other font updates
  148. Migrating components
  149. Migrating JavaScript
  150. Miscellaneous migration changes
  151. Summary
  152. 3. Module 3
  153. 1. Revving Up Bootstrap
  154. What Bootstrap 4 Alpha 4 has to offer
  155. Setting up our project
  156. Summary
  157. 2. Making a Style Statement
  158. Image elements
  159. Responsive utilities
  160. Helper classes
  161. Text alignment and transformation
  162. Summary
  163. 3. Building the Layout
  164. Adding Bootstrap components
  165. Summary
  166. 4. On Navigation, Footers, Alerts, and Content
  167. Improving navigation using Scrollspy
  168. Customizing scroll speed
  169. Icons
  170. Using and customizing alerts
  171. Creating a footer
  172. Creating and customizing forms
  173. Form validation
  174. Progress indicators
  175. Adding content using media objects
  176. Figures
  177. Quotes
  178. Abbreviations
  179. Summary
  180. 5. Speeding Up Development Using jQuery Plugins
  181. Enhanced pagination using bootpag
  182. Displaying images using Bootstrap Lightbox
  183. Improving our price list with DataTables
  184. Summary
  185. 6. Customizing Your Plugins
  186. Customizing plugins
  187. Writing a custom Bootstrap jQuery plugin
  188. Summary
  189. 7. Integrating Bootstrap with Third-Party Plugins
  190. Hover
  191. Summary
  192. 8. Optimizing Your Website
  193. Minifying CSS and JavaScript
  194. Introducing Grunt
  195. Running tasks automatically
  196. Stripping our website of unused CSS
  197. JavaScript file concatenation
  198. Summary
  199. 9. Integrating with AngularJS and React
  200. Introducing React
  201. Summary
  202. Bibliography
  203. Index

Chapter 5. Speeding Up Development Using jQuery Plugins

The previous chapter showed us how to style the content for MyPhoto and how to improve a website's overall appearance by using and customizing different Bootstrap components. We learned how to use Bootstrap's navbar, how to use icons, and how to customize a website's scrolling behavior using the Scrollspy plugin. In this chapter, we will emphasize the power of third-party plugins, introducing you to some essential third-party (and hence non-Bootstrap) plugins that will help speed up the development of the most common and mundane features. Building on the features implemented throughout the previous chapters, we will first teach you how to quickly and efficiently implement client-side browser detection using the jQuery browser plugin (jquery.browser). We will then improve the display of our tabular Events section by using pagination, first covering Bootstrap's pagination, and then showing you how to rapidly improve the default pagination feature using bootpag, Bootstrap's pagination plugin. Further improvements to our Events section will be made by adding images using Bootstrap Lightbox. Finally, staying within our tabular data display, we will improve the display of MyPhoto's price list using jQuery DataTables.

To summarize, this chapter will cover the following:

  • Browser detection using the jQuery browser plugin
  • Improved pagination using bootpag
  • Using Bootstrap Lightbox to display images
  • Enhancing the display of tabular data using DataTables

Browser detection

Recall the hypothetical example from Chapter 4, On Navigation, Footers, Alerts, and Content, where MyPhoto only supports browsers above certain versions. To this end, we added a Bootstrap alert to our page, which notified visitors that their browser is not supported. Up until now, however, we had no way to actually identify which browser or browser version a MyPhoto visitor was using. Lacking any logic to hide and display the alert, the alert was visible regardless of whether or not the user's browser was actually supported by our website. Now the time has come for us to implement this missing logic.

Web browsers identify themselves by specifying their name and version information using a special field called User-Agent, which is part of the HTTP Request Header (see Figure 5.1). JavaScript allows users to access this field using the window.navigator property. This property contains the exact same string that is present in the User-Agent field of the HTTP Request Header. Therefore, to determine whether our visitor's browser is indeed supported, all that one needs to do is match the supported browser against the string presented by window.navigator. However, as can be seen from Figure 5.1, these strings are typically quite long and complex. As a result, matching different browsers can be tedious and prone to programming errors. Therefore, it is much better to use external resources that do this matching for us, and have been well tested, well documented, and are kept up to date. The popular, open source jQuery browser plugin (https://github.com/gabceb/jquery-browser-plugin) is one such resource that we can use. Let's go ahead and do just that! As usual, go ahead and fire up your console and install the plugin using Bower:

    bower install jquery.browser

Once the installation is complete, you should see a new directory under

bower_components:

bower_components/jquery.browser

Inside the dist folder, you should see two files, namely,  jquery.browser.js and jquery.browser.min.js.

Take a look at the following screenshot:

Browser detection

Figure 5.1: An example of a User-Agent string transmitted as part of the HTTP header when making a request.

If you can see the aforementioned files, then this means that jquery.browser has been successfully installed. Now, before we start using jquery.browser, we first need to work some more on our alert. The first thing that we will need to do is find a way of uniquely identifying it. Let's use the HTML id attribute for this purpose. Add the id attribute to our alert: id="unsupported-browser-alert".

Next, we should tidy up our markup. Go ahead and open css/myphoto.css and move the inline styles for our alert into our style sheet.

Tip

As previously stated, we must eliminate inline styles at all costs. While we may, at times, apply inline styles to keep sample code snippets short, you should always try to avoid doing so outside the classroom.

This won't affect browser detection, but will simply allow us to keep our markup nice and tidy. Observe the following code:

    #unsupported-browser-alert { 
        position: fixed; 
        margin-top: 4em; 
        width: 90%; 
        margin-left: 4em; 
    } 

Note

Generally, it is not a good idea to couple CSS rules with an id, as this is bad for reusability. But hold tight; we will talk more about this in Chapter 6, Customizing Your Plugins.

Since we only want the alert to display under certain conditions (namely, if the user is using a specific version of Internet Explorer), we should hide the alert div by default. Go ahead and add the following CSS to our alert styles:

    display: none;

Save and refresh. The alert at the top of the page should now no longer be visible. Furthermore, our outermost alert div should now only contain id and class attributes:

    <div class="alert alert-danger" id="unsupported-browser-alert">
        <a href="#" class="close" data-dismiss="alert" aria-label="close"
        >&times;</a> 
<strong class="alert-heading"><i class="fa fa-exclamation"></i>
        Unsupported browser</strong> Internet Explorer 8 and lower are
        not supported by this website.
    </div>

Now it is finally time to start using our freshly installed jQuery plugin. Open the MyPhoto index.html file and include the minified jquery.browser JavaScript file within the head of the document:

    <script
    src="bower_components/jquery.browser/dist/jquery.browser.min.js">         
    </script> 
    Great. The head of our index.html should now look as follows: 
    <head> 
        <meta charset="UTF-8"> 
        <title>ch05</title> 
        <link rel="stylesheet" href="bower_components/bootstrap/dist/css
        /bootstrap.min.css" /> 
        <link rel="stylesheet" href="styles/myphoto.css" /> 
        <link rel="stylesheet" href="bower_components/components-
        font-awesome/css/font-awesome.min.css" /> 
        <script src="bower_components/jquery/dist/jquery.min.js"></script> 
        <script src="bower_components/bootstrap/dist/js/bootstrap.min.js">
        </script> 
        <script src=
        "bower_components/jquery.browser/dist/jquery.browser.min.js">
        </script> 
        <script type="text/javascript"> 
            $( document ).ready(function() { 
                $("nav ul li a").on('click', function(evt) { 
                    evt.preventDefault(); 
                    var offset = $(this.hash).offset(); 
                    if (offset) { 
                        $('body').animate({ 
                            scrollTop: offset.top 
                        }); 
                    } 
                }); 
            }); 
        </script> 
    </head> 

We are now ready to use jquery.browser to detect which browser the visitor is using. To this end, and as noted in the jquery.browser documentation, the following variables are are available:

  • $.browser.msie : This is true if the website visitor is using Microsoft Internet Explorer.
  • $.browser.webkit : This is  true if the website visitor is using either Chrome, Safari, or Opera.
  • $.browser.version : This shows the browser version (not type!) that the website visitor is using.

To add the logic that makes the alert visible, let us start with a general condition of whether or not the user is using Internet Explorer. We will move to a more specific condition later (whether the user is using a specific version of Internet Explorer). In other words, let us begin by making the browser alert visible only if the visitor's browser is identifying itself as Internet Explorer. The code for this is fairly straightforward. We simply check the $.browser.msie variable. If this variable evaluates to true, then we use jQuery to make our alert visible:

    if ($.browser.msie) { 
        $('#unsupported-browser-alert').show(); 
    } 

Now let us make our browser test more specific. We now want to test whether the visitor is using both Internet Explorer and whether the version of Internet Explorer (let's say version 8 and below) is unsupported by MyPhoto. To do so, we simply perform a second check using the $.browser.version variable. If the conditional evaluates to true, then the show() function is executed. The show() function modifies the display rule of an element to make it visible:

    if ($.browser.msie && $.browser.version <= 8) { 
        $('#unsupported-browser-alert').show(); 
    }

Go ahead and insert this snippet into the head of our HTML document:

    <script type="text/javascript"> 
        $( document ).ready(function() { 
            $("nav ul li a").on('click', function(evt) { 
                evt.preventDefault(); 
                var offset = $(this.hash).offset(); 
                if (offset) { 
                    $('body').animate({ 
                        scrollTop: offset.top 
                    });

                }

            }); 
            if ($.browser.msie && $.browser.version <= 7) { 
                $('#unsupported-browser-alert').show(); 
            }
    
        });

    </script>

Note

Conditional comments with Internet Explorer

If you wished to target Internet Explorer only for specific portions of your markup, then you can use Microsoft's conditional comments with Internet Explorer. Other browsers that are not Internet Explorer will simply ignore these proprietary comments:     

<!--[if IE 8]-->
         <insert IE specific markup here>
     [endif]-->