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 and customizing forms

At long last we are ready to move on to the final part of our landing page, the Contact Us form. Note that we will not be writing any of the actual JavaScript that transfers the contents of the Contact Us form to the server. Instead, we will learn how to use Bootstrap to lay out our form elements in an elegant and responsive manner.

Typically, Contact Us forms require at least three pieces of information from the user—the user's name (so that the recipient of the form data will know who they are talking to), the user's e-mail address (so that the recipient of the form data can reply to the user), and the actual message (what the user wants to send to the recipient). As such, we will be creating a form that will consist of three inputs—a name field, an e-mail address field, and a text area for the user to write their message.

Let's dig right into it. Start by creating an empty form below the Contact Us header:

    <div class="container-fluid myphoto-section bg-myphoto-light"
    id="contact">        
<div class="container">
            <div class="row">
                <h3>Contact Us</h3>
                    <form>                      
<!--Our form will go here-->
                    </form>
            </div>
        </div>
    </div> 

At the heart of Bootstrap's form layout is the form-group class. On its own, all this class does is set a 15 px margin. However, as we will see, combining the form group with form controls gives us a powerful way of controlling our form's appearance.

By default, Bootstrap lays out its form elements vertically. That is, each element is stacked above the other. Let's go ahead and add a name field to our form. Insert the following snippet in between our previously created form tags:

    <div class="form-group">
<label for="name">First and Lastname</label>
<input type="name" class="form-control" id="name"
        placeholder="Name">
    </div>

Save and hit refresh. As you can see, the label takes over the entire first row, with the name input field appearing on the second row (Figure 4.13). Note how we applied the form-control class to our name input field. This class is what styles our actual input element. Among other things, Bootstrap sets its height (to exactly 34 px), pads it, sets the input's font size (to 14 px), and gives the element a nice inset effect by adjusting the border style, border color, and border radius. Removing this class from our input will result in just a simple, plain input box being displayed.

Now, what if we wanted slightly smaller form elements? Well, we could apply our own styles, for example, we could change the font size or height of our input element. However, Bootstrap provides us with form-group-sm and form-group-lg . The former reduces the height of nested form controls to 30 px, the font size to 12 px, and the line height to 1.5 mm. The form-group-lg , on the other hand, makes its containing form controls larger by increasing their font size to 18 px and their height to 46 px. Go ahead and apply either one to our form group. For example:

    <div class="form-group-sm">
        <label for="name">Name</label>       
        <input type="name" class="form-control" id="name"
        placeholder="Name">
    </div> 

Take a look at the following screenshot:

Creating and customizing forms

Figure 4.13: Our name input.

Great! We have created our first form. But note how (Figure 4.13) our name field stretches across the entire page, taking up a lot of valuable real estate. Seeing how users will certainly not need all this space for their names, you might wish to align the label and name input field. Luckily, Bootstrap makes this quite easy for us by offering horizontal form layouts and inline forms through the form-inline class. Applying it to our form element will change the display style of form controls and form groups contained within the form to inline-block . This will align the various components within your form element. Go ahead and apply it (see Figure 4.14):

    <form class="form-inline">
        <div class="form-group-sm">     
            <label for="name">Name</label>            
            <input type="name" class="form-control" id="name" placeholder="Name">
</div> 
    </form>    

Take a look at the following screenshot:

Creating and customizing forms

Figure 4.14: Our vertical name form group.

This is much better. But we are not done quite yet. Try adding a second form group to the form. It will end up on the same line as our name form group. Furthermore, the input fields are now a bit too small to be of practical use. Try customizing them by combining the lessons that you learned in this chapter with the lessons you learned in Chapter 2, Making a Style Statement.

While we can save valuable space by aligning labels and input boxes, there is a second approach that we can take to combine input description with the actual input fields: Icons and placeholder texts (see Figure 4.15). The idea is that we combine icons and placeholder text to signify the expected input. The advantage behind this approach is that we will not be losing space to label elements.

First, let's remove the previously inserted label element and edit the input's placeholder attribute to contain the string Your name. Our name form group should now consist of a single input element:

    <div class="form-group"> 
        <input type="text" class="form-control" placeholder="Your name">
    </div> 

Let's go ahead and duplicate this form group so that we now also have an input for our user's e-mail address:

    <form>
        <div class="form-group"> 
            <input type="text" class="form-control" placeholder="Your 
            name">
        </div>
        <div class="form-group"> 
<input type="text" class="form-control" placeholder="Your email 
            address">
        </div>
    </form> 

Now let's go ahead and select two Font Awesome icons that best describe the expected input. We recommend:

  • A user symbol to indicate the user's name: fa fa-user
  • An @ symbol to indicate the user's e-mail address: fa fa-at

Take a look at the following screenshot:

Creating and customizing forms

Figure 4.15: The finished Contact Us form: labels and placeholder text are combined to describe the expected input for a given input field.

To combine the input element with the icon, we first apply Bootstrap's input-group class to the form control. That is, our form control's class attribute should now be set to class="form-group input-group" . Next, we create the <i> tag for our Font Awesome icon, insert it before our input element, and then wrap the icon with a span element. We set the span class attribute to class="input-group-addon" :

    <span class="input-group-addon"><i class="fa fa-user"></i></span>

Note

Under the hood

The input-group class modifies the display, position, and z-index properties of a form-control element. In combination with an input-group-addon , this allows you to insert elements to the left or right of input elements.

Apply this to both the name and e-mail inputs and save. You should now see that the icons are aligned to the left of our input elements. To align them to the right, simply move the icon span after the input element:

    <div class="form-group input-group">
        <input type="text" class="form-control" placeholder="Your email 
        address">
        <span class="input-group-addon"><i class="fa fa-at"></i></span>
    </div> 

Our form is almost done. All that is left is to add a text area for our message (using the textarea element) and a Send button. To achieve the former, go ahead and create a new form-group. However, instead of the form-group containing an input box, add a label and a textarea . Just as with the input elements, the textarea should be a form control (so go ahead and set its class attribute to form-control ):

    <div class="form-group">
        <label for="name">Your message</label>
        <textarea class="form-control" rows="5" id="message"></textarea>
    </div> 

The last missing part of the puzzle is our Send button. Although we won't be writing the event listeners for our Send button, we will now explore the various Bootstrap context styles available for buttons. The parent class of any button is the btn class. In essence, the btn class adjusts the padding, margin, text alignment, font size, and weight as well as the border radius of any element that it is applied to. The seven context classes offered by Bootstrap are btn-primary , btn-secondary , btn-success , btn-link , btn-info , btn-danger , and btn-warning (see Figure 4.16). Take a look at the following screenshot:

Creating and customizing forms

Figure 4.16: The five Bootstrap button context styles. From left to right: Default, success, danger, info, and warning.

Let's go ahead and complete our form by inserting a button element and apply the success context class. Create a new form group below our text area. Inside the form group, create a button element and set its class to btn btn-success . Insert a nice icon and add some text to the button:

    <div class="container-fluid myphoto-section bg-myphoto-light"
    id="contact">
        <div class="container">
            <div class="row">
                <h3>Contact Us</h3>
                <form>
                    <div class="form-group input-group">
                        <span class="input-group-addon"><i class="fa 
                        fa-user">
                        </i></span>
                        <input type="text" class="form-control" 
                        placeholder="Your name">
                    </div>
                    <div class="form-group input-group">
                        <span class="input-group-addon"><i class="fa 
                        fa-at">
                        </i></span>
                        <input type="text" class="form-control" 
                        placeholder="Your email address">
                    </div>
                    <div class="form-group">
                        <label for="name">Your message</label>
                        <textarea class="form-control" rows="5"
                        id="message">
                        </textarea>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-success">
                            <i class="fa fa-send-o"></i>
                            Send
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

Last but not least, we can add some descriptive text to our Contact Us section. In this case, we will use placeholder text generated using http://generator.lorem-ipsum.info/ (a useful tool if you ever require placeholder text for demo purposes):

    <h3>Contact Us</h3>
    <p>
        Lorem ipsum dolor sit amet, modo integre ad est, omittam temporibus
        ex sit, 
        dicam molestie eum ne. His ad nonumy mentitum offendit, ea tempor 
        timeam nec, 
        doming latine liberavisse his ne. An vix movet dolor. Ut pri
        qualisque reprehendunt, 
        altera insolens torquatos in per. Mei veri omnium omittam at, 
        ea qui discere ceteros.
    </p> 

Take a look at the following screenshot:

Creating and customizing forms

Figure 4.17: The Contact Us section along with the Send button.

While in the case of the preceding example the inputs are pretty explanatory, there may be inputs that require an explanation. As such, you can add descriptive text using the form-text class:

    <p class="form-text">Some text describing my input.</p>

All that the form-text class does is set the element's display to block, and add a top margin of 0.25 rem to the element.

Furthermore, it should be noted that we wrapped both the Contact Us and Send us a message text inside h3 and h4 header tags. This is appropriate in this case, as the titles blend nicely with the content. However, for cases in which the title should stand out more, or should not blend with the content, one should use Bootstrap's display heading class display-*,where * denotes a number between 1 and 4. As with the header tags, the smaller the number, the bigger the font. The largest display class applies a font size of 6 rem to the target element, and decreases this by 0.5 rem for each successive display class (so, for example, display-2 sets the font size to 5.5 rem). The display style definition also sets the font weight to 300. The Figure 4.18 contrasts the use of display-* against the use of the HTML header tags. Take a look at the following screenshot:

Creating and customizing forms

Figure 4.18: Contrasting Bootstrap's display classes against the HTML header tags.

Note

Increasing the font size and weight of paragraphs

In order to emphasize the text contained within paragraphs (as opposed to their headers), the lead class should be used. This class changes the font size to 1.25 rem and the font weight to 300.