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 site using Neat and PostCSS

Remember our demo with a Japanese theme from earlier, in Creating an example with Bourbon Neat? It's a simple demo, using Bourbon Neat to help create our grid. The downside, though, is, of course, the dependency on SASS!

Well, we can fix that: PostCSS has a plugin available that mimics Bourbon Neat, but is written entirely in JavaScript, so there is no dependency on SASS. It's easy to install and use, over the next few pages, we'll work through the changes required to switch to this plugin.

First though, let's get it set up:

  1. We'll begin by extracting a copy of the Tutorial32 folder from the code download that accompanies this book. Save this to the root of our project area.
  2. Copy the sample pre-compile.css file to the src folder at the root of our project area.
  3. Copy the gulpfile.js, samplesite.html and package.json files to the root of our project area. These should replace any existing versions that are present.
  4. Next, we need to install two plugins, although we've covered using postcss-css-variables earlier in the book, installing them will ensure the right references are added to the package.json file. Go ahead and fire up a Node.js command prompt, then change the working folder to our project area.
  5. At the command prompt, enter these two statements in turn, pressing Enter between each one:
    npm install postcss-css-variables --save-dev
    npm install postcss-nested
    
  6. When both plugins are installed, go ahead and enter gulp, then press Enter to fire off a compilation of our style sheet.
  7. If all is well, we should see two style sheets and a source map folder appear in the dest folder. Copy these to the css folder at the root of our project area.
  8. If we fire up a copy of samplesite.html, we should see our demo appear as before, but this time without the dependency on SASS:
    Creating a site using Neat and PostCSS

Do you notice any difference to our SASS-only version of this demo, from earlier? Hopefully not; while it may not be pixel-identical to the original, it is not far from it! However, it does show that with a little ingenuity, it is possible to make the transition to using PostCSS and still maintain the same results. It will require a few changes to your code and processes, so let's take a look at these in more detail, starting with the style sheet.

Making the change to PostCSS

Making the switch requires changes in both the gulp task file and style sheet. These are not to change how the page will look, but to maintain the same theme from the original demo. The key changes made to the style sheet are:

  • The _reset.scss partial style sheet that we import will no longer work, as we are removing the reliance on SASS. To maintain its use, a compiled version was created using the online playground at Sassmeister (http://www.sassmeister.com); we can then link to it from our markup page.
  • If you take a peek at the source version of sample.css, you will see a :root block at the top of the file; this replaces the import statements we used. This block can be used to store any variables used, and we will cover this in more detail when we explore the changes made to our gulp task file.
  • We no longer needed the following three statements; they are used to debug the SASS version of Bourbon Neat, and were then removed:
    $visual-grid: true;
    $visual-grid-color: #E6F6FF;
    $visual-grid-opacity: 0.4;
  • We're using PostCSS equivalents for all of the variable statements. The SASS versions were modified using search and replace from $... to var(--….), where the ... represents the variable name.
  • Our original code had a number of references to Bourbon mixins which had to be updated. We used the same search and replace principle, this time changing @include outer… to @neat-outer… throughout the code.
  • To keep things simple, we manually calculated any instance where $body-line-height was referenced, and replaced the calculation with the result. We could have stayed with using calculations, but it would have required the use of another plugin which would have been overkill for their limited use in our code.
  • We also adjusted the width of the main area in our page; it's a minor quirk, but required to ensure we had two areas side by side, and not one above the other!

In addition to altering our style sheet, we also had to make some changes to the gulp task file. They center around replacing the main compilation task and adding in additional tasks to manage production and minification of our source files:

  • We added in the rename, lint-styles, and sourcemap tasks covered in earlier demos. These already worked well, and required no modification.
  • We stripped out the original styles task, and replaced it with this:
    Making the change to PostCSS

    This time, we're calling them nested(), cssvariables() and Neat plugins. These are referenced used variables and are added in at the top of our gulp file.

  • Our final change is at the end of the gulp file, where we had to adjust the default and watcher tasks to include the additional tasks that we added to our gulp file.

At this stage, do we have a working demo ready for use? Well, not quite, but let's try resizing our demo:

Making the change to PostCSS

Hmm, what's happened to our content? It doesn't look great, does it? We can easily fix it though; it just requires the addition of some media queries to reorganize how our content is displayed on the screen. Let's dive in and take a look at what is needed to get our demo looking better at smaller sizes.