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

Transitioning to using PostCSS plugins

We've seen that adapting code to use nesting is a simple principle, but the real art is getting the balance right, many developers fall into the trap of nesting everything in their code when using the technique for the first time.

With this in mind, let's explore how we can convert our code to using PostCSS. We'll start by installing the postcss-nesting plugin, which will perform most of the work for us:

  1. Fire up a Node.js command prompt, then change the working directory to our project area.
  2. At the command prompt, enter the following command, then press Enter:
    npm install --save-dev postcss-nesting
    
  3. Node.js will go away and install the plugin—it is ready for use when we see something akin to this screenshot:
    Transitioning to using PostCSS plugins
  4. With the plugin installed, we need to configure PostCSS to use it—open up a copy of gulpfile.js from the project area, ready for editing.
  5. We need to make a few changes—the first is to assign a variable that references the plugin. Add the highlighted line in immediately below the last variable statement:
    var cssnano = require('cssnano');
    var nesting = require('postcss-nesting');
  6. The autoprefixer task needs to be altered—this time around, we will start with compiling our nested code and adding the appropriate vendor prefixes. Alter the first line of this task, as indicated:
    gulp.task('autoprefixer', function() {
            return gulp.src('src/*.css')
  7. Next, add in the nesting configuration call:
         .pipe(postcss([ autoprefixer, nesting({ /* options */ }) ]))
  8. SASS normally compresses any code on compilation by default—as we're no longer using it, we need to provide an alternative. For this, we will reuse the cssnano plugin from Chapter 2, Creating Variables and Mixins. Go ahead and add this at line 20:
    .pipe(postcss([ cssnano() ]))
  9. The lint-styles task should then run once the vendor prefixes have been added; to make this happen, add the constraint as shown:
    gulp.task("lint-styles", ['autoprefixer'], function() {
  10. We no longer have any need for the sass task, so go ahead and remove it in its entirety, and from the default task entry—we should be left with this:
    gulp.task('default', ['lint-styles', 'autoprefixer', 'rename']);
  11. Last, but by no means least, go ahead and switch the order of the rename task. Instead of running it immediately after the autoprefixer task, we'll run it once the lint-styles task has been completed:
    gulp.task('rename', ['lint-styles'], function () {

At this stage, our gulp task file is now ready for use. We can begin to convert our style sheet to use PostCSS nesting as a replacement for SASS. Let's make a start on converting it, as part of the next exercise.

Note

If you get stuck, there is a completed version of gulpfile.js in the code download that accompanies this book—simply extract a copy and place it in the root of our project area to use it.

Converting our demo to PostCSS

Altering our code to use PostCSS is very simple. Even if it requires a few changes, the format does not change significantly when compared to processors such as SASS; let's take a look at what is involved:

  1. We'll begin by opening a copy of style.scss from the Tutorial6A folder in the code download that accompanies this book—save it to the src folder of our project area. Rename it to style.css.
  2. On line 19, add @nest immediately before &:, as indicated—this is required to allow the postcss-nesting plugin to correctly compile each nesting statement:
    @nest &:before, &:after {
  3. On line 53, add @nest & immediately before h2, as shown:
    @nest & h2 {
  4. On line 61, add @nest immediately before &., as shown:
    @nest &.page1 {

    Repeat step 4 for lines 65, 69 and 73.

  5. On line 119, add @nest immediately before &., as shown:
    @nest &.invisible {
  6. On line 123, add @nest immediately before ul, as shown:
    @nest & ul {
  7. On line 125, add @nest immediately before & li, as shown:
    @nest & li {
  8. On line 136, add @nest immediately before &., as shown:
    @nest &:after {

    Repeat the same process for lines 150 and 155.

  9. On lines 179, add @nest immediately before &., as shown:
    @nest &.up {

    Repeat the same process for lines 183 and 187, then save the file.

Our style sheet is now converted; to prove it works, we need to run it through PostCSS, so let's do that now as part of the next exercise.

Compiling our code

With the changes made to our code, we need to compile it—let's go ahead and do that now, using the same process we saw back in Chapter 2, Creating Variables and Mixins:

  1. Fire up a Node.js command prompt session, or use the one from earlier if you still have it open, and change the working folder to the project area.
  2. At the prompt, enter this command, then press Enter:
    gulp
    
  3. If all is well, we should see something akin to this screenshot:
    Compiling our code
  4. A quick peek in the dest folder of our project area should reveal the relevant compiled CSS and source map files, produced by PostCSS.
  5. At this point, we need to extract a copy of the Tutorial7 folder from the code download that accompanies this book—save this to our project area.
  6. Copy the contents of the dest folder from our project area to the css folder under Tutorial7—if all is well, our demo should continue to work, but without the dependency of SASS.

    Note

    Note, make sure you expand the demo to the full width of the screen to view it properly!

Try previewing the results in a browser—if all is well, we should see the same results appear as before, but this time using PostCSS, and without the dependency on SASS. We can now apply the same techniques to any project, safe in the knowledge that using the postcss-nesting plugin will allow us to compile to valid CSS code—or will it?