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

Working with SVG in PostCSS

The rapidly increasing use of mobile devices makes creating responsive content a must; the traditional route is using something akin to max-width: 100% to control the size of an element on screen.

A better alternative is to use SVG—this maintains quality, even when resized; standard image formats will become pixelated if resized to an excessive size. For those of you who have previously used SASS, then there isn't any in-built support for SVG as such; the most we can hope to achieve is efficient nesting within our style sheet.

An example of what we might use can be found in the sass folder within the Tutorial22 folder in the code download that accompanies this book.

If we're a regular user of SVG images within SASS, then it is likely we would use a library such as sass-svg, from https://github.com/davidkpiano/sass-svg. Moving away from SASS to PostCSS is easy; the PostCSS ecosystem has a number of plugins we can use to manipulate images. Let's take a look at how, using the postcss-svg plugin.

Altering icon images using PostCSS

We'll use the postcss-svg plugin (from https://github.com/Pavliko/postcss-svg), to manipulate some icons from the Evil Icon package (available from https://github.com/outpunk/gulp-evil-icons), as part of the next demo:

  1. We'll begin by extracting a copy of the Tutorial22 folder from the code download that accompanies this book. Save it to the root of our project area.
  2. From within the Tutorial22 folder, extract copies of the gulpfile.js and package.json files, and use them to replace any that are currently stored at the root of our project area.
  3. Go ahead and extract a copy of style – pre-compile.css from the same folder; save this as style.css within the src folder. Do the same for the index.html file as well.
  4. In a break to previous demos, we need an additional css folder—go ahead and create one within the dest folder.
  5. Next, fire up a Node.js command prompt, and change the working folder to that of our project area.
  6. We need to install the postcss-svg plugin, so at the command prompt, enter this command, then press Enter:
    npm install postcss-svg --save-dev
    
  7. Once completed, enter gulp at the command prompt, then press Enter.
  8. If all is well, we should see the usual two style sheets appear in the /dest/css folder, along with a source map folder. The HTML markup file will appear in the dest folder.

    If you don't see the source map or minified versions appear, then rerun gulp—sometimes these files will only appear if a compiled style.css file is present.

  9. Copy the contents of the dest folder to the css folder within the Tutorial22 folder—if all is well, we should see these icons appear when previewing the results in a browser:
    Altering icon images using PostCSS

Although this is a simple demo, we've covered some useful tips and tricks within; it's worth taking some time to explore how the demo was put together in more detail.

Exploring the results in more detail

There are several key elements to this exercise that are worthy of attention, the use of a CDN link and Node to provide the style sheet and icons for Evil Icons, the compiled HTML file and the references to use within our custom style sheet. We will cover all of these, but first let's explore the gulp file in more detail.

We begin with these two lines:

var evilIcons = require("gulp-evil-icons");
var postcssSVG = require('postcss-svg')

You should not be surprised to see the latter, but the former is present as the Evil Icons library can be installed using the gulp-evil-icons package. There are a number of different options available for installing, but as we're already using Gulp, it makes sense to continue using the task runner.

Next, we spread our work over two tasks—the first compiles the HTML code to assign the relevant icon image to our <icon> statements within our markup:

gulp.task('icons', function () {
  return gulp.src('src/index.html')
.pipe(evilIcons())
    .pipe(gulp.dest('dest/'));
});

To change the colors requires the use of the postcss-svg plugin, here referenced by postcssSVG:

gulp.task('changecolor', ['icons'], function() {
  gulp.src('src/style.css')
  .pipe(postcss([ postcssSVG() ]))
    .pipe(gulp.dest('dest/'));
});

We of course had to update our default task, if we simply call gulp at the command line, then it will know to run all of these tasks in turn:

gulp.task('default', ['icons', 'changecolor', 'lint-styles' , 'rename', 'sourcemap' ]);

The last step also applies a similar update to our watch facility:

var watcher = gulp.watch('src/*.*', ['default', 'icons', 'changecolor', 'lint-styles', 'rename', 'sourcemap']);

If we then take a look within the HTML markup, we can see a link to the Evil Icons library that was installed using Node.js:

<link rel="stylesheet" href="../node_modules/gulp-evil-icons/node_modules/evil-icons/assets/evil-icons.css">

We then put our customizations into a separate style sheet:

<link rel="stylesheet" type="text/css" href="css/style.css">

These look something like this:

Exploring the results in more detail

At this stage, the CSS styles may look simple, but the HTML markup is anything but; the postcss-svg plugin has added an in-line version of our icons to the HTML markup, with the appropriate edits made from our custom style sheet:

Exploring the results in more detail

Sometimes, it is easy to wonder if using SVG is worth the extra markup, the main benefit being that if it is added in-line, then we reduce the number of calls to external resources; any content that requires altering can be done, without sacrificing the quality of our images.

Considering alternative options

We concentrated on using the postcss-svg plugin throughout our exercise, as a start to manipulating SVG images within the PostCSS system; there are some more options available, which may be of interest:

If you have a need to provide a fall-back position for SVG files, then you can try the postcss-svg-fallback plugin, available from https://github.com/justim/postcss-svg-fallback— we will use this plugin later, in Chapter 8, Creating PostCSS Plugins.

Okay, let's change tack: using SVG images can be a little heavy handed if all we need is a straightforward format for displaying images, right? Well, we could use standard formats, or one which has superior quality while maintaining smaller sizes. I'm talking about the lesser-known WebP format from Google—let's dig in and find out more about this format, and why it deserves more attention.