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 Sass in the blog project

As I mentioned in the previous section, Sass is part of the development process and the browser cannot read it in its native format. Before you can deploy the project, you need to convert or compile the Sass files into regular CSS files. Normally this would require you to install a Ruby gem and you would have to manually compile your code before you can test it. Luckily for us, Harp.js actually has an Sass compiler built into it. So when you run the harp compile command to build your templates, it will also build your Sass files into regular CSS. I bet you're starting to like Harp even more after learning that.

Updating the blog project

Before we go any further, we need to make a few updates to our blog project to set it up for Sass. Head to your project directory and navigate to the CSS directory. In this directory, create a new file called custom.scss.

Note

The file extension used for Sass files is .scss.

What we're doing here is creating a custom style sheet that we are going to use to overwrite some of the default Bootstrap look-and-feel CSS. To do this, we need to load this custom file after the Bootstrap framework CSS file in our layout file. Open up _layout.ejs in the root of the project directory and insert the following line of code after bootstrap.min.css. Both lines together should look like this:

<link rel="stylesheet" href="css/bootstrap.min.css"> 
<link rel="stylesheet" type="text/css" href="css/custom.css"> 

Note here that I'm using the .css file extension for custom.css. This is because, after the files are compiled, the template will be looking for the actual CSS file, not the Sass file. The critical part is just that the actual filenames match and that you use .css in the layout file. Before we go any further, let's test out our Sass file to make sure it is set up properly. Open up custom.scss in your text editor and add the following code:

body { 
   background: red; 
} 

This is just a simple way to make sure that Sass is compiling to CSS and is being inserted into our layout. Compile your project and launch the server. If you've done everything correctly the background for your homepage should be red and look like this:

Updating the blog project

Hopefully this is what you're seeing and you can confirm you've set up your file correctly. Once you've successfully done this, delete the CSS we entered in the Sass file.

Note

It's perfectly acceptable to write regular CSS in Sass files. Ideally, you want to combine regular CSS code with Sass syntax to take full advantage of the preprocessor.

Now that you've finished setting up your files, let's start to learn a little bit more about using Sass in your project.

Using variables

In Sass, variables are called by using the $ sign character. If you're familiar with Less, the @ symbol is used for variables. So in that case, all you would need to do is use $ instead of @. To write a variable, start with the $ sign and then insert a descriptive keyword that can be anything you like. Here are a few examples of generic variable names:

$background-color 
$text-size 
$font-face 
$margin 

I've named these pretty generically and they actually match some CSS property names. This is a good idea and they are easy to reuse and make sense of if multiple developers are working on the same project. However, like I said, you can name your variables whatever you want. If you'd like to get more creative, you could name variables like this:

$matts-best-color 
$awesome-background-color 
$fantastic-font-face 

These are extreme examples and it is advisable not to name your variables in this way. To you $awesome-background-color might mean red but to another person it could mean anything. It's always a good idea to name your variables in a descriptive manner that makes sense.

I've shown you how to write the variable name but the other side of the equation is the actual value for the variable. Let's add in some sample values for our first set of variable names:

$background-color: #fff; 
$text-size: 16px; 
$font-face: helvetica, sans-serif; 
$margin: 1em; 

You write Sass variables the same way that you would write CSS properties. It's also worth noting that you should enter your variables at the very top of your style sheet so that they can be used in all of the CSS you write after them.

Using the variables in CSS

Now that we've written some variables, let's actually insert them into some CSS. After the variables in custom.scss, enter the following code:

body { 
   background: $background-color; 
   font-size: $text-size; 
   font-family: $font-face; 
   margin: $margin; 
} 

So instead of using actual values for our CSS properties, we're using the variable names that we set up. This starts to get more powerful as we add more CSS. Let's reuse some of these variables:

body { 
   background: $background-color; 
   font-size: $text-size; 
   font-family: $font-face; 
   margin: $margin; 
} 
 
h1 { 
   font-size: 36px; 
   font-family: georgia, serif; 
} 
 
h2 { 
   font-size: $text-size; 
   font-family: $font-face; 
} 

In this example, you can see a few things going on that I should explain:

  • For the <h1> tag, I'm not using any variables. I'm using regular CSS property values.
  • For the <h2> tag, I'm reusing the same variables to insert the font-size and font-family values.

As your style sheet grows longer, I'm sure you'll see the value in this strategy. For example, if I decide I want to change my font-size to 24px, all I need to do is change the value for the $text-size variable to 24px. I don't have to go through my entire style sheet and change all the values individually. These are just the basics of what you can do with variables. Let's look at a more advanced use case.

Using other variables as variable values

That might sound like a bit of a mouthful, but you can actually use a variable as the default value for another variable. A good example of where you might want to do this is when you are defining a color palette. You can switch the hex values to readable names and then use them for your other variables. This is much easier to scan and understand when you are debugging your code. Here's an example of what I mean:

$black: #000; 
$white: #fff; 
$red: #c00; 
 
$background-color: $white; 
$text-color: $black; 
$link-color: $red; 

Let me break down what is happening here for you:

  • First I've created three color variables for black, white, and red
  • Next I've created three CSS property variables for background-color, text-color, and link-color; the values for these CSS property variables are the color variables

Instead of using hex number values for the CSS property variables, I used a color keyword variable which is much easier to read and understand. That concludes the introduction to variables in Sass. Next we'll learn about importing different files into custom.css and using partials.