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

Using responsive utility classes

Responsive utility classes will allow you to selectively hide <div>s or components based on the screen resolution size. This is great for creating a mobile-first web application, because in many cases you'll want to hide some components that don't work well on a phone or tablet. Mobile application design generally means a simpler, more minimal experience some using responsive utility classes will allow you to achieve this. Open up index.ejs in a text editor and go down to the sidebar <div>, then add the .hidden-md-down class to your code:

<div class="col-md-4 hidden-md-down"> 

Adding this class will hide the <div> from the browser when your screen resolution is smaller than 720 pixels. Make sure your column class, in this case -md, matches the hidden class. Now, if you shrink your web browser down again, you'll notice that the sidebar <div> will disappear.

There are a number of responsive utility classes you can use in your projects. There is a -down version for each of the column class names. You can also use a -up version if you wish to hide something when viewing at a larger resolution. Some examples of that class are .hidden-lg-up or .hidden-xl-up. To learn more about responsive utility classes, check out the page in the Bootstrap docs at http://v4-alpha.getbootstrap.com/layout/responsive-utilities/ .

That completes the layout of the grid for our blog home page. Before we move onto learning about content classes in Bootstrap 4, let's set up the layout grid for the other pages we'll be building for the blog.

Coding the additional blog project page grids

Before we create new templates for our contact and single blog post pages, we need to update some of the Harp project files. Let's update those files, then we'll move onto the page templates.

Updating _data.json for our new pages

Remember a couple chapters back we learned how to code the _data.json file and we created a variable for the page title of each of our templates? We need to update this file for our two new pages by providing the pageTitle variable value for each of them. Open up _data.json in a text editor; you can find the file in the root of your blog project directory. Once you have the file open, insert the following code. The entire file should read as follows:

{ 
   "index": { 
      "pageTitle": "Home" 
   }, 
   "contact": { 
      "pageTitle": "Contact" 
   }, 
   "blog-post": { 
      "pageTitle": "Blog Post" 
   } 
} 

Originally, we only included the index file. I've added two more entries, one for the contact page and one for the blog-post page. I've entered a value for each page's pageTitle variable. It's as simple as that. Save the JSON file and then you can close it.

Creating the new page templates

Now that _data.json has been updated, we need to create the actual page template EJS files like we did with index. In your text editor, create two new files and save them as contact.ejs and blog-post.ejs. For now, just leave them blank and we'll start to fill them in the next steps. The templates are now set up and ready to be coded. For now, both of our new pages will use the same _layout.ejs file as the index file, so there is no need to create any more layouts. Let's start by coding the contact page template.

Coding the contact page template

Open up the contact.ejs file you just created in your text editor. Let's start the template by setting up our page title. Enter the following code into the file:

<div class="container"> 
  <!-- page title //--> 
  <div class="row m-t-3"> 
    <div class="col-lg-12"> 
      <h1>Contact</h1> 
    </div> 
  </div> 
</div> 

Let's breakdown what's happening here in the code:

  • I've opened up the file with a <div> with a .container class on it.
  • Next I added .row <div> and I've added the same m-t-3 spacing classes so it matches the blog home page.
  • I've added a column <div> with a class of .col-md-12. Since this is our page title, we want it to stretch to the width of our layout.
  • Finally, I've added an <h1> tag with our contact page title.

Adding the contact page body

Next let's insert our grid layout for the body of the contact page. Following the page title code, insert the following grid code:

<!-- page body //--> 
  <div class="row m-t-3"> 
    <div class="col-md-12"> 
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
    </div> 
  </div> 

Let's review the code for the page body:

  • I've opened up another row <div> for the page body. It also has the same m-t-3 spacing class on it for consistent vertical spacing.
  • I've used the col-md-12 column class again because the contact page layout will fill the whole width of our container.
  • I've added some filler text for now so that we can verify that the page is laid out properly.

Before we finish, let's add one more row for our mailing list section. I'd like this to be available on every page of our blog. The grid code for this section will be identical to the markup we did for the page body. Here's what it looks like, for reference:

<!-- mailing list //--> 
<div class="row m-t-3"> 
  <div class="col-md-12"> 
   <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
  </div> 
</div> 

Since this code is identical to the page body, I won't bother breaking it down again. Our layout for the contact page is now complete. Make sure you save the file and let's test it before we move onto the blog post page.

Open your Terminal back up and navigate to the root directory of the blog project. Once there, run the harp compile command and then the Harp server command to launch the local web server. Open your web browser and enter the following URL to preview your page: http://localhost:9000/contact.html.

Your contact page should load up and you should see a page title and two rows of filler text. Here's what it should look like:

Adding the contact page body

Our contact page grid is now complete. Before we move onto creating the blog post template, let's take a look at all the code for the contact template:

<div class="container"> 
  <!-- page title //--> 
  <div class="row m-t-3"> 
    <div class="col-md-12"> 
      <h1>Contact</h1> 
    </div> 
  </div> 
  <!-- page body //--> 
  <div class="row m-t-3"> 
    <div class="col-md-12"> 
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
    </div> 
  </div> 
  <!-- mailing list //--> 
  <div class="row m-t-3"> 
    <div class="col-md-12"> 
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
    </div> 
  </div> 
</div> 

Coding the blog post template

Head back to your text editor and open the file blog-post.ejs that you previously created. Like our contact page template, let's start by first setting up the page title section of code. Enter the following code into the blog post template file:

<div class="container"> 
  <!-- page title //--> 
  <div class="row m-t-2 m-b-2"> 
    <div class="col-lg-12"> 
      <h1>Post Title</h1> 
    </div> 
  </div> 
</div> 

As you can see, this code is almost identical to the contact page. There are two small differences that I will point out for you:

  • I've changed up the spacing classes on the row <div>. In a future chapter, we are going to add some different components around the page title, so I've altered the vertical spacing to allow for them. I'm using the same margin top spacer but I've only set it to a value of 2. I've added a second margin bottom spacer with a value of 2 with the .m-b-2 class. Switching the -t to a -b will apply a bottom margin instead a of top margin.
  • I've changed the page title to Post Title in the <h1> tag.

Adding the blog post feature

The body of our blog post will have some different elements compared to the blog home template. After the page title, I'm going to insert a feature section that will be used for an image or carousel in a future chapter. For now, let's just lay in the grid column and some filler text for testing purposes. Enter the following code after the page title section:

<!-- feature //--> 
<div class="row"> 
  <div class="col-md-12"> 
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
  </div> 
</div> 

This is a very simple section. Notice the row <div> doesn't have a spacer class on it, since we added the bottom margin to the page title section. I've inserted a full-width col-md-12 column class so the feature can stretch to the width of the layout.

Adding the blog post body

Now that we've added the blog post feature section, let's add the actual body part of the template. This section will use the same layout as our blog home page. It will be a two-column layout, the first being 2/3 wide, and the sidebar being 1/3 of the layout. Insert the following code after the feature section:

<!-- page body //--> 
<div class="row m-t-2"> 
  <div class="col-md-8"> 
     <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
   </div> 
   <!-- sidebar //--> 
   <div class="col-md-4 hidden-md-down"> 
    <p>sidebar</p> 
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
  </div> 
</div> 

Let's break down what's happening here in the code:

  • The row <div> has a m-t-2 spacer class added on to provide some vertical spacing
  • I'm using the same col-md-8 and col-md-4 column classes to set up the layout
  • I've also used the hidden-md-4 class on the sidebar <div> so that this section will not be visible on smaller resolution devices
  • Finally, I added some temporary filler text for testing purposes

Converting the mailing list section to a partial

As I mentioned earlier, I would like the mailing list section to appear on all pages of the blog. Since this is the case, it would make more sense to make this chunk of code a partial that can be included in each template. It saves us having to reinsert this snippet in every one of our page templates.

From your text editor, create a new file called _mailing-list.ejs and save it to the partial directory in the root of your blog project. Once the file is saved, insert the following code into it:

<div class="row m-t-3"> 
  <div class="col-md-12"> 
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
  </div> 
</div> 

Now go back to the blog post template file and insert the following line of code where the mailing list section should appear:

<%- partial("partial/_mailing-list") %> 

Remember to do the same thing for the index and contact template. Delete the hardcoded mailing list and replace it with the preceding partial line.

That concludes the setup of the blog post template. Let's test it out before we move onto the next chapter, to make sure the new mailing list partial is working properly. Return to the Terminal and compile your project from the root directory. Run the Harp server command, then visit the following URL: http://localhost:9000/blog-post.html.

If all went as planned, your blog post page should look like this:

Converting the mailing list section to a partial

Make sure you don't forget to test the index and contact page templates in your browser to make sure the mailing list partial is working properly. That concludes the design layout for the blog post template. All of our templates are now ready to go, but before we move onto the next chapter on content components, let's review what we've learned.