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

Chapter 5. Managing Colors, Images, and Fonts

A website isn't a great website without some form of color, imagery, or fonts—a mix of these will add interest, express content more clearly where words might be insufficient, and generally help maintain visitor engagement.

Users of existing preprocessors will of course be familiar with libraries such as the Compass authoring framework for SASS from http://www.compass-style.org; what if we could produce similar effects, but much faster, and without the need for dependencies? No problem, with PostCSS, we can pick and choose which plugins are needed for our site, and begin to build up a processor that suits our needs. We will cover a number of topics throughout this chapter, which will include:

  • An overview of plugins available to handle colors, images, and fonts
  • Creating image sprites using existing preprocessors
  • Adding SVG support using existing preprocessors
  • Transitioning to using PostCSS plugins
  • Manipulating colors and color palettes using PostCSS plugins

Let's make a start…!

Adding color, fonts, and media to sites

A picture paints a thousand words…

Originally created in the 1920s, this phrase is so apt in the world of digital content—writing a hundred words doesn't have the same appeal if we can replace it with a single image and still convey the same meaning!

A part of any developer or designer's work will be to source the right images or fonts, or choose the right colors, and include them on the site they are building so they can be referenced at the appropriate point. We'll explore some of the plugins and tricks we can use to modify colors, but for now, let's take a look at some of the plugins available for manipulating images and fonts within a site.

Maintaining asset links

When sourcing media for a site, the usual process will be to create a folder for fonts, another for images, and so on, if any part of the process is likely to fail, then it is likely to be with applying incorrect links in our code. The risk of this happening will of course increase if we have a particularly complex folder structure!

Instead, we can take an alternative approach: why not get PostCSS (or a plugin) to do the work for us?

We can use the postcss-assets plugin for this purpose; if we specify a name, it will look in the files relative to the source file, then file paths specified in the loadPaths configuration option, and finally search in the URL specified in the basePath config path. The beauty of this is that we can simply reference the image name, and provided PostCSS finds an image with the same name in one of these preassigned locations, then it will substitute in the appropriate path for us at compilation.

If a link needs to change, then no problem, we can either add a new one in, or modify the existing one; CSS styles will be updated at the next compilation. Let's put this into practice, using the postcss-assets plugin, in a simple demo.

Automating links to assets

Remember the moody landscape image from Chapter 4, Building Media Queries?

In our first example, we're going to rework this demo, but this time use the postcss-assets plugin (available from https://github.com/borodean/postcss-assets) to automate the insertion of links for all of our assets. We'll focus on images and fonts, but this can equally apply to media such as videos as well.

Here's a screenshot to remind ourselves of that image:

Automating links to assets

Let's make a start:

  1. Go ahead and download a copy of the Tuturial19 folder from the code download that accompanies this book, save this at the root of our project area. This contains a partially reworked version of the demo from Chapter 4, Building Media Queries.
  2. Next, go ahead and remove any copies of gulpfile.js and package.json from the root of our project area—we'll start this chapter with fresh copies from our code download.
  3. We now need to install the postcss-assets plugin, so fire up a Node.js command prompt session, enter this command, and then press Enter:
    npm install postcss-assets --save-dev
    

    Don't close it, we will use it again shortly!

  4. We need to extract copies of the gulpfile.js and package.json files from the code download—go ahead and save them to the root of our project area.

    The sharp-eyed amongst you will note we are not installing any other plugins—we're using ones that we have already installed in earlier exercises; the package.json file will include references to these and the postcss-assets file.

  5. In the Tutorial19 folder, look for and copy the styles – pre-compile.css file to the src folder in our project area; rename it to styles.css.
  6. Revert back to the Node.js command prompt window, then enter gulp at the prompt and press Enter.
  7. If all is well, we should have a maps folder and two CSS stylesheets (one full version, one minified)—if we copy these back to the css folder in the Tutorial19 folder, then run the demo, we should see a familiar image of a landscape with early mist, as shown at the start of this demo.

Okay, the image is displayed, along with the text in Roboto font, but how does it all work? It's worth taking a few moments to explore the code; setting it up correctly will help save you a lot of time!

Most of what is in the gulp file you will recognize from earlier demos—we've included the same linting, renaming, and source map creations as before. In addition to the new assets task (to handle our asset links), we've removed the autoprefixer task; we're not calling anything that requires vendor prefixes, so there is no need to use it.

The key process in the gulp file centers on this code—this creates, and substitutes in, the correct asset links. We start with the options configuration object—the loadPaths take care of the asset locations, and relativeTo tells the plugin to set relative links in relation to the dest/ folder. In this case, loadPaths defines specific folders to use; we use relativeTo to make these paths relative:

var options = {
  loadPaths: ['img/', 'fonts/'],
  relativeTo: 'dest/'
};

The dest/ folder is used in our creation process—in reality, this would be the location of our CSS style sheets on the production server. This next simple task simply calls the postcss-assets plugin, and processes each style sheet found in the src folder:

gulp.task('assets', function() {
  return gulp.src('src/*.css')
    .pipe(postcss([ assets(options) ]))
    .pipe(gulp.dest('dest/'));
});

We then simply call the task, if we were to call gulp from a command prompt, then it will run all of these tasks:

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

All in all, a very simple but highly effective tool, it removes the need to insert any links manually, provided we've included them within the configuration object.

Alright…let's move on: we've covered a simple method to ensure we always have the right links for font or image files. There is still an element of manual work required though—do we really need to include all of the lines added for our custom font?

Well, we could always just use a font hosted on Google, but that destroys the point of using PostCSS! Instead, we can simply use the custom font name in our style sheet, but get PostCSS to add in the custom font-face declaration automatically at compilation. Intrigued? Let's take a look at how, as part of our next exercise.