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

Managing fonts with PostCSS

In our previous demo, we explored a means to automatically add links using PostCSS—it shortcuts the need to worry about providing the right locations for files. The trouble is, when used with custom fonts, it still requires too much work (yes, I know, we humans are inherently lazy!). There is a better alternative:

Enter the postcss-fontpath plugin, available from https://github.com/seaneking/postcss-fontpath; this is a simple plugin that requires limited information about our custom font, and in return will produce the full font-face declaration at the compilation stage.

So, rather than talk about it, why don't we put it to use? Let's revisit the responsive image demo we covered in the previous demo, and alter our style sheet to use the fontpath plugin to handle our custom font:

  1. We'll start by extracting a copy of the Tutorial20 folder from the code download that accompanies this book, and save the folder to the root of our project area.
  2. Next, take a copy of package.json and gulpfile.js files from the Tutorial20 folder, and replace the existing versions that are at the root of our project area.
  3. Go ahead and fire up a Node.js command prompt, and change the working folder to that of our project area.
  4. At the command prompt, enter this command, then press Enter:
    npm install postcss-fontpath --save-dev
    

    Although we've installed the plugin explicitly, we can easily install it using just npm install; the presence of the package.json file in the folder will tell NPM what to install (in this case the missing postcss-fontpath plugin). Keep the session open, we will use it again shortly.

  5. Take a copy of styles – pre-compile.css from the css – completed version folder, and save this as styles.css into the src folder at the root of our project area.
  6. Revert back to the Node.js command prompt window, then enter gulp at the prompt, and press Enter.
  7. If all is well, we should see the, by now, familiar style sheets and source map appear in the dest folder; copy these to the css folder within the Tutorial20 folder.

At this point, we should now have a working demo; we won't see anything intrinsically different, but know that at compilation, PostCSS has automatically added the right font-face declarations for our font.

The beauty about this plugin is in its simplicity—it needs no more than the addition of a simple command in the main task:

gulp.task('fonts', function () {
  return gulp.src('src/*.css').pipe(
    postcss([ fontpath() ])
  ).pipe(
    gulp.dest('dest/')
  );
});

There is no need to have to specify any additional configuration elements or rules, the plugin does exactly what it says on the tin, so to speak! Although we've not achieved anything ground-breaking with this example, it does serve to illustrate some key points about using PostCSS:

  • PostCSS works best when plugins concentrate on a single task and don't try to achieve everything under the sun in one go. Adhering to the single responsibility principle means we can reduce duplication, make the plugin more robust, and avoid instances where changes can end up breaking functionality elsewhere in our processor! This plugin is perfect—it just provides a font-face declaration for the specified font, and nothing else.
  • Sometimes, when choosing the right plugin in PostCSS, there will be occasions when we choose something that later turns out not to work as expected. A case in point is the postcss-font-magician plugin (available from https://github.com/jonathantneal/postcss-font-magician); it has the right idea of providing font-face declarations, but tries to provide them for Google-hosted fonts, locally hosted fonts, Bootstrap, and so on.

    Note

    Unfortunately, the net result is that at the time of writing, not all of the functionality appears to work as expected, so it is at this point where we have to look for alternatives.

If you would like to explore more, then the postcss.parts directory (at http://www.postcss.parts) has more options available; two that might be of interest are the Assets Rebase plugin (from https://github.com/devex-web-frontend/postcss-assets-rebase), and the PostCSS Font Pack plugin, from https://github.com/jedmao/postcss-font-pack. We will cover the latter plugin in more detail in Chapter 8, Creating PostCSS Plugins.

Okay, so we have our text in place: it does look a little boring, doesn't it? Well, we can fix that by adding images. So, how exactly can PostCSS help us, I hear you ask?

It can help in a number of ways—instead of using plain colors, we can begin to mix some together, for example. Or how about using image sprites? A pain to create manually, right? Not with PostCSS. I'll bet you've seen some of the image filters you can use on images (such as sepia or tint), but found that they don't work in every browser right?

These are just some of the ways that PostCSS can help us, and we will cover all of these and more throughout this chapter. Let's make a start though on working with images: our first demo will cover the creation of image sprites. We'll start with a quick recap of the SASS process, before switching to using PostCSS.