Table of Contents for
Mastering PostCSS for Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering PostCSS for Web Design by Alex Libby Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Mastering PostCSS for Web Design
  4. Mastering PostCSS for Web Design
  5. Credits
  6. About the Author
  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 PostCSS
  16. Introducing PostCSS
  17. Setting up a development environment
  18. Creating a simple example using PostCSS
  19. Linting code using plugins
  20. Exploring how PostCSS works
  21. Summary
  22. 2. Creating Variables and Mixins
  23. Creating a hover effect example
  24. Transitioning to using PostCSS
  25. Adding variable support to PostCSS
  26. Updating our hover effect demo
  27. Setting the order of plugins
  28. Creating mixins with PostCSS
  29. Looping content with PostCSS
  30. Summary
  31. 3. Nesting Rules
  32. Navigating through pages
  33. Transitioning to using PostCSS plugins
  34. Exploring the pitfalls of nesting
  35. Making the switch to BEM
  36. Exploring our changes in more detail
  37. Summary
  38. 4. Building Media Queries
  39. Exploring custom media queries in PostCSS
  40. Making images responsive
  41. Adding responsive text support
  42. Optimizing media queries
  43. Retrofitting support for older browsers
  44. Moving away from responsive design
  45. Taking things further with CSS4
  46. Summary
  47. 5. Managing Colors, Images, and Fonts
  48. Managing fonts with PostCSS
  49. Creating image sprites
  50. Working with SVG in PostCSS
  51. Adding support for WebP images
  52. Manipulating colors and color palettes
  53. Creating color functions with PostCSS
  54. Summary
  55. 6. Creating Grids
  56. Creating an example with Bourbon Neat
  57. Exploring the grid plugins in PostCSS
  58. Transitioning to using PostCSS-Neat
  59. Creating a site using Neat and PostCSS
  60. Adding responsive capabilities
  61. Summary
  62. 7. Animating Elements
  63. Moving away from jQuery
  64. Making use of pre-built libraries
  65. Switching to using SASS
  66. Making the switch to PostCSS
  67. Exploring plugin options within PostCSS
  68. Updating code to use PostCSS
  69. Creating a demo in PostCSS
  70. Optimizing our animations
  71. Using our own animation plugin
  72. Summary
  73. 8. Creating PostCSS Plugins
  74. Dissecting the architecture of a standard plugin
  75. Creating an transition plugin
  76. Building a custom font plugin
  77. Simplifying the development process
  78. Guidelines for plugin building
  79. Making the plugin available for use
  80. Summary
  81. 9. Working with Shortcuts, Fallbacks, and Packs
  82. Exploring plugin packs for PostCSS
  83. Adding shortcuts with Rucksack
  84. Linting and optimizing your code
  85. Providing fallback support
  86. Summary
  87. 10. Building a Custom Processor
  88. Exploring our processor
  89. Dissecting issues with our processor
  90. Optimizing the output
  91. Adding reload capabilities
  92. Extending our processor further
  93. Testing the final pre-processor
  94. Getting started with some hints and tips
  95. Introducing the CSStyle library
  96. Summary
  97. 11. Manipulating Custom Syntaxes
  98. Preparing our environment
  99. Implementing custom syntax plugins
  100. Parsing CSS
  101. Formatting the output with the API
  102. Highlighting our syntax code
  103. Summary
  104. 12. Mixing Preprocessors
  105. Exploring the conversion process
  106. Introducing the Pleeease library
  107. Compiling with other preprocessors
  108. Using the PreCSS library
  109. Converting a WordPress installation
  110. Setting up our environment
  111. Considering the conversion process
  112. Making changes to our code
  113. Compiling and testing the changes
  114. Summary
  115. 13. Troubleshooting PostCSS Issues
  116. Exploring some common issues
  117. Getting help from others
  118. Summary
  119. 14. Preparing for the Future
  120. Converting CSS4 styles for use
  121. Supporting future syntax with cssnext
  122. Creating plugins to provide extra CSS4 support
  123. Summary
  124. Index

Adding support for WebP images

Manipulating SVG images is an acquired art, and in some instances, it will clearly be overkill for what we need to achieve.

Instead, for those occasions where we need the detail in our images, we might normally use the JPEG format, or potentially PNG as an alternative. There's nothing wrong with either, but, it's old hat, and I do like to push the boundaries of what is possible! In addition, the JPEG image format is lossy and does not support alpha channels; PNG images are lossless, but suffer from larger file sizes for more complex images. If all we did was simply insert images onto a page, then PostCSS wouldn't be helpful here; instead, how about considering a different format altogether?

Enter Google's WebP. You'd be forgiven for thinking "Web…what?", as it isn't a common format! Part of this can be attributed to the lack of take-up; the only browsers to support it natively are Chrome, Android, and Opera. That doesn't mean to say it should be discounted. The format can offer some significant space savings over standard image formats such as JPEG or PNG, while maintaining superior quality. We can even get PostCSS to do most of the work for us, to boot! Let's explore the nuts and bolts of this in more detail, with a simple demo.

Switching WebP images in and out

Image switching is nothing new, we covered one aspect back in Chapter 4, Building Media Queries, when we used PostCSS to switch-in hi-res images when supported in the browser.

We can use a similar technique, but this time with image formats, Google's WebP format was designed as a replacement for the myriad of other image formats available for the web. In an ideal world, we would use the new <picture> tag to take care of switching images automatically:

<picture>
  <source srcset="../img/landscape.webp" type="image/webp">
  <img src="../img/landscape.jpg" alt="The Oslo Opera House">
</picture>

It's not supported in all browsers, so instead, we can use a mix of PostCSS and Modernizr to apply the same effect. The plugin we need for this task is the webpcss plugin (available from https://github.com/lexich/webpcss)—we will need to run npm install gulp-webp --save-dev in a Node.js command prompt session to install the plugin. Let's dive in and take a look at it in more detail.

Tip

For best results, I would recommend using Chrome throughout these two demos, support can be added for Windows and other browsers, by visiting https://developers.google.com/speed/webp/.

Viewing the differences in file sizes

Before we get stuck into using PostCSS, let's take a moment to perform a quick test. The files for this tutorial are in the Tutorial 23 folder:

  1. In the code download that accompanies this book, go ahead and extract a copy of landscape – original version.jpg, and rename it as landscape.jpg. The size should be around 11.5 MB in size.
  2. Save the image to the root of our project area—we also need a copy of cwebp.exe, so go ahead and extract that to our project area as well.
  3. Fire up a command prompt session, change the working folder to our project area, enter gulp, and then press Enter.
  4. If all is well, we should see the results of our conversion, and the new WebP-format image appear in our project area:
    Viewing the differences in file sizes
  5. Try performing the same process with a PNG format image; here are the results of a similar test I performed, with a PNG version of our landscape image:
    Viewing the differences in file sizes

In both cases, the image sizes reduced significantly, the JPEG version dropped from around 12.5 MB to just over 7 MB; the PNG format shrunk from an enormous 25 MB to around the same size!

Note

To learn more about using the WebP format, take a look at the documentation on the Google Developers site at https://developers.google.com/speed/webp/.

Okay, time for another demo! Let's now make use of PostCSS to create our styles for both standard JPEG format, and WebP equivalents:

Viewing the differences in file sizes

For this demo, we'll use the gulp-webpcss plugin, available from https://github.com/lexich/webpcss:

  1. Go ahead and download a copy of the Tuturial23 folder from the code download that accompanies this book, save this at the root of our project area.
  2. Next, go ahead and remove any copies of gulpfile.js and package.json from the root of our project area; we need to replace them with copies from the Tutorial23 folder.
  3. With these files in place, we still need to install the plugins, in a Node.js command prompt window, change the working folder to our project, then run these commands, pressing Enter after each:
    npm install --save-dev gulp-webp 
    npm install --save-dev gulp-webpcss 
    

    Note the order of the parameters in these commands, if they are written in a different order, they will not install.

  4. Copy the style – pre-compile.css file from the Tutorial23 folder to the src folder at the root of our project area, then rename it as style.css.
  5. Fire up a Node.js command prompt, change the working folder to our project area, then enter gulp at the prompt and press Enter.
  6. If all is well, we should see the code shown in this screenshot when viewing the contents of the compiled file; the converted image will also appear in the img folder:
    Viewing the differences in file sizes
  7. Copy the contents of the img folder into the img folder within the Tutorial23 folder.
  8. Copy the style.css file from the dest folder into the css folder within the Tutorial23 folder.
  9. Go ahead and run index.html in a browser, if all is well, we should see something akin to the screenshot at the start of this exercise.

If we run the same index.html in Google Chrome or Firefox, at first we should not see any difference—we'll only see the difference when viewing the compiled source within the Developer Toolbar in Chrome:

Viewing the differences in file sizes

The real benefit, though, is in the img folder within our project area, the original JPEG image we use is 222 KB; however, the WebP is a fraction of this size: it weighs in at just 82 KB. See what I mean about the saving in space?

Okay, onwards we go: time to focus on another area of site building, which is manipulating colors. Colors play a key role within any site, as they make up a part of the message to the end user; let's dive in and take a look at some of the options available when using PostCSS.