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

Checkbox and radio buttons

A new feature in Bootstrap 4 is the ability to convert checkboxes and radio buttons into regular buttons. This is really handy from a mobile standpoint because it is much easier to touch a button than it is to check a box or tap a radio button. If you are building a mobile app or responsive website, it would be a good idea to use this component. Let's start by taking a look at the code to generate a group of three checkboxes as a button group:

<div class="btn-group" data-toggle="buttons"> 
  <label class="btn btn-primary active"> 
    <input type="checkbox" checked autocomplete="off"> checkbox 1 
  </label> 
  <label class="btn btn-primary"> 
    <input type="checkbox" autocomplete="off"> checkbox 2 
  </label> 
  <label class="btn btn-primary"> 
    <input type="checkbox" autocomplete="off"> checkbox 3 
  </label> 
</div> 

Let me break down the code and explain what is going on here:

  • To generate a button group with checkboxes, you need to wrap the boxes in a <div> with a class of .btn-group.
  • To allow the buttons to toggle on and off, you also need to add the data attribute data-toggle="buttons" to the <div>.
  • Next we need to use the button classes on the <label> tag to convert each checkbox into a button. Note that on the first button I'm using the .active class, which will make this checkbox toggled on by default. This class is totally optional.
  • Your basic checkbox <input> tag is nested within the label.

Keep in mind since these are checkboxes, you can toggle multiple options on or off. Here's what the button group should look like when rendered in the browser:

Checkbox and radio buttons

As you can see, this renders a nice-looking button group that is optimized for mobile and desktop. Also, notice how the first checkbox has a different background color as it is currently toggled on because of the .active class applied to that label. In the same way that we've created a button group with checkboxes, we can do the same thing with radio buttons.

Creating a radio button group

Creating a radio button group is very similar to the checkboxes. Let's start by checking out the code to generate this different variation:

<div class="btn-group" data-toggle="buttons"> 
  <label class="btn btn-primary active"> 
    <input type="radio" name="options" id="option1" autocomplete="off" checked> radio 1 
  </label> 
  <label class="btn btn-primary"> 
    <input type="radio" name="options" id="option2" autocomplete="off"> radio 2 
  </label> 
  <label class="btn btn-primary"> 
    <input type="radio" name="options" id="option3" autocomplete="off"> radio 3 
  </label> 
</div> 

Let me explain what's happening here with this code:

  • Like the checkboxes, you need to wrap your collection of radio buttons in a <div> with the same class and data attribute
  • The <label> tag and button classes also work the same way
  • The only difference is that we are swapping the checkbox <input> type for radio buttons

Keep in mind that with radio buttons, only one can be selected at a time. In this case, the first one is selected by default, but you could easily remove that. Here's what the buttons should look like in the browser:

Creating a radio button group

As you can see, the button group is rendered the same way as the checkboxes, but in this case we are using radios. This should be the expected result to optimize your group of radio buttons for mobile and desktop. Next we'll build on what we've learned about button groups, but learn how to use them in other ways.

Note

We'll circle back later in this chapter and actually add the components to our blog project.

Using button groups

If you're new to Bootstrap, button groups are exactly as they sound. They are a group of buttons that are connected horizontally or vertically to look like a single component. Let's take a look at the code to render the most basic version of the component:

<div class="btn-group" role="group" aria-label="Basic example"> 
  <button type="button" class="btn btn-secondary">Left</button> 
  <button type="button" class="btn btn-secondary">Middle</button> 
  <button type="button" class="btn btn-secondary">Right</button> 
</div> 

As you can see, we have a group of regular button tags surrounded by <div> with a class of .btn-group on it. At the very least, this is all you need to do to render a button group. There are a couple of other optional attributes on the <div> tag, which are role and aria-label. If you need to worry about accessibility, then you should include those attributes, otherwise they are optional. One other small change in this code is I've decided to use the .btn-secondary class to mix things up a bit with the button styles. Let's take a look at how this will appear in the browser:

Using button groups

As you can see, we have a single component that is made up of three buttons. This component is commonly used for a secondary navigation, or in a form like I explained in the previous section. If you'd like to display the buttons vertically, that is also possible with a small change.

Creating vertical button groups

If you'd like to arrange the buttons in your group vertically, that is actually quite easy to do. There is no need to change any of the code on the <button> tags, you just need to update the CSS class name on the wrapping <div> tag. Here's the code you need to change:

<div class="btn-group-vertical"> 
  ... 
</div> 

If you make that alteration to your code, then the same button group will appear like this in the browser:

Creating vertical button groups

It would probably have made sense to change the left button label to the top and the right button label to the bottom. However, I left them as they are because I wanted to show you how you can simply shift the alignment of the group by changing one CSS class. That covers the basics of using the button groups component; in the next section, I'll show you how to create button drop-down menus.

Coding a button dropdown

The code to render a button as a dropdown is a little bit more complicated but still fairly easy to get up and running. You'll combine a button tag with <div> that has a nested collection of links inside it. Let's take a look at the code required to render a basic drop-down button:

<div class="btn-group"> 
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
    Dropdown 
  </button> 
  <div class="dropdown-menu"> 
    <a class="dropdown-item" href="#">Link</a> 
    <a class="dropdown-item" href="#">Link Two</a> 
    <a class="dropdown-item" href="#">Link Three</a> 
    <div class="dropdown-divider"></div> 
    <a class="dropdown-item" href="#">Link Four</a> 
  </div> 
</div> 

Okay, there are a few things going on here. Let's break them down one by one and explain how the dropdown works:

  • The entire component needs to be wrapped in a <div> with a class of .btn-group on it.
  • Next you insert a <button> tag with some button CSS classes on it. Like in the previous section, some of the other attributes are optional. However, it is a good idea to include this attribute: aria-expanded. This can either be set to false or true and controls whether the dropdown is open or closed on page load. In most cases, you will want to set this to false.
  • After the <button> tag, insert another <div> tag which will hold all the links that appear in the drop-down menu list. Make sure you give this <div> a class of .dropdown-menu.
  • Within the second <div> you insert a collection of <a> tags, one for each item in your list. Each <a> tag requires a class of .dropdown-item so that the proper CSS styling is applied.
  • You may also want to insert a divider in your drop-down list if you have a large amount of links. This is done by inserting a third <div> with a class of .dropdown-divider on it.

As I mentioned, this component is a little more complex, but in Bootstrap 4 they have actually simplified it a bit to make it easier to use. Let's take a look at what it should look like in the browser. In the following screenshot, I've showed what the expanded version of the dropdown will look like so you can see the button and the list of links:

Coding a button dropdown

As you can see, we have a drop-down button with a list of links nested within it. Keep in mind that if you want to use this component, it does require that you include jQuery and bootstrap.min.js in your template. There are some other variations of this component you can easily implement, such as the pop-up menu.

Creating a pop-up menu

In some cases, you might want to have your menu pop up above the button instead of below it. You can achieve this by adding one class on the wrapping <div> for the component. Check out the code here:

<div class="btn-group dropup"> 
  .. 
</div> 

As you can see, I've added the class .dropup to the <div>. This will make the menu appear above the button, and it should look like this:

Creating a pop-up menu

As you can see, the list appears above the button when it is expanded.

Creating different size drop-down buttons

By adding a single class to the <button> tag in the dropdown, you can make the trigger larger or smaller. Let's take a look at the code for the smaller and larger button variations:

<!-- large button //--> 
<div class="btn-group"> 
  <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
    Large button 
  </button> 
  <div class="dropdown-menu"> 
    ... 
  </div> 
</div> 
 
<!-- small button //--> 
<div class="btn-group"> 
  <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
    Small button 
  </button> 
  <div class="dropdown-menu"> 
    ... 
  </div> 
</div> 

If you find the button tag in the first example, you'll see I've added a class of .btn-lg to it. This class will increase the button size to be larger than the default. Take a look at the second chunk of code, find the <button> tag again, and you'll see a class of .btn-sm on it. This works the same way except the button will now be smaller than the default. Let's see how these buttons will render in the browser.

Note

The .btn-lg and .btn-sm classes are not exclusive to the button drop-down component. You can use them on any button component variation you like.

Creating different size drop-down buttons

That concludes the basics of using the button drop-down component. In the next section, we'll cover a more complicated component, which is forms.