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

Manipulating colors and color palettes

A challenge that any developer or designer will face is which color should be used on a site—a nice shade of red, or how about sea blue, for example? It doesn't matter whether they are responsible for choosing the shade to use, or if they have to pick the right RGB or HEX color to use.

Irrespective of where responsibilities lie, we still have to choose a color, and there is a good chance we won't be choosing one that comes from the default 256-color palette, but one that is likely to be a lighter or darker shade, or perhaps a mix of two colors:

Manipulating colors and color palettes

Anyone used to working with SASS will already be aware of functions such as lighten(), darken() or saturate()—the great thing about PostCSS is that we can replicate similar functionality for those who want to move away from the dependency of SASS.

To see how easy it is to use, we're going to combine the power of two plugins for PostCSS—postcss-color-palette (available at https://github.com/zaim/postcss-color-palette), and postcss-color-mix (from https://github.com/iamstarkov/postcss-color-mix). The former allows us to choose one or more colors from any of three palettes, while postcss-color-mix will mix specific colors to make a new color. There are reasons for using these plugins, which will become clear; for now, let's get stuck in and watch these plugins in action.

Displaying and mixing colors using palettes

In this exercise, we're going to take a look at mixing colors; postcss-color-palette allows us to choose multiple colors by name (and not by number!), then converts them to HEX equivalent values. We can then either create gradient-type effects, or simply mix the colors together (using postcss-color-mix) to produce a new color.

Let's make a start:

  1. We'll start by extracting a copy of the Tutorial24 folder from the code download that accompanies this book; save the folder to the root of our project area.
  2. From the Tutorial24 folder, copy the package.json and gulpfile.js files to the root of our project area.
  3. We also need our stylesheet, for this, go ahead and copy the style – pre-compile.css file from the same folder and drop this into the src folder in our project area. Rename it as style.css.
  4. At this point we need to install the plugin, for this, so go ahead and fire up a Node.js command prompt session, then change the working folder to our project area.
  5. At the prompt, enter the command shown in this screenshot, then press Enter, if all is well, we should see confirmation that the plugin has installed correctly:
    Displaying and mixing colors using palettes
  6. Repeat step 5, but this time, run the command shown in this screenshot:
    Displaying and mixing colors using palettes
  7. At the prompt, enter gulp, then press Enter—PostCSS will go away and compile the style sheet, and drop the compiled results into the dest folder.
  8. Copy the contents of the dest folder (which will be the uncompressed and minified style sheets, along with a source map file) to the css folder within the Tutorial24 folder.
  9. Try previewing index.html at the root of our Tutorial24 folder; if all is well, we should see our mixed colors, as shown in this screenshot:
    Displaying and mixing colors using palettes

Okay, the colors I've chosen clearly aren't going to win any style awards any time soon, but they help serve a purpose: it is very easy to use proper color names, if preferred, while still allowing PostCSS to compile them into valid HEX values. That aside, let's take a moment to consider the code we've used in this demo—it does raise a few key points, which we should cover, when using these plugins.

Dissecting our demo in more detail

The demo we've created follows similar principles to most other demos we've built so far; we begin with declaring variables to store instances of our plugins, thus:

var palette = require('postcss-color-palette');
var colormix = require('postcss-color-mix')

The magic then happens in this task, within our gulp file:

gulp.task('palette', function () {
  return gulp.src('src/*.css')
  .pipe(postcss([ autoprefixer, palette({ palette: 'mrmrs' }), colormix() ]))
  .pipe(gulp.dest('dest/'));
});

Notice that we've specified a palette to use, the mrmrs option is the default, but we can equally use material or flatui as alternatives. All three reference the webcolors plugin from https://github.com/zaim/webcolors/; this package could be expanded to include other palettes if desired.

With the links to our two plugins in place, and the task set up, we can then begin to specify rules within our style sheet, which will use the plugins. We've created three, and all three use the postcss-color-palette to determine what the HEX value should be for each color; the third and final mixes the two colors together once HEX values have been assigned:

#box0 { background: linear-gradient(aqua, blue 50%, purple); }

#box1 { background: linear-gradient(to right, orange, red, yellow); }

#box2 { background: mix(teal, navy, 80%); }

Getting the mix of the color right for the third rule isn't easy, the key to a successful mix is to avoid using colors that are in the same spectrum; the closer they are, the less impact the mix will have!

If you want a quick way to gauge how well colors have mixed, then try http://jackiebalzer.com/color—this demo has a mix() option in it, which will compile them in the browser and avoid the need to run the compilation process manually.

We've covered some of the plugins that are likely to be more popular; there are more available via the PostCSS.parts directory, which may be of interest:

  • colorguard: Helps maintain a consistent color palette
  • postcss-ase-colors: Replaces color names with values read from an ASE palette file; this is perfect if you happen to be a user of Adobe PhotoShop, InDesign, or Illustrator
  • postcss-shades-of-gray: Helps keep grayscale colors consistent to a gray palette
  • postcss-color-pantone: Transforms Pantone color to RGB.

In the meantime, let's move on: we've explored using palettes to select our colors, before mixing them to create new ones. This is just scratching the surface of what is possible; how about creating different shades of colors, using functions such as darken(), tint() or lightness()? Such functions already exist in most preprocessors, such as SASS; let's explore how we can achieve the same results using PostCSS plugins.