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

Creating a collection of variables

One of the main things you'll want to do when using Sass in Bootstrap is to create a library of global variables that can be used throughout your theme. Think of things such as colors, backgrounds, typography, links, borders, margins, and padding. It's best to only define these common properties once and then you can reuse them through different components. Before we go too far, we need to create a new .scss file. Open up your text editor, create a new file, and call it _variables.scss. Save that file to the /css/components directory. For now, you can just leave it blank.

Importing the variables to your custom style sheet

Now that we've created the variables Sass file, we need to import it into our custom style sheet. Open up custom.css in your text editor and paste the following line of code at the top of the file:

@import "components/_variables.scss"; 

It's important to note that this file must be at the top of your custom style sheet file. The variables will cascade through all the code that follows them so they must load first. Let's start filling out our variables file with a color palette.

Adding a color palette

Save the custom style sheet and then go back to the variables file. Let's start by inserting a color palette into the variables file like this:

$red: #e74c3c; 
$red2: #c0392b; 
$blue: #3498db; 
$blue2: #2980b9; 
$green: #2ecc71; 
$green2: #27ae60; 
$yellow: #f1c40f; 
$yellow2: #f39c12; 
$purple: #9b59b6; 
$purple2: #8e44ad; 
$white: #fff; 
$off-white: #f5f5f5; 
$grey: #ccc; 
$dark-grey: #333; 
$black: #000; 

As you can see, I've set up a palette of several colors that I'll use through my components and later my theme. Here are a few key points to keep in mind:

  • It's good to have two variations for your key colors. This comes in handy for a component such as a button where $red would be the static color and $red2 would be the hover or active color for the button.
  • I'm guessing you can already see how using variable names such as $purple is much more readable than hex values in a long style sheet.

Adding some background colors

The next thing you should add to your collection of variables is background colors. As we move through this variables file, we're going to create a variable for all properties that get used over and over again in our style sheet.

Add the following background color variables to the file:

$primary-background: $white; 
$secondary-background: $off-white; 
$inverse-background: $black; 

Let me explain, as best practice, how I have set this up:

  • First of all, I'm using the color variables we just set up as the values for our new background color variables. This keeps things simple and it also allows you to change the color and have it cascade through all your other variables. This is a great time-saving tip.
  • At the very least, it's a good idea to define a primary, secondary, and inverse background color variable. Note how I'm reusing the same language here that Bootstrap uses. This is a good practice to follow. Feel free to define additional background colors if you think you'll need them in your project.

Setting up the background color variables is pretty simple. Next let's set up our base typography variables.

Setting up variables for typography

The next section of variables we are going to set up is for the base typography styles. Insert the following code after the background colors:

$body-copy: helvetica, arial, verdana, sans-serif; 
$heading-copy: helvetica, arial, verdana, sans-serif; 
$base-font-size: 16px; 
$font-size: 1em; 
$base-line-height: 1.75; 

Let me explain why I'm setting the following variables for the typography:

  • For consistency, it's good to have a body and heading typeface. In this case, I'm using the same font stack for both but you could easily change the heading variable to something else. As you are coding your CSS, it's really easy to think of the font-family in either the body or heading version, compared with trying to remember the entire font stack for each, which also involves much more typing.
  • For the $base-font-size variable, we are going to use a pixel value. This is one of the only places you'll see pixels and it's set to the base em size that everything else will work off. Remember that ems are a relative sizing unit, so if you ever want to make all your components a little bigger or smaller, you can just tweak this one pixel value.
  • We also need a $font-size variable, which will be set to 1em. This is a base unit and it can easily be changed in other selectors by using Sass operators. The reason we set it to 1em is because it simply makes the math easy to do.
  • Finally, I set the $base-line-height to 1.75 because I like a little extra line spacing in my copy. You could choose to leave this out if you are fine with the Bootstrap default, which is closer to 1.5.

Now that we've set up our typography variables, let's move on to coding our text colors.

Coding the text color variables

As with the background colors, we need to set up some common color styles for text, as well as defining some colors for base HTML tags such as <pre> and <code>. Insert the following markup after the typography variables in the file:

$primary-text: $black; 
$light-text: $grey; 
$loud-text: $black; 
$inverse-text: $white; 
$code-text: $red; 
$pre-text: $blue; 

Let me break down how each variable is set up:

  • As in the background color variables, we are using a variable name for the value of our text color variables. I've included a variable called $primary-text and set it to black, following the same naming convention that was previously established.
  • I've added $light-text and $loud-text variables so we can easily apply lighter or darker text throughout our components.
  • I've also included an $inverse-text variable to be used with the corresponding background color.
  • Finally, I've set up default colors for the <pre> and <code> tags, which we will use to overwrite the default colors so they match our theme and color palette.

That finishes off the color variables that I recommend setting up. Feel free to add more if you have other uses you want to cover. Next we'll continue with some text colors by adding links.

Coding variables for links

An extension of basic text colors will be colors for links in our project. Go ahead and add the following code after the text colors in the file:

$primary-link-color: $purple; 
$primary-link-color-hover: $purple2; 
$primary-link-color-active: $purple2; 

In this case, I've decided to only define a primary link color to keep things simple. In your own projects, you will likely want to come up with a couple more variations.

  • For the static link color, I'm using the $purple color variable.
  • For the hover and active states of the primary link, I'm using $purple2. As I previously mentioned, this is an example of why it's a good idea to have two variations of each color in your palette.

Like I said, I've kept the link variables simple. It's nice to try and keep your set of variables as compact as possible. If you have too many then it starts to defeat the purpose of using them as it will be harder to remember them in your code. Next let's cover the variables we should set up for borders.

Setting up border variables

Another CSS property that gets used often is borders. That makes it a great candidate for Sass variables. Insert the following code after the link colors in the file:

$border-color: $grey; 
$border-size: 1px; 
$border-type: solid; 
$border-focus: $purple; 

Let me explain why I've set up the border variables in this manner:

  • When you are deciding on a value for $border-color, you should pick a color that you think will get used the most often in your components. Something like $grey is always a safe bet in most designs.
  • As with the color value, you should set the $border-size to the most common border size you anticipate using. It's also a good idea to set this to 1px because you can easily do the math to apply a Sass operator if you want a thinner or thicker border.
  • Again for the $border-type, set it to the value you will use the most, which is probably going to be solid.
  • Finally, set up a common $border-focus color. This is primarily used in form inputs once they are active. It's a good idea to pick a contrasting color for this variable so it really stands out when the input is in focus.

That concludes all the border variables I would recommend including. Next let's include some basic layout variables.

Adding variables for margin and padding

For consistent spacing throughout your designs, it's a good idea to use variables for margin and padding so that you can standardize on size. These properties are also used often so it's smart to make them variables that can be reused. Add the following code after the border markup:

$margin: 1em; 
$padding: 1em; 

All I'm doing here is setting a base size (for both padding and margin) 1em. Again, it's a good idea to set both of these to 1em because it is easy to do the math if you want to use Sass operators to increase or decrease the values of specific components. Those are the last variables that I would recommend adding to your variables file. However, we should add at least one mixin to the file before we are finished.

Adding mixins to the variables file

Since mixins will also be used through a number of your components, you should define them in this variables file. Then they will be available to all the CSS code that follows them in the custom theme file. At the very least, I would recommend setting up a mixin for border-radius, which I will show you how to do next. You may also want to include additional mixins for other CSS3 features.

Coding a border-radius mixin

We talked a little bit about mixins earlier but let's review them again now that we are actually applying them to our project. Insert the following code after the layout variables in your file:

@mixin border-radius($radius) { 
  -webkit-border-radius: $radius; 
     -moz-border-radius: $radius; 
      -ms-border-radius: $radius; 
          border-radius: $radius; 
} 

In Less, it is possible to set a global value for all your border-radius in a mixin. However, with Sass you have to set up the above formula but then on the actual selectors that follow you have to set the actual border-radius value. An example of that would look like this:

.my-component { 
  @include border-radius(5px); 
 } 

In this example, I've added the border-radius mixin to a CSS class called .my-component. The component will have a border-radius of 5px applied to it. You will need to repeat this step on any CSS class or component where you want to apply the border-radius mixin. That concludes our variables Sass file. We went over a bunch of code there, so let's see what it all looks like together. I've also included some CSS comments in the following code to help remind you what each section does:

/* variables */ 
 
/* color palette */ 
$red: #e74c3c; 
$red2: #c0392b; 
$blue: #3498db; 
$blue2: #2980b9; 
$green: #2ecc71; 
$green2: #27ae60; 
$yellow: #f1c40f; 
$yellow2: #f39c12; 
$purple: #9b59b6; 
$purple2: #8e44ad; 
$white: #fff; 
$off-white: #f5f5f5; 
$grey: #ccc; 
$dark-grey: #333; 
$black: #000; 
 
/* background colors */ 
$primary-background: $white; 
$secondary-background: $off-white; 
$inverse-background: $black; 
 
/* typography */ 
$body-copy: helvetica, arial, verdana, sans-serif; 
$heading-copy: helvetica, arial, verdana, sans-serif; 
$base-font-size: 16px; 
$font-size: 1em; 
$base-line-height: 1.75; 
 
/* text colors */ 
$primary-text: $black; 
$light-text: $grey; 
$loud-text: $black; 
$inverse-text: $white; 
$code-text: $red; 
$pre-text: $blue; 
 
/* links */ 
$primary-link-color: $purple; 
$primary-link-color-hover: $purple2; 
$primary-link-color-active: $purple2; 
 
/* border */ 
$border-color: $grey; 
$border-size: 1px; 
$border-type: solid; 
$border-focus: $purple; 
 
/* layout */ 
$margin: 1em; 
$padding: 1em; 
 
/* border-radius mixin */ 
@mixin border-radius($radius) { 
  -webkit-border-radius: $radius; 
     -moz-border-radius: $radius; 
      -ms-border-radius: $radius; 
          border-radius: $radius; 
} 

Now that we have all our variables and mixins set up, let's go ahead and start to learn how to apply them. We'll continue to build on the button example we started earlier by extending it into a custom look and feel.