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

Using and customizing alerts

Now that we know how to use icons, let's turn to a different topic, namely, alert boxes. Alert boxes are typically used to highlight an important event, or to emphasize an important message. As such, the purpose behind alerts is to provide a content area that immediately stands out, and therefore cannot be easily overseen by the user. For example, imagine that MyPhoto only supports browsers above certain versions. In such a case, a user who visits the site with an unsupported browser version should be notified that their browser is not supported. After all, the website may not function or display correctly when viewed with unsupported software. Bootstrap provides us with the alert class, which makes it very easy for us to implement this hypothetical scenario (the JavaScript for browser detection will be presented in Chapter 5, Speeding Up Development Using jQuery Plugins).

Bootstrap's alert comes with four contexts: Success, Warning, Info, and Danger. Each context class styles the element to which it is applied differently, depending on the intended message. See Figure 4.7, which lists the four default alert styles that come with Bootstrap:

Using and customizing alerts

Figure 4.7: Bootstrap's four contextual alert classes: .alert-success, .alert-warning, .alert-info, and .alert-danger.

Let's go ahead and apply one of these styles to our new unsupported browser alert box. Go ahead and create a new div and set its class attribute to alert alert - danger :

    <div class="alert alert-danger">
        <strong>Unsupported browser</strong> Internet Explorer 8 and
        lower are not supported by this website.
    </div>

Now insert this alert div inside our Welcome section, below the jumbotron:

    <div class="container-fluid myphoto-section bg-myphoto-welcome"
    id="welcome">
        <div class="container"> 
            <div class="jumbotron">
                <h1>Welcome to MyPhoto</h1>
                <p>Photographs you can cherish!</p>

            
</div> 

            
<div class="alert alert-danger"> 
                <strong class="alert-heading">Unsupported browser</strong>
                 Internet Explorer 8 and lower are not supported by this
                 website.
            
</div>
        </div>
    </div> 

Save and refresh. Voila! We have just created our very first Bootstrap alert (see Figure 4.8). Take a look at the following screenshot:

Using and customizing alerts

Figure 4.8: Our first dangerous alert dialog.

But something isn't quite right. What if the user knows that their browser is outdated, but still wishes to continue viewing the contents of our Welcome section without the invasive alert? Could we provide a way for the user to acknowledge the message and then allow them to continue browsing without its invasive presence? The answer is yes. Bootstrap provides us with a very easy way to make alerts dismissible using the data - dismiss attribute:

    <div class="alert alert-danger alert-dismissible">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">
        &times;</a>

            
<strong>Unsupported browser</strong> Internet Explorer 8
            and lower are not supported by this website.
    </div>

This will add an X to the right side of our alert dialog. Adding the alert-dismissible class will align the X and have it inherit its color. Click this X, and see the alert disappear.

This is great. Users can now dismiss our alert. However, what happens when the user jumps straight into a different section of MyPhoto? Currently, the alert is placed inside our Welcome section. As such, users viewing other sections of our website will not necessarily be able to see the alert dialog. The solution is to adjust the position of our alert dialog so that it appears stuck to our page, regardless of the section that the user is currently in. How do we do this? First, we will need to take the alert outside of our Welcome section, and move it just below our navbar. This will make the alert hidden behind our fixed navbar. To make the alert visible below our navbar, we can simply offset the position of the alert from the top of the page using the CSS margin - top property. To then make the alert sticky, that is, fixed below the navbar regardless of which section the user is currently in, we use the CSS position property and set it to fixed. Lastly, we can adjust the left offset and width of our alert so that it is nicely indented from the left hand side of our page, and stretches horizontally across the page (note that, for the sake of keeping the sample code short and concise, we are applying inline styles to achieve this. However, as we will discover in Chapter 8Optimizing Your Website we should normally avoid using inline styles whenever we can). Observe the following code:

    <nav class="navbar navbar-myphoto navbar-fixed-top" role="navigation">
        <!-- Navbar markup -->
    </nav>
    <div class="alert alert-danger alert-dismissible" style="position:
    fixed; margin-top: 4em; width: 90%;margin-left: 4em;">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">
    &times;</a>

    
<strong class="alert-heading">Unsupported browser</strong>
    Internet Explorer 8 and lower are not supported by this website.
    </div>

Take a look at the following screenshot:

Using and customizing alerts

Figure 4.9: Our sticky alert dialog now stretches across the entire page and remains visible across the sections.

Great! Try scrolling down the page and observe how our alert remains fixed below the navbar. Let's add an icon:

    <div class="alert alert-danger alert-dismissible" style="position:
    fixed; margin-top: 4em; width: 90%;margin-left: 4em;">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&
        times;</a>

        
<strong class="alert-heading"><i class="fa fa-exclamation"></i>

        
Unsupported browser</strong>Internet Explorer 8 and lower are not
        supported by this website.
    </div>

Our alert is already looking pretty decent. It is positioned nicely below our navbar, is dismissible, and sports a nice little icon. But it somehow doesn't look very dangerous, does it? How about we customize its colors a bit? For example, we could darken the background color slightly, and lighten the foreground color. But how would we go about doing this without modifying the Bootstrap source? Easy! Just apply the desired CSS properties using either an inline style, or, even better, apply it globally throughout our style sheet. Go ahead and open styles/myphoto.css . Insert the following CSS snippet, save, and then refresh the MyPhoto page:

    .alert-danger
    
{
        background-color: #a94342;

        
color: white;
    }

The snippet that you just added to your myphoto.css file should be pretty self-explanatory: It applies a white foreground and a dark red background (Figure 4.10) to any element that has the class alert-danger . Consequently, this foreground and background color will apply to any alert dialog that uses the alert-danger context class. Congratulations! You just learned how to customize your first Bootstrap component!

Let's finish by tidying any inline styles that we created (we will talk more about inline styles in Chapter 8Optimizing Your Website; for now just accept that you should avoid using inline styles whenever possible). Create a custom class, alert-myphoto , extract the inline styles into it, and add a z-index rule to ensure that our warning will appear above all other elements on the page:

    .alert-myphoto
    {
        background-color: #a94342;
        color: white;
        position: fixed;
        margin-top: 4em;
        width: 90%;
        margin-left: 4em;
        z-index: 3000;
    }

Take a look at the following screenshot:

Using and customizing alerts

Figure 4.10: Our alert now with a white foreground and a darker background.

Note

Under the hood

Bootstrap defines the background color for the individual context classes using the $state-<context>-bg variable, where context refers to one of the four Bootstrap contexts: Success, Danger, Info, or Warning. As such, you can globally override the default background color for a given alert by changing the value of $state-<context>-bg to your desired background color. For example, to make the background of the danger alert blue, you could write the following:

$state-danger-bg: blue

However, this is generally not recommended as it will change the background color for all danger alerts across your entire website.

Note that, when adding links to alerts, that you should apply the alert-link class to the link element. This will style the element to match the alert's context.