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

Creating a demo in PostCSS

As an afterthought to the previous exercise, I posed the question, "How many times have you seen forms that display labels above, or to the right of, fields?" If I were to collect a dime for each answer, I suspect I would be off on some exotic island, rich, and without a care in the world—I've lost count of the number of times I've seen such forms, let alone anyone else who uses the Internet!

There is no excuse for plain, boring forms. To prove this, we're going to create a simple demo using the postcss-transform-shortcut plugin by Jonathan Neal, available from https://github.com/jonathantneal/postcss-transform-shortcut. It's a straightforward plugin that allows us to specify single properties, which the plugin combines into a single line of code within our style sheet. Let's quickly install it:

  1. First, go ahead and fire up a Node.js command prompt session, then change the working folder to our project area.
  2. At the prompt, enter this command, then press Enter:
    npm install postcss-transform-shortcut --save-dev
    
  3. Node will now install the plugin—it will return back to a flashing prompt when this is complete.

There is no need to configure it, although there is a small task we have to complete before we can use it.

Updating the plugin

While researching for this book, I came across an issue in the current release (1.0.0), whereby style sheets weren't compiling properly if they had multiple rules within; there are occasions when plugins may or may not work for your environment, and this is one of them!

Thankfully, this is an easy fix—if we take a look within the postcss-transform-shortcut folder within the node_modules folder in our project area, we should see this:

Updating the plugin

Simply copy the contents of the file at https://raw.githubusercontent.com/pc035860/postcss-transform-shortcut/07af8a78d1fb5e7fdeebc8c7f56c0c9ecdd83efb/index.js and paste straight over the top of index.js; this should resolve the issue.

Note

This has been logged as an issue in the developer's GitHub site, at https://github.com/jonathantneal/postcss-transform-shortcut/issues/4, if you would like to see more details about the issue.

Building our demo

Now that we have our updated plugin in place, we can get on with building our demo! The next exercise will take the form of a simple credit card form—I don't suggest you use it in a production environment, as it is purely designed to show the animation effects only, and does not have any security attached to the form!

That aside, here's a screenshot of what we're going to produce, using PostCSS:

Building our demo

It's a simple demo, based on a codepen created by Michael Arestad, which you can view at http://codepen.io/MichaelArestad/pen/ohLIa—I've simplified and reworked the demo to illustrate how we can use PostCSS to compile the code into valid CSS styles.

Okay, let's make a start with setting up our demo:

  1. We'll start by extracting a copy of the T40 – Creating a demo in PostCSS folder from the code download that accompanies this book; save it to our project area.
  2. From within the folder, move the package.json and gulpfile.js files up a level to the root of our project area.
  3. In the css – completed versions folder, copy style – pre-compile version.css to the src folder, and rename as style.css.
  4. Next, fire up a Node.js command prompt session, then change the working folder to our project area.
  5. At the prompt, enter gulp, then press Enter—PostCSS will go away and compile our code; if all is well, we should see our compiled style sheet files and source maps appear in the dest folder.
  6. Copy the contents of the dest folder to the css folder within the original T40 – Creating a demo in PostCSS folder.
  7. Go ahead and preview the results—if all is well, we should see something akin to the screenshot shown at the start of our exercise.

It's a simple demo, but it shows off how we can use animations perfectly—it adds a subtle effect to the label, and doesn't spoil the overall use of our form. The use of the plugin does raise a couple of useful points, so let's take a moment to explore what we've just created in more detail.

Dissecting our demo in more detail

The key to a successful plugin in PostCSS is one that follows the 1:1 principle—one plugin for one task. The postcss-transform-shortcut plugin is no exception: it takes the various elements that make up a transition rule, and puts them together in the right order. To see what we mean, take a look at these lines from within our style sheet before it is compiled:

Dissecting our demo in more detail

Where's our transform: statement? Well, when using this plugin, it's not needed—instead, we can simply specify the various attributes, thus:

.transform {
  transform: skewX(25deg);
  rotate: 180deg;
  scale: 2 2;
  translate: 10px 10px;
}

The plugin is set to recognize these four attributes and compile them into one single rule, as shown in this code excerpt:

.transform {
  transform: skewX(25deg) rotate3d(180deg,0,1) scale3d(2,2,1) translate3d(10px,10px,0px);
}

Any gaps in the attributes will be automatically filled in with default values from within the plugin. We can even use this plugin as the basis for an equivalent for transitions—we will do this toward the end of the next chapter.