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

Linting and optimizing your code

Bandwidth usage has always been critical to the success of a website; remember the good old days of 56K modems? We've come a long way since then, but this is still no excuse for producing sites that swallow bandwidth like it's going out of fashion!

A part of this comes in the form of linting and minifying our style sheets before deploying into production use—it goes without saying that this should form part of any developer's workflow process by default. We can do this manually, but this manual job is prone to missing opportunities, which can lead to inconsistencies in our code.

Instead, we can use the power of PostCSS to perform the heavy lifting for us; the stylelint and cssnano plugin packs make for a powerful optimization facility! If we take a careful look at most gulp task files that we've created throughout the course of this book, both processes are taking place; in this example, stylelint is used at line 22, and cssnano at line 38:

Linting and optimizing your code

Exploring the use of cssnano

For anyone starting out with PostCSS for the first time, then simply specifying cssnano() as one of the processors for PostCSS should be sufficient:

Exploring the use of cssnano

If we take a look at the T45 – converting to use Rucksack demo, our original style sheet file weighs in at 4KB when compiled, but which drops to 3KB after compression. Granted, it's only a small file, but a 25% drop in size is still not an insignificant drop!

Tip

At this point, it is worth noting that even though we are using Gulp, the plugin in use is the PostCSS version, and not gulp-cssnano.

The cssnano plugin is not a single plugin, but a wrapper for a number of plugins, which include examples such as these:

  • postcss-reduce-idents: This reduces any custom identifier names (such as those used in @keyframes) to two letter equivalent codes; this helps with minifying code.
  • postcss-zindex: This plugin reduces any z-index declarations that are unnecessarily higher than they should be.
  • postcss-convert-values: If our CSS uses any number of different units, then we can reduce the CSS size by expressing the value a different way. For example, 400ms can be expressed as .4s (a reduction by two characters). Some might argue this is a little extreme, but every little helps!
  • postcss-colormin: In a similar vein, we can reduce the length of color names using this plugin: if rgba(255, 0, 0, 1) is used in our code, then we can replace this with red. Although the name is indeed shorter, this is at the expense of losing consistency with naming our colors, which may not be so desirable.

Moving on, there are some key points we should be aware of, when using cssnano:

  • You will notice the presence of the gulp-rename plugin in use within our Gulp file—cssnano does not have a capability to rename a compressed file to something we would expect to see within our code. We can use gulp-rename to create a version that developers would expect to see in code; it does leave a copy of the original file in place, if needed.
  • Most options within cssnano are enabled by default; we can switch off individual ones in the configuration object, as shown in this example:
    var nano = require('gulp-cssnano');
    
    gulp.task('default', function () {
        return gulp.src('./main.css')
            .pipe(nano({discardComments: {removeAll: true}}))
            .pipe(gulp.dest('./out'));
    });

    Tip

    For a full list of the transform options, take a look at http://cssnano.co/options/. Click on the link to view individual configuration options for that plugin.

  • This plugin automatically includes autoprefixer. Technically, there is no need to include it separately as we have done in previous exercises, so ideally it should be removed. We will focus on this more as part of optimizing our processor in Chapter 10, Building a Custom Processor.
  • There are some transforms that are available within cssnano, but which are not switched on by default; these are not considered safe and should only be included if you are 100% sure it has not affected your code. The details of unsafe transforms are available on the options page at http://cssnano.co/options/.

Okay, let's move on: the second half of our double act is the stylelint plugin pack; unlike cssnano, stylelint takes the opposite approach and allows you to enable any rule as needed, from a list of over 100 available rules. Let's dive in and take a look in more detail.

Configuring Stylelint as our linter

How does one describe Stylelint, if you've never met it before? Well, to quote its website, "stylelint is a mighty, modern CSS linter".

Whether we agree with such a bold statement, it is certainly worth getting to know Stylelint as a linter. Available from http://stylelint.io/, the key to this plugin lies not in the plugin itself, but in the rules that define what we want to check in our code. At present, we can use any one of 100+ rules, or a mix of several; these can be specified in a .styleintrc file, within our package.json file, or as a stylelint.config.js file that exports a JavaScript object.

We've already used stylelint in earlier projects; for convenience, our Gulp task for linting styles has a number of rules specified within the object:

Configuring Stylelint as our linter

I've chosen a number of rules to illustrate how we can use Stylelint; it is, of course, up to each of you as developers to choose which rules you want to test as part of linting your code. Stylelint does not contain a core set of rules that are enabled by default—any checking it does will be dependent on what is specified in the rule configuration.

Tip

A useful source to bookmark is http://stylelint.io/—this contains a full set of rules that can be added to our Stylelint configuration prior to compiling code.

For example, if we were building a responsive site that made heavy use of the Golden Rule, we may want to limit any percentage values to no more than three or four places. For this, we would specify the number-max-precision rule—this takes an integer value; specifying 3 would flag warnings for these two attributes:

.foo { top: 3.2456px; }
.foo { top: 3.245634px; }

This is not the case for this one:

@media (min-width: 3.234em) {...}

I would strongly recommend reading through the list of rules to get a flavor of what is available; it will take time to familiarize yourselves with the contents, but the reward will be code that is optimized and checked prior to it being used in a production environment. There is one small point though—even if we optimize our code ad infinitum, there is always a possibility that we still have to include some support for older browsers.

In an ideal world, we would convince our clients of the merits of limiting such support (or not even covering it). If clients insist on it, however, against our better judgment, then PostCSS can easily help with providing that support. Let's explore what is available—much of this will center around IE (as this is the biggest culprit), but will equally apply to other browsers.