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

Exploring custom media queries in PostCSS

Making the switch to using PostCSS is a cinch, we can use the postcss-custom-media plugin for this purpose, available at https://github.com/postcss/postcss-custom-media.

The plugin is easy to install, it follows the same principles as all of the other plugins we've covered, so without further ado, let's get that out of the way now:

  1. Fire up a Node.js command prompt, then navigate to the working directory.
  2. At the prompt, enter this command, then press Enter:
    npm install --save-dev postcss-custom-media
    
  3. Keep the command prompt open for now, we will use it in the next few steps.

    With the plugin installed, we can now use it, before we get stuck into converting our previous demo, let's work through a simple example, so you can see it in action:

  4. In a new file, add the following code, saving it as style.css within the src folder at the root of our project area:
    @custom-media --apple-watch (max-device-width: 42mm) and (min-device-width: 38mm);
    
    @media (--apple-watch) {
      h2 {
        font-size: 0.8rem;
      }
    }
  5. Remove the existing gulpfile.js file from the root of the project area.
  6. In a new file, add the following code, this will form a new gulpfile.js file; save this to the root of our project area:
    var gulp = require('gulp');
    var postcss = require('gulp-postcss');
    var customMedia = require('postcss-custom-media');
    
    gulp.task('default', function() {
        return gulp.src('src/*.css')
        .pipe(postcss([ customMedia() ]))
        .pipe(gulp.dest('dest/'));
    });
    
    var watcher = gulp.watch('src/*.css', ['default']);
    watcher.on('change', function(event) {
      console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });
  7. Revert back to the command prompt session we had open earlier, then enter gulp at the command prompt, and press Enter.
  8. If all is well, we should see this code if we open up the compiled style.css from within the dest folder of our project area:
    @media (max-device-width: 42mm) and (min-device-width: 38mm) {
      h2 {
        font-size: 0.8rem;
      }
    }

Believe it or not, this is all that is required to use the plugin; let's take a moment to consider what we've covered through this demo.

At first glance, you might be forgiven for thinking that this plugin doesn't actually do anything to help us—it's a valid point, but there is one key benefit to using this plugin. We can separate out the media breakpoints into separate variable statements, and store these at the top of our style sheet. This means that if we should ever need to update a particular breakpoint, we only need to do it once. Our code is then updated automatically at the compilation stage.

With that in mind, let's get stuck into a demo; we're going to work through the previous plain CSS version of our parallax scrolling example, and convert it to use PostCSS.

Beginning with plain CSS

Over the next few pages, we're going to use a relatively recent technique as the basis for our demo—parallax scrolling. Just in case you've been under a rock, parallax scrolling is a single page application, which allows us to scroll through content whilst showing a number of fixed images behind our content:

Beginning with plain CSS

We'll be using a demo created by Nick Salloum, which is available at http://callmenick.com/_development/simple-parallax-effect/ (I've simplified some of the CSS styles used, removed vendor prefixes, and reduced the number of separate files called by the example). We'll start with a plain CSS version of our demo—go ahead and extract a copy of Tutorial11 to our project area. Try running index.html; if all is well, we should see something akin to the screenshot at the head of this section.

It's a great effect when used well, our interest is in the last section of the CSS file, from around line 133; this section contains the media queries we will convert in our next demo.

Altering our demo to use PostCSS

If media queries are used correctly, this can open up a world of possibilities; we can tweak our style sheet for anything from an iPhone through to a printer. In our demo, we've used a couple to adjust how content is displayed on sites where displays are larger than 600px or 960px width; altering these to work in PostCSS is a cinch.

Note

The CSS3 Media Queries site has a large list of different types of queries that are available; if you check out the site on a target PC or device, it will show you if that query is supported on that device. The full list is available at http://www.cssmediaqueries.com.

We only need to make a couple of changes in the style sheet to switch to using PostCSS, so let's make a start:

Let's make a start on the changes:

  1. We'll start by copying the style.css file from Tutorial11 folder to the src folder in our project area.
  2. We need to edit the file, to convert our media queries to use the PostCSS plugin—go ahead and add these two lines at lines 4 and 5:
    @custom-media --small-viewport all and (min-width: 600px);
    @custom-media --medium-viewport all and (min-width: 960px);
  3. Further down, replace lines 161 and 182 with this code:
    @media (--small-viewport) {
  4. On line 200, replace that line with this code:
    @media (--medium-viewport) {
  5. Save the file—next, go ahead and replace the current gulpfile.js file with the version from the root of the Tutorial12 folder. It has the same initial PostCSS task, but this has been renamed and extended with additional tasks that we've already used from earlier chapters.
  6. Next, go ahead and save a copy of the package.json file from the same location to the root of our project area—this contains updated links to the plugins used in this demo.

    Fire up a Node.js command prompt window, then change the working directory to our project area. At the prompt, enter gulp then press Enter.

  7. If all is well, we should have a compiled CSS file appear in the dest folder—go ahead and copy this into the css folder of the Tutorial12 folder.
  8. Go ahead and run index.html in our project area, to preview the results—if all is well, we should not see anything different, but a quick check in the source code should show that we're using the minified version of our code:
    Altering our demo to use PostCSS

It's worth noting that in our demo, we used a typical format of media query: we could for example extend or alter our style sheet to work on handheld devices such as Galaxy tablets; the same principles apply, but clearly different width values will need to be used! For details on the values to use, take a look at http://cssmediaqueries.com, which has a useful list of queries to use for recent devices.

If we want to push the boundaries of what is possible, there are a couple of options that we can consider:

  • postcss-media-variables: This plugin (available at https://github.com/WolfgangKluge/postcss-media-variables) works in the same way, but allows us to use variables in media queries. The benefit of using this plugin is that we can hive off width values into a central :root rule; we can potentially use one fixed value, but work out others based on this value:
    /* input */
    :root {
        --min-width: 1000px;
        --smallscreen: 480px;
    }
    @media (min-width: var(--min-width)) {}
    @media (max-width: calc(var(--min-width) - 1px)) {}
    
    @custom-media --small-device (max-width: var(--smallscreen));
    @media (--small-device) {}
    /* output */
    @media (min-width: 1000px) {}
    @media (max-width: 999px) {}
    @media (max-width: 480px) {}

    The downside is that it is considered as non-standard, the plugin must be called twice, and if other plugins are used, be called in a certain order—this means it might only suit a specific set of circumstances!

  • postcss-quantity-queries: This plugin (available at https://github.com/pascalduez/postcss-quantity-queries) is based on the SASS quantity queries mixins by Daniel Guillan. This allows us to use rules such as this:
    ul > li:at-least(4) { color: rebeccapurple; }

    Which will compile to this:

    ul > li:nth-last-child(n+4),
    ul > li:nth-last-child(n+4) ~ li {
      color: rebeccapurple;
    }

This is one of four pseudo-selector extensions we can use with this plugin, it's a perfect way to style items such as navigation entries, or if we wanted a numbered list of items with different styles for even or odd numbers.

Note

For a useful reference article on using quantity queries in CSS, head over to the post by Heydon Pickering at http://alistapart.com/article/quantity-queries-for-css.

Let's change tack now, and focus on our content. So far, we've concentrated on the page layout, but we can take it further by making images truly responsive; let's dive in and take a look.