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

Writing a theme

Creating your own Bootstrap theme is a bit of an undertaking. The good news is that once you've done it you can reuse a ton of the code for future themes. That's where the real power in making your code modular comes into play. Instead of starting over from scratch each time, you can reuse old code and just extend it. In the last section, we learned how to customize the button component that was the start of our own theme. Let's first start by looking at some common Bootstrap components that you'll want to customize for your own themes.

Common components that need to be customized

There are many ways that you can theme Bootstrap. In some cases, you may only need to customize a few components to get a unique look and feel going. However, you may want to do a more thorough theming process so that your theme doesn't resemble the default Bootstrap look at all. In this section, let's start by listing some of the common components you will most likely want to customize.

Next we'll go through the process of writing the code to customize a few so you get an idea as to how it works. Here's a list of components that I would recommend customizing:

  • Buttons
  • Drop-downs
  • Alerts
  • Navbar
  • Typography
  • Tables

This list is just a starting place. If you want to create a unique theme, you should really try to customize all Bootstrap components. At the very least, you should change them to use your custom color palette, typography, and layout styles. We've already covered buttons so let's jump into customizing the drop-down component, which is an extension of the button.

Theming the drop-down component

The drop-down component requires a medium-sized amount of customization so it's a good starting place to get an idea of what is involved in this process. It also builds on the code we wrote for the button so it's a natural second step. It's important to note that some components will require a good amount of CSS to customize them, while others will only need a little bit. Let's start by creating a new Sass file for drop-downs. From your project folder, create a new file called _dropdown.scss in the css/components directory. You can leave the file blank for now, just save it.

Once you've created the new Sass file for the drop-down component, we need to import it into our main theme is called custom.scss. Open up the custom style sheet in your text editor and insert the following line of code after the @import for the button component:

@import "components/_dropdown.scss"; 

Now we are ready to start coding our custom drop-down styles. Open up _dropdown.scss in your text editor and let's insert this first section of CSS:

.dropdown-menu { 
   color: $primary-text; 
} 

As with the buttons in the previous section, I'm only going to change the most basic properties to demonstrate how you can customize the component. Feel free to customize additional properties to get a more unique look and feel.

Let's break down what is happening here. The drop-down component is made up of the base .dropdown-menu CSS class. This controls how the menu will look. Here I've simply changed the text color to use for the $primary-text variable.

We also need to do some work on the list of links that appear in our drop-down menu. Insert the following CSS after the first section you just entered:

.dropdown-item:focus, 
.dropdown-item:hover { 
   color: $primary-text; 
   background-color: $secondary-background; 
} 

Let me break down what is happening here:

  • These CSS classes control the hover and focus states for each list item in our drop-down menu. Again, I've set it to use our $primary-text font color.
  • When you hover on a list item, the background color changes. I've changed that background color to use our $secondary-background color variable. In this case you should use the background color variable, not a customized color variable. The reason for this is it's easier to keep track of what background colors you are using as you progress through the writing of your code.

The last thing we need to do is update the actual drop-down button trigger with some additional code. Enter the last part of CSS into the file:

.open > .btn-primary.dropdown-toggle:focus { 
   background-color: $purple2; 
   border-color: $purple2; 
} 

When the drop-down button trigger is clicked the .open CSS class will dynamically be inserted into the HTML code. This initiates a unique variation on the button class, a drop-down toggle focus. That may sound complicated but what you need to know is that you need to set this selector to our $purple2 color so it matches the rest of the button.

I've overwritten the background-color and border-color properties to use $purple2 from our color palette.

That's it, the drop-down component has now been themed to match our look and feel. If you preview it in the browser it should look like this when the menu is open:

Theming the drop-down component

Now that we've finished with the drop-down component let's move on to learning how to theme the alerts component.

Customizing the alerts component

The alerts component in Bootstrap is fairly easy to theme. As with the button component, it comes in a few variations. Let's start by coding up the CSS for the default color method. Create a new file called _alerts.scss and save it to the css/components directory. Don't forget to import it into custom.scss with the following line of code:

@import "components/_alerts.scss"; 

Once you've set up the file, let's get started with the code for the success alert component:

.alert-success { 
   color: $green; 
   background-color: lighten( $green, 30% ); 
   border-color: lighten( $green, 30% ); 
} 

What you're now seeing should start to look familiar. However, I have introduced something new that I need to explain:

  • This is the success alert so it should be green in color. The first thing I've done is change the text color to use the green from our palette with the $green variable.
  • For the background-color and border-color properties, I'm using something new, a Sass function. In this case, I want a green color that is slightly lighter than my text. Instead of introducing another green color variable, I can use a Sass function to lighten the base $green variable color.
  • To create the function, you use the lighten keyword. Inside the brackets you need to include the variable name you want to target, in this case $green, and finally include a percentage value for how much to lighten it by. This is a nice little trick to save you having to create more variables.

Once you code this up it should look like this in the browser:

Customizing the alerts component

As you can see, we are using the green color values from our color palette. Let's continue and customize the colors for the rest of the alert bar variations. Enter the following code into the _alerts.scss file:

.alert-info { 
   color: $blue; 
   background-color: lighten( $blue, 30% ); 
   border-color: lighten( $blue, 30% ); 
} 
 
.alert-warning { 
   color: $yellow; 
   background-color: lighten( $yellow, 30% ); 
   border-color: lighten( $yellow, 30% ); 
} 
 
.alert-danger { 
   color: $red; 
   background-color: lighten( $red, 30% ); 
   border-color: lighten( $red, 30% ); 
} 

The other alerts follow the same pattern as the success version. They should look like this in the browser when you are done:

Customizing the alerts component

As you can see, the alerts are now using our color palette. Let's move on to the last component that I will show you how to customize, which is typography.

Customizing the typography component

The typography component isn't difficult to customize. We'll build off the base variables we set up to apply them to the appropriate HTML tags. As we did with our other components, start with creating a new file called _typography.scss and save it to the css/components directory. Once you do this, import the file into custom.scss with the following line of code:

@import "components/_typography.scss"; 

Let's start customizing the type by applying some styles to the base header tags:

h1, h2, h3, h4, h5, h6 { 
   font-family: $heading-copy; 
   color: $primary-text; 
} 

Here I've simply used the $heading-copy variable and applied it to all the HTML heading tags. This will allow our custom heading typeface to be used for all headers. I've also added the $primary-text variable so that our headers are using the correct text color. Next let's take a look at a few miscellaneous text styles that you will likely want to overwrite:

small { 
   color: $light-text; 
} 
 
pre { 
   color: $pre-text; 
} 
 
code { 
   color: $code-text; 
} 

As we did with our base variables, I'm now applying some of them on actual selectors. Let's break it down:

  • For the <small> HTML tag, I want it to look more subtle so I've set the text color to use the $light-text variable.
  • I purposely set up color text variables for the HTML <pre> and <code> tags. I've now applied the $pre-text and $code-text variables to these tags.

That covers some of the basic typography styles you're going to want to customize. There are more you could add but I will let you explore these on your own. That also goes for all the Bootstrap components. We have only scratched the surface of the level of customizing you can do for your Bootstrap theme. However, I think I've given you a good introduction to what you need to do for coding your own Bootstrap themes.