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 8. Optimizing Your Website

Loosely put, website optimization refers to the activities and processes that improve your website's user experience and visibility while reducing the costs associated with hosting your website. In his book Website Optimization, Andrew B. King summarizes this notion succinctly with the question: "How do we make our website better?" (Website Optimization, Andrew B. King, O'Reilly). As such, the topic has been the sole subject of entire books, and a single chapter barely touches the tip of the iceberg. The topic is one of many facets, ranging from server-side optimization, search engine optimization, pay-per-click optimization, and client-side optimization. In this chapter, we will only discuss the latter. That is, we will be improving the loading and rendering time of MyPhoto. Specifically, this chapter is concerned with:

  • Speeding up the loading time of our MyPhoto index.html
  • Automating the tasks that achieve this objective

By the end of this chapter, you will understand the essential techniques behind client-side optimization. Within the context of MyPhoto you will therefore learn how to:

  • Reduce the overall number of HTTP requests required to render our web page
  • Automatically remove unused CSS rules
  • Make our JavaScript and CSS files smaller (commonly referred to as minification)
  • Automate the various optimization tasks
  • Optimize CSS rules

CSS optimization

Before we even consider compression, minification, and file concatenation, we should think about the ways in which we can simplify and optimize our existing style sheet without using third-party tools. Of course, we should have striven for an optimal style sheet to begin with, and in many aspects we did. However, our style sheet still leaves room for improvement. Some of these improvements we have ignored on purpose within the previous chapters, as they would have detracted from the chapter's intended purpose. However, as this chapter is concerned with optimizing the client-side code of a web page, the time has come to talk a little about general tips and practices that will help you keep your style sheets small and your rules short. We will address each of these tips and practices in turn.

Inline styles

If, after reading this chapter, you only remember one thing, then please let it be the following: inline styles are bad. Period. Avoid using them whenever possible. Why? Because not only will they make your website impossible to maintain as the website grows, they also take up precious bytes as they force you to repeat the same rules over and over. Consider the following markup for our Gallery section:

    <div class="carousel-inner" role="listbox"> 
        <div style="height: 400px" class="carousel-item active"> 
            <img data-modal-picture="#carouselModal" src=
            "images/brazil.png"> 
            <div class="carousel-caption"> 
                Brazil 
            </div> 
        </div> 
        <div style="height: 400px" class="carousel-item"> 
            <img data-modal-picture="#carouselModal" src=
            "images/datsun.png"> 
            <div class="carousel-caption"> 
                Datsun 260Z 
            </div> 
        </div> 
        <div style="height: 400px" class="carousel-item"> 
            <img data-modal-picture="#carouselModal" src=
            "images/skydive.png"> 
            <div class="carousel-caption"> 
                Skydive 
            </div> 
        </div> 
    </div> 

Notice how the rule for defining a gallery item's height, style="height: 400px", is repeated three times, once for each of the three gallery items. That's an additional 21 characters (or 21 bytes assuming that our document is UTF-8) for each additional image. Multiplying 3 * 21 gives us 63 bytes. And 21 more bytes for every new image that you want to add to the Gallery. Not to mention that, if you ever want to update the height of the gallery images, you will need to manually update the style attribute for every single image. The solution is of course to replace the inline styles with an appropriate class. Let's go ahead and define an img class that can be applied to any carousel image:

    .carousel-item { 
        height: 400px; 
    } 

Now let's go ahead and remove the style rules:

    <div class="carousel-inner" role="listbox"> 
        <div class="carousel-item active"> 
            <img data-modal-picture="#carouselModal" src=
            "images/brazil.png"> 
            <div class="carousel-caption"> 
                Brazil 
            </div> 
        </div> 
        <div class="carousel-item"> 
            <img data-modal-picture="#carouselModal" src=
            "images/datsun.png"> 
            <div class="carousel-caption"> 
                Datsun 260Z 
            </div> 
        </div> 
        <div class="carousel-item"> 
            <img data-modal-picture="#carouselModal" src=
            "images/skydive.png"> 
            <div class="carousel-caption"> 
                Skydive 
            </div> 
        </div> 
    </div> 

Great! Not only is our CSS now easier to maintain, we also shaved 29 bytes off our website (the original inline styles required 63 bytes; our new class definition, however, requires only 34 bytes). Yes, this does not seem like much, especially in the world of high-speed broadband. But remember, your website will grow, and every byte adds up.

There are several more inline styles spread around our HTML document. Go ahead and fix them before moving on to the next section.

Long identifier and class names

The longer your strings, the larger your files. It's a no-brainer. As such, long identifier and class names naturally increase the size of your web page. Of course, extremely short class or identifier names tend to lack meaning, and therefore will make it more difficult (if not impossible) to maintain your page. As such, one should strive for an ideal balance between length and expressiveness (we will be covering a handy little tool that will provide you with the benefits of both later on in this chapter). Of course, even better than shortening identifiers, is to remove them altogether. One handy technique for removing these is to use hierarchical selection. Take our events pagination code. For example, we are using the services-events-content identifier within our pagination logic as follows:

    $('#services-events-pagination').bootpag({ 
        total: 10 
        }).on("page", function(event, num){ 
            $('#services-events-content div').hide(); 
 
            var current_page = '#page-' + num; 
            $(current_page).show(); 
    }); 

To denote the services content, we broke the name of our identifier into three parts, namely, services, events, content . Our markup is as follows:

    <div id="services-events-content"> 
        <div id="page-1"> 
            <h3>My Sample Event #1</h3> 
            ... 
        </div> 
    </div> 

Let's try and get rid of this identifier altogether by observing two characteristics of our Events section:

  • The services-events-content is an indirect descendent of a div with the id services-events. This id we cannot remove, as it is required for the menu to work.
  • The element with the id services-events-content is itself a div. If we were to remove its id, we could also remove the entire div.

As such, we do not need a second identifier to select the pages that we wish to hide. Instead, all that we need to do is select the div within the div that is within the div that is assigned the id services-events. How do we express this as a CSS selector? Easy. Use #services-events div div div. And as such, our pagination logic is updated as follows:

    $('#services-events-pagination').bootpag({ 
        total: 10 
    }).on("page", function(event, num){ 
        $('#services-events div div div').hide(); 
        var current_page = '#page-' + num; 
        $(current_page).show(); 
    }); 

Save and refresh. What's that? As you clicked on a page, the pagination control disappeared. That is because we are now hiding all div elements that are two div elements down from the element with the id services-events. Move the pagination control div outside its parent element. Our markup should now look as follows:

    <div class="tab-content bg-myphoto-light"> 
        <div role="tabpanel" class="tab-pane active" id=
        "services-events"> 
            <div class="container"> 
                <div class="row" style="margin: 1em;"> 
                    <div id="page-1"> 
                        <h3>My Sample Event #1</h3> 
                        ... 
                        <h3>My Sample Event #2</h3> 
                        ... 
                    </div> 
                    <div id="page-2"> 
                        <h3>My Sample Event #3</h3> 
                        ... 
                    </div> 
                </div> 
            <div id="services-events-pagination"></div> 
            </div> 
        </div> 
    </div> 

Save and refresh. That's better! Last but not least, let us update myphoto.css. Take the following code:

    #services-events-content div { 
        display: none; 
    } 
    #services-events-content div img { 
        margin-top: 0.5em; 
       margin-right: 1em; 
    } 
    #services-events-content { 
        height: 15em; 
        overflow-y: scroll; 
    }

Replace this code with:

    #services-events div div div { 
        display: none; 
    } 
    #services-events div div div img { 
        margin-top: 0.5em; 
        margin-right: 1em; 
    } 
    #services-events div div div { 
        height: 15em; 
        overflow-y: scroll; 
    } 

That's it, we have simplified our style sheet and saved some bytes in the process!

Shorthand rules

According to, the Mozilla Developer Network (shorthand properties, Mozilla Developer Network, https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties, accessed November 2015), shorthand properties are:

"CSS properties that let you set the values of several other CSS properties simultaneously. Using a shorthand property, a Web developer can write more concise and often more readable style sheets, saving time and energy."                                                                                    – Mozilla Developer Network, 2015

Unless strictly necessary, we should never be using longhand rules. When possible, shorthand rules are always the preferred option. Besides the obvious advantage of saving precious bytes, shorthand rules also help increase your style sheet's maintainability. For example, border: 20px dotted #FFF is equivalent to three separate rules:

    border-style: dotted;
    border-width: 20px;
    border-color: #FFF; 

Grouping selectors

Organizing selectors into groups will arguably also save some bytes. Consider lines 80 to 93 in myphoto.css:

    .navbar-myphoto .dropdown-menu > a:hover 
{
        color: gray;
        background-color: #504747;
    }
    .navbar-myphoto .dropdown-menu > a:focus
 {
        color: gray;
        background-color: #504747;
    }
    .navbar-myphoto .dropdown-menu > .active > a:focus 
{
        color: gray;
        background-color: #504747;
    }

Notice how each of the three selectors contains the same declarations. That is, the color and background-color properties are set to the exact same values for each selector. To prevent us from repeating these declarations, we should simply group them (reducing the code from 274 characters down to 181 characters):

    .navbar-myphoto .dropdown-menu > a:hover,
    .navbar-myphoto .dropdown-menu > a:focus,
    .navbar-myphoto .dropdown-menu > .active > a:focus { 
        color: gray;
        background-color: #504747;
    }
 

Voilà! We just saved 93 bytes! (assuming UTF-8 encoding).

Rendering times

When optimizing your style rules, the number of bytes should not be your only concern. In fact, it comes secondary to the rendering time of your web page. CSS rules affect the amount of work that is required by the browser to render your page. As such, some rules are more expensive than others. For example, changing the color of an element is cheaper than changing its margin. The reason for this is that a change in color only requires your browser to draw the new pixels. While drawing itself is by no means a cheap operation, changing the margin of an element requires much more effort. Your browser needs to both re-calculate the page layout and also draw the changes. Optimizing your page's rendering times is a complex topic, and as such beyond the scope of this book.

However, we recommend that you take a look at http://csstriggers.com/ . This site provides a concise overview of the costs involved when updating a given CSS property.

Note

Did you know?

Udacity now offers a free online course on Browser Rendering Optimization. Head over to https://www.udacity.com for more. We cannot recommend the course highly enough!