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

Supporting future syntax with cssnext

A key part of supporting CSS4 is the constant state of flux that we must deal with, until such time as modules have been standardized. If we maintained our current approach, it would entail constant updates of any plugins we decided to use that relate to CSS4 attributes—this is clearly not sustainable!

Instead, we can use a single plugin pack, in this case cssnext, to manage support for a range of new features. The beauty, though, is that all of the features are enabled by default, and will only kick in when needed in the code. There will of course come a time when new features are supported natively, at this point, we can simply discard the compilation process without impacting the final result.

It's worth taking the time to get to know cssnext—let's dive in and take a look in more detail.

Creating a site template with cssnext

The cssnext plugin is one of those exceptions to our guideline of one plugin per task; we call it using the plugin name, but in reality, it will perform a number of transformations at the same time.

The plugin is available from http://cssnext.io/. It is worth noting that an older version exists; we're using the newer version in this demo. The cssnext plugin was originally a complete system in its own right, before PostCSS became as popular as it is now.

The plugin contained options which didn't really belong to a plugin focused on the future of CSS, so the developers rewrote it to make cssnext simpler. At the same time, it was designed to be integrated into PostCSS, where we can use it at the same time as other plugins within our processor.

Note

The plugin even has its own playground, which we can use to test if changes will produce the desired effect—check it out at http://cssnext.io/playground/.

Let's explore this plugin in more detail—we'll begin by installing it, before setting up code for our next demo.

Setting up our demo

For this next demo, we're going to set up a basic template that can be used for a site—it's not going to win any awards for style, but the aim here is to explore how easy it is to make the changes, not become top billing at the next awards ceremony! Let's dive in and take a look at what we need to do:

  1. We'll start by firing up a Node.js command prompt session then changing the working folder to the root of our project area.
  2. At the prompt, enter this command, then press Enter:
    npm install postcss-cssnext --save-dev
    

    If all is well, we should see something akin to this screenshot:

    Setting up our demo
  3. Next, go ahead and extract a copy of the T71 - working with cssnext folder from the code download that accompanies this book—save it to the root of our project area.
  4. From the T71 - working with cssnext folder, save copies of the styles.css file to the src folder at the root of our project area, then package.json and gulpfile.js to the root of our project area.
  5. Revert to the prompt we had open, then enter gulp and press Enter.
  6. Gulp will go away and compile our file, when it has completed, we will see the now familiar files appear in the dest folder at the root of our project area.
  7. Copy the contents of the dest folder at the root of our project area to the css folder within T71 - working with cssnext folder at the root of our project area.
  8. Go ahead and preview sitepage.html from within the T71 - working with cssnext folder in a browser—if all is well, we should see something akin to this screenshot:
    Setting up our demo

We've now seen the template for our site—there are several places where we have made changes to take advantage of the power of cssnext. For this, go ahead and open up a copy of the file stylescss from within the css folder of T71 - working with cssnext folder that we saved to our project area—let's explore what changes have been made:

  • Autoprefixer: The plugin covers support for providing vendor prefixes for any future styles; any in the base file have been removed, and will be added during compilation.
  • Media query ranges / custom queries: Instead of using the existing format, which isn't always clear, we can begin to use >= or <= to better express the range that we're applying as part of our query. At the same time, we can use a custom query to define preset ranges at the top of our file, which can be used throughout the style sheet.
  • Nesting: Fans of existing preprocessors such as SASS or Less will be familiar with the nesting principle; put simply, this allows us to nest styles inside each other. We can then adjust the selectors used for these rules, so that when compiled, each rule is transformed into valid CSS.
  • Custom selectors: Staying with the preprocessor theme, we can create custom values at the top of our code, and apply them as appropriate throughout.
  • #rrggbbaa colors: Traditionally, hex colors have been expressed as either three or six digit values; using cssnext, we can expand them to either four or eight digit values. The cssnext plugin will transpile them into standard RGBA values, with equivalent HEX values provided as a fallback mechanism.
  • rem units: Traditionally, developers have used pixel values to apply sizes to elements or fonts. Pixel values don't respect user settings in a browser, so may not resize as expected. To work around this, em units were introduced; the math behind calculating em values was simplified with the introduction of rem units. Today, some developers argue that pixel values should reign supreme; the cssnext plugin provides both pixel and rem units, which can be used where supported by browsers.

At first thought, you might expect to have to include a number of plugins, or a detailed configuration object; not so! Instead, all we need in our Gulp task file is this:

Setting up our demo

I've always been a keen fan of keeping things simple—the cssnext plugin is a perfect example! Whilst we may need to update the plugin regularly to keep abreast of changes, we don't need to change our Gulp file.

The plugin will simply transform those styles it finds that are supported by the plugin, and leave alone any not covered by the plugin. The beauty of this is that we can either let it run as is, or if we want disable functionality that is no longer needed, then we simply disable it within the configuration object:

cssnext(input, { 
  features: { customProperties: false }
})

To prove that the changes we made work, we've turned our (non-responsive) template from this:

Setting up our demo

…to this view, where our content clearly fits the smaller screen better:

Setting up our demo

Even though it's only a small part of the changes made, is directly responsible for making our template responsive, it goes to show that incorporating cssnext into our processes is easier than you might think!

Okay, let's move on: we're going to take a look at a couple more plugins, but with a twist. We'll first look at using a plugin to provide support for a new color being introduced as part of CSS4, before using it as a basis for fixing a keyword issue that should have been fixed a long time ago!