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-Neat

Making the transition to PostCSS is relatively straightforward. We need, of course,to update our compilation process to remove links to SASS, and introduce our PostCSS plugin.

Tip

The transition process will be completed over this and the next two sections.

In terms of changing the CSS, it's a little more complicated, as we have to work out how many columns are required for each grid block. Fortunately, our example is relatively straightforward, as we numbered the original blocks with the appropriate column count, so we can use that as a basis for changing our CSS.

Let's make a start with updating our compilation process:

  1. We'll start by extracting a copy of the Tutorial31 folder from the code download that accompanies this book. Save it to the root of our project area.
  2. From the Tutorial31 folder, go ahead and extract copies of package.json and gulpfile.js files. Save these to the root of our project area.
  3. Next, we need to install the postcss-neat plugin. For this, fire up a Node.js command prompt, then change the working folder to our project area.
  4. At the prompt, go ahead and enter this command, then press Enter:
    npm install postcss-neat --save-dev
    
  5. Node will go away and install our plugin—the plugin is installed, when we see this confirmation:
    Transitioning to using PostCSS-Neat

We now have a plugin installed and configured for use. Before we create a test to confirm it works OK, let's take a quick look at our gulp file, at the root of our project area.

If you were expecting a complex configuration, then I'm sorry to disappoint you—it's even easier than installing Bourbon and Neat using the normal method outlined on their site! Our gulp file contains the requisite variable calls to each plugin at the start, with a watch facility at the end of the file. The section of interest to us is this:

gulp.task('neat', function () {
  var processors = [
    require('autoprefixer-core')({ browsers: ['last 1 version'] }),
    require('postcss-neat')(/* { options } */)
  ];
  return gulp.src('src/*.css')
    .pipe(require('gulp-postcss')(processors))
    .pipe(gulp.dest('dest/'));
});

This setup should satisfy most scenarios, with a default of 12 columns; if there is a need to override it, we can do so by specifying the appropriate option in our configuration object:

postcss([
  ...
  require('postcss-neat')({
    neatMaxWidth: '128em'
  })
  ...
])

We will use this option later in this chapter in the Testing our configuration section, when we build our test example.

Note

For a full list of the attributes that can be modified, head over to https://github.com/jo-asakura/postcss-neat#custom-settings.

We have a basic configuration now in place, but hold on...it looks a little short! The sharp-eyed among you should notice that we've included additional options in the gulp files in previous exercises, such as creating source maps or minifying our CSS files. Let's fix that now, by amending our gulp file to include these missing options. Everything will then be in place, ready for when we create our example site.

Refining our task list

Our gulp file, as it stands, is perfectly usable, but isn't really as useful as it could be—there are a handful of tasks we've built into previous exercises, but which of these are missing here.

A perfect example is the addition of source maps, but how about minifying our code too? Let's take a moment to refine our task list, and add in the missing tasks:

  1. The first task is to add in some variables that will act as references for the various plugins we will use—this goes in immediately after the last var statement, at the top of our gulp file:
    var cssnano = require('cssnano');
    var sourcemaps = require('gulp-sourcemaps');
    var rename = require('gulp-rename');
    var stylelint = require('stylelint');
    var reporter = require('postcss-reporter');
  2. The first task to add in is a facility to lint our styles:
    gulp.task("lint-styles", ['neat'], function() {
      return gulp.src("dest/css/*.css")
        .pipe(postcss([ stylelint({
          "rules": {
            "color-no-invalid-hex": 2,
            "declaration-colon-space-before": [2, "never"],
            "indentation": [2, 2],
            "number-leading-zero": [2, "always"]
          }
        }),
        reporter({
          clearMessages: true,
        })
      ]))
    });
  3. With our styles checked for accuracy and consistency, we can now minify our code. Add the following block:
    gulp.task('rename', ['lint-styles'], function () {
      return gulp.src('dest/css/*.css')
        .pipe(postcss([ cssnano() ]))
        .pipe(rename('style.css'))
        .pipe(gulp.dest("dest/css"));
    });
  4. The next step is to add a source map option:
    gulp.task('sourcemap', ['rename'], function () {
      return gulp.src('dest/css/*.css')
        .pipe(sourcemaps.init())
        .pipe(sourcemaps.write('maps/'))
        .pipe(gulp.dest("dest/css"));
    });
  5. With the additions to our gulp file, we need to adjust the main default task to call these additional tasks:
    gulp.task('default', ['neat', 'lint-styles', 'rename', 'sourcemap']);
  6. We have a watch facility in place, but it knows nothing about these extra tasks; let's add them in now:
    var watcher = gulp.watch('src/*.css', ['default',  'lint-styles', 'rename', 'sourcemap']);

We now have a working gulp file, that includes all of the configuration tasks required for our exercise—let's put it to the test by compiling some example code, to confirm it all works as expected.

Testing our configuration

A key part of our process is testing our gulp file to ensure it works; not only should it run all of the required tasks, but in the correct order, and produce the expected results. Although we've reused existing code for our gulp file, we've made some major changes to our gulp file—let's take a moment to test it is working, using the code from our previous demo.

To get our demo working under PostCSS, we need to make some changes to our code:

  1. We'll start by resaving the style.scss file (from within the css folder in the Tutorial31 folder) as a plain CSS file, and not a SASS stylesheet, we've removed the use of SASS from our previous demo, making the use of the .scss extension redundant.
  2. Next, we used a .wrapper class in our previous demo. This needs to be modified as indicated:
    .wrapper {
      @neat-outer-container;
      margin: 0 auto;
    }
  3. Our col-* class rules need to change too. In place of the static percentages from the old demo, we're going to replace them with this:
    .col-1 { @neat-span-columns 1; }
    .col-2 { @neat-span-columns 2; }
    .col-3 { @neat-span-columns 3; }
    .col-4 { @neat-span-columns 4; }
    .col-5 { @neat-span-columns 5; }
    .col-6 { @neat-span-columns 6; }
    .col-7 { @neat-span-columns 7; }
    .col-8 { @neat-span-columns 8; }
    .col-9 { @neat-span-columns 9; }
    .col-10 { @neat-span-columns 10;}
    .col-11 { @neat-span-columns 11; }
    .col-12 { @neat-span-columns 12; }
  4. Our code is now ready, so go ahead and copy the style.css file into the src folder at the root of our project area.
  5. Next, fire up a Node.js command prompt, then change the working folder to our project area.
  6. At the command prompt, enter gulp then press Enter.
  7. If all is well, we should see a compiled style.css file appear in the dest folder. If we open it up, we should see a number of styles displayed that relate to each column, such as is shown in this screenshot:
    Testing our configuration
  8. If we try previewing the demo in a browser, we should see something akin to this screenshot. Notice how similar it is to the original version, which we built in SASS:
    Testing our configuration

The demo that we've constructed is nearly identical to the original version. This proves that we have a working capability, which we can use to build our sites. The changes we made to our code are very simple, we added a @neat-outer-container to define how wide our site should be, followed by multiple instances of @neat-span-columns, to define how many columns each element should span.

Let's put some of this new knowledge to constructing something a little more useful, in the form of an example site with content. We'll reuse the example site page we created earlier in the chapter, and work through converting it for use with PostCSS plugins.