Table of Contents for
Responsive Web Design with HTML5 and CSS3 Essentials

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Responsive Web Design with HTML5 and CSS3 Essentials by Asoj Talesra Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Responsive Web Design with HTML5 and CSS3 Essentials
  4. Responsive Web Design with HTML5 and CSS3 Essentials
  5. Credits
  6. About the Authors
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Introducing Responsive Web Design
  16. Exploring how RWD works
  17. Understanding the elements of RWD
  18. Appreciating the importance of RWD
  19. Comparing RWD to adaptive design
  20. Preparing our development environment
  21. Considering a suitable strategy
  22. Exploring best practices
  23. Setting up a development workflow
  24. Exploring mistakes
  25. Summary
  26. 2. Creating Fluid Layouts
  27. Understanding the different layout types
  28. Setting the available viewport for use
  29. Exploring the benefits of flexible grid layouts
  30. Understanding the mechanics of grid layouts
  31. Implementing a prebuilt grid layout
  32. Exploring the use of flexbox
  33. Visiting the future
  34. Taking it further
  35. Summary
  36. 3. Adding Responsive Media
  37. Making video responsive
  38. Making text fit on screen
  39. Summary
  40. 4. Exploring Media Queries
  41. Understanding media queries
  42. Identifying common breakpoints
  43. Putting our theory into practice
  44. Creating some practical examples
  45. Examining some common mistakes
  46. Exploring best practices
  47. Taking things further
  48. Summary
  49. 5. Testing and Optimizing for Performance
  50. Understanding why pages load slowly
  51. Optimizing the performance
  52. Testing the performance of our site
  53. Best practices
  54. Providing support for older browsers
  55. Considering cross-browser compatibility
  56. Testing site compatibility
  57. Following best practices
  58. Summary

Putting our theory into practice

Throughout the course of this chapter, we've covered the different parts that make up media queries as I am sure someone once said, it is time.

Yes, it's time to put this into practice. Before we start creating some real-world examples, let's create something we can use to explore the effects of adding a media query to code. We'll start by resizing some simple boxes on screen:

  1. From the code download, go ahead and extract a copy of queryexample.html and save it to our project area.
  2. In a text editor, go ahead and add the following styles; we'll start with some basic styles for our boxes:
            body { background-color: #F3ECDD; text-align: center; 
              font-family: Arial, sans-serif; color: #ffffff; 
              min-width: 33%; } 
     
            .box { width: 100%; background: #905f20; border-radius: 0.625em; 
              margin: 0; } 
     
            .box2{ min-width: 100%; background: #6b8e6f; border-radius: 
              0.625rem; 
              float: left; } 
     
            h3:after{ content: ' is less than 30rem'; } 
    
  3. We then need to add our media query; go ahead and add this below the closing bracket of the previous style rule:
            @media screen and (min-width: 30rem) { 
              body { background-color: #C7B47C; } 
     
              .box { width: 49.5%; float: left; } 
     
              .box + .box { margin-left: 1%; margin-bottom: 0.625em;} 
     
              h3:after{ content: ' is greater than 30rem'; } 
            }  
    
  4. Save the file as queryexample.css within the css subfolder in our project area.

If we try previewing the results of our work by running queryexample.css, we should see something akin to this screenshot:

Putting our theory into practice

Let's understand what happened here. In the core CSS (in step 3), we added three <div> elements to our markup. Since we gave them 100% width and height is set to auto by default, they'll be stacked as a series of boxes.

If we use Chrome and activate the device mode as we did before, then we can begin to see the effect of resizing the browser window. If we resize it to below 30rem in width as our breakpoint (or 480px), we can see the boxes realign and resize at the same time; the background also changes to a light brown color:

Putting our theory into practice

Now that we've seen the basics of setting up queries, let's take this a step further and create some practical examples. We'll begin with setting up a simple web page to which we will apply some simple media queries.