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

Coding forms in Bootstrap 4

If you are familiar with Bootstrap 3, then you'll notice the CSS form classes are pretty much the same in version 4. The biggest change I see in forms for the new version is that each form group uses a <fieldset> tag instead of <div>. If you are new to Bootstrap forms, a basic form group is made up of a label and an input. It can also include help text, but that is optional. Let's jump right in by creating a generic form that uses a number of core components.

Setting up a form

At the very least, a form needs to be made up of one input and one button. Let's start with the basics and create a form following those requirements. Here's the code to get you started:

<form> 
  <fieldset class="form-group"> 
    <label>Text Label</label> 
    <input type="text" class="form-control" placeholder="Enter Text"> 
    <small class="text-muted">This is some help text.</small> 
  </fieldset> 
  <button type="submit" class="btn btn-primary">Submit</button> 
 </form> 

Let me explain what is happening here in the code:

  • Every form needs to start with a <form> tag. However, no special classes are required on this tag.
  • I've inserted a <fieldset> tag with a class of .form-group on it for our single input. This <fieldset> pattern will be repeated in the future when you add additional inputs.
  • Within the <fieldset>, we have a <label>. Again, no special CSS classes need to be added to the <label>.
  • After the label, you need to insert the form <input> tag. In this case, I'm using a text input. On this HTML tag, you need to add a class of .form-control. All input tags in Bootstrap will require this class. The placeholder text is optional but nice to add for usability.
  • In the last line of the <fieldset>, I've included a <small> tag with a class of .text-muted, which will render the text small and light grey. This is the pattern you should use if you want to include some help text with your form input.
  • Close the <fieldset> tag and then you need to add a <button> tag for the form submit button.
  • Close the <form> and you are done.

After you've finished reviewing the code, fire up your web browser, and your form should look like this:

Setting up a form

You've successfully coded your first Bootstrap 4 form. Let's continue and I'll explain how to implement other common form components using the latest version of Bootstrap.

Adding a select dropdown

Let's build on our form code by adding a select drop-down menu. Insert the following code after our text input:

<fieldset class="form-group"> 
  <label>Select dropdown</label> 
  <select class="form-control"> 
    <option>one</option> 
    <option>two</option> 
    <option>three</option> 
    <option>four</option> 
    <option>five</option> 
  </select> 
</fieldset> 

Let's break down the parts of the code you need to be aware of:

  • Note that the entire <select> is wrapped in a <fieldset> with a class of .form-group. This pattern should repeat for each type of form input you add.
  • On the <select> tag, there is a class of .form-control that needs to be added.
  • Aside from that, you should code the <select> as you normally would, following the best HTML syntax practices.

Once you're done, if you view the form in the browser, it should now look like this:

Adding a select dropdown

That completes the setup for <select> dropdowns. Next let's check out the <textarea> tag.

Inserting a textarea tag into your form

Moving along to the next input type, let's insert a <textarea> tag into our form. After the <select> menu, add the following code:

<fieldset class="form-group"> 
  <label>Textarea</label> 
  <textarea class="form-control" rows="3"></textarea> 
</fieldset> 

Using this input is fairly simple. Like our other examples, you need to use a <fieldset> tag with a CSS class of .form-group to wrap the entire thing. On the actual <textarea> tag, you need to add the .form-control class. That's it; once you're done, your form should now look like this:

Inserting a textarea tag into your form

Now that the <textarea> is complete, let's move on to the file input form field.

Adding a file input form field

Historically, the file input form field has been a tricky one to style with CSS. I'm happy to say that in Bootstrap 4 they've created a new approach that's the best I've seen to date. Let's start by inserting the following code after the <textarea> in our form:

<fieldset class="form-group"> 
  <label>File input</label> 
  <input type="file" class="form-control-file"> 
  <small class="text-muted">This is some help text. Supported file types are: .png</small> 
</fieldset> 

Again, this form field is constructed in the same manner as the previous ones. However, there is one small change you need to be aware of with the File input field. On the <input> tag, you need to change the CSS class to .form-control-file. There are some specific styles being applied to clean up the look and feel of this form field. Once you're done, your form should look like this:

Adding a file input form field

That completes the File input field which leaves us with two more basic form field inputs to go over. They are radio buttons and checkboxes. Let's learn how to add them next.

Inserting radio buttons and checkboxes to a form

These fields are pretty similar so I'm going to group them together in their own section. The code for these two fields differs a little bit from the other inputs, as I'll outline now. First, let's insert the following code after the File input field in our form:

<div class="radio"> 
  <label> 
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked> 
    Option 1 
  </label> 
</div> 
<div class="radio"> 
  <label> 
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2"> 
       Option 2 
  </label> 
</div> 
 
<div class="checkbox"> 
  <label> 
    <input type="checkbox"> Checkbox 
  </label> 
</div> 

Let's start by going over the radio button code first, then we'll move on to the checkbox:

  • The fields don't use the <fieldset> tag as the wrapper. In this case, you should use a <div> and give it a class of either .radio or .checkbox, depending on what type you want to use.
  • For these fields, the <label> tag will also wrap around the <input> tag so that everything is displayed in a horizontal line. We don't want the text label to drop down below the radio button or checkbox.
  • You don't need a special class on the <input> for either of these fields.

As you can see, the code for these fields is a bit different from what we've learned about the other form inputs. Not to worry, as they are pretty easy to use and there aren't a bunch of CSS classes you have to memorize. One of the nicest changes with forms in Bootstrap 4 is that they require less HTML markup, so are easier to write. Finally, if you view our form in the browser, it should look like this:

Inserting radio buttons and checkboxes to a form

That completes the explanation of all the core form fields that you need to know how to use in Bootstrap 4. Before we move on to some more advanced form fields and variations, why don't we add a form to our blog project?

Adding a form to the blog contact page

I know, I know. I said we would wait till the end of the chapter to build components into the blog project. However, I'm thinking you might like a break from learning and actually add some of what you've learned to your project. Let's go ahead and do just that by filling in a form on the Contact page.

Updating your project

Let's start by opening up our project directory and finding the file named contact.ejs. Open up that file in your text editor and we are going to add some new form code and remove some filler code. To start, find the body section of the page that is wrapped in the following column <div>:

<div class="col-md-12"> 

Within that <div> is currently some filler text. Remove that text and replace it with the following form code:

<form> 
  <fieldset class="form-group"> 
    <label>Email</label> 
    <input type="email" class="form-control" placeholder="Enter email"> 
    <small class="text-muted">We'll never share your email with anyone else.</small> 
  </fieldset> 
  <fieldset class="form-group"> 
    <label>Name</label> 
    <input type="text" class="form-control" placeholder="Name"> 
  </fieldset> 
  <fieldset class="form-group"> 
    <label>Message</label> 
    <textarea class="form-control" rows="3"></textarea> 
  </fieldset> 
  <button type="submit" class="btn btn-primary">Submit</button> 
</form> 

I've coded up a basic contact form that you'll commonly see on a blog. It has e-mail, name, and message fields along with a submit button. Save your file and then preview your project in a browser. The Contact page should now look like this:

Updating your project

That concludes the updates to the Contact page for now. Later on in the book, we'll add some additional components to this page. Let's jump back into learning about forms in Bootstrap 4 by reviewing some additional form controls.

Additional form fields

Now that we've learned how to build a basic form and added one to our project, let's circle back and talk about some more advanced form fields and variations you can apply with Bootstrap 4. I'm going to start by showing you how to lay out forms in a few different ways.