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

Providing fallback support

A key concern when designing web content is browser support—which browsers do we support? If the only browsers we had to support were the likes of Firefox or Chrome, then our job would be easy. Trouble is, we also have IE, Safari, Edge…not to mention mobile devices! Life would be boring otherwise…

But I digress, back to reality: for those legacy browsers that refuse to conform (yes, I'm looking at you in particular, IE), we have to consider providing some form of support or graceful degradation. Thankfully, there are a number of plugins we can use within the PostCSS ecosystem—we've already used one in the form of Autoprefixer; there are others available, so let's dive in and take a look at a selection of these plugins in more detail. Before we do so, though, there is a useful tip that I want to explore, which can help with checking for, and providing, legacy support in our browsers.

Detecting support for features

A key part of the development process is to ensure that our code works on those browsers we have to support. If we're lucky enough that this range of browsers is limited to newer offerings, then this is less of an issue.

For some developers, there will be a need to have to support older browsers; this is particularly true if the environment contains other browser-based applications that require use of these older browsers, and cannot be replaced.

To help get around this, we could use libraries such as Modernizr (http://www.modernizr.com), but a more efficient means is to use the CSS @supports directive. In short, this operates in a similar fashion to media queries; we can specify a backup property that is supported by all browsers, and cancel it out if we're using a browser that can support an enhanced property:

section {
  float: left;
}

@supports (display: -webkit-flex) or (display: flex) {
  section {
    display: -webkit-flex;
    display: flex;
    float: none;
  }
}

In the main, this is more likely to be useful for those who have to provide support for older versions of IE (given that other browsers have offered better support for some time). Ideally, we would look to persuade clients of the merits of not supporting older versions of IE (at least 8, and possibly 9). If we have to support them, this method coupled with the use of autoprefixer could prove very useful.

Providing support for oldie

When it comes to providing support for legacy browsers, the biggest culprit is arguably Microsoft's IE. Whilst it has to be said that support in newer versions is improving, its popularity means that there are still enough old versions to warrant the need to provide support!

To their credit, Microsoft have announced that they no longer support IE8 to 10—this is a step in the right direction to help encourage users to upgrade, although it will be some time before these versions completely disappear from use.

So, if you still have to support old versions of IE, what can we do? A good solution to try out is the oldie pack of plugins; its name makes a reference to what some term oldIE, or those versions of IE that should have been consigned to history a long time ago!

This plugin, available from https://github.com/jonathantneal/oldie, is a wrapper for a host of plugins available within PostCSS; examples include the following:

  • post-calc: This reduces any instance of calc() to single values where possible; instances of calc() that use a mix of units may not be replaced
  • postcss-unroot: If our CSS uses :root selectors, then old versions of IE will fail; this plugin replaces them with HTML to allow our code to compile
  • postcss-unnot: In a similar vein, if we've specified rules that include elements where :not pseudo-elements have been used, then these selectors will be removed
  • postcss-unopacity: This converts any instance of the opacity property to use filter: alpha(opacity=XX), where XX is the equivalent value for the original opacity property

The pack includes other plugins—to get a feel for what plugins are included, it's worth taking a look at the index.js file for the plugin pack; it will look something like this:

Providing support for oldie

The beauty, though, is that we are not forced to use the oldie plugin in its entirety. If, by some quirk of fate, we've managed to create something that in the main avoids those CSS attributes that are likely to make oldIE choke, then we can opt to simply reference those plugins that we need to use instead.

This will, of course, depend on how much we've used—a more likely scenario is that we will end up needing to use all of the plugins, so it makes better sense to use oldie instead! Of course, we could always encourage our clients to drop oldIE—this might not be as simple as it sounds…

Removing style hacks from code

If we're lucky enough to have understanding clients—and chance would be a fine thing—then there is likely to be a task that we need to perform: remove any style hacks from our code that relate to browsers we no longer support. The removal of these hacks may be trivial if we only have a small style sheet; the reality is that it will likely be a long, manual process for larger sheets, which carries a risk of us missing hacks.

Instead, we can avail ourselves of a plugin, in the form of stylehacks; this works very well with the stylelint plugin we've already used in demos throughout this book. Available from https://github.com/ben-eb/stylehacks, the plugin uses hacks listed on http://browserhacks.com, and is a cinch to install—let's take a look at it in action:

  1. We'll start by extracting a copy of the T47 - using stylehacks folder from the code download that accompanies this book; save it to the root of our project folder.
  2. Copy the gulpfile.js and package.json files to the root of the project folder.
  3. In a text editor, go ahead and add this code, saving it as style.css in the src folder in our project area (not within the T47 - using stylehacks folder!):
    h1 {
      _color: white;
      color: rgba(255, 255, 255, 0.5);
    }
  4. Next, fire up a Node.js command prompt session, then change the working folder to our project area.
  5. We now need to install the plugin—in the prompt, enter this command, then press Enter:
    npm install stylehacks --save-dev
    
  6. When the plugin has installed, go ahead and enter gulp in the prompt, then press Enter.
  7. PostCSS will compile our code—if all is well, we should see this in the dest folder at the root of our project area:
    Removing style hacks from code

Assuming we have a successful compilation, try opening the style.css file in a text editor—we will, of course, have the requisite source map directive at the foot of the code, but otherwise notice how it has removed the color hack:

Removing style hacks from code

The key to this process lies within this task in our Gulp file:

gulp.task('styles', function () {
  return gulp.src('src/*.css')
    .pipe(postcss([ stylehacks({browsers: 'last 1 version, > 10%'}) ]))
    .pipe(gulp.dest('dest/'));
});

In our Gulp file, we've added the browsers attribute—this tells stylehacks to remove any hacks that are not required for modern browsers or those that have more than 10% global usage. This setting is based on the Browserslist query list available from https://github.com/ai/browserslist—it's worth noting that this can be used for plugins such as Autoprefixer as well.

Note

If we start to include more plugins that make use of the Browserslist queries, then consider using a variable in place of the query; updating this from a central location will automatically update all plugins that use it accordingly. For more details, please refer to the Browserslist site on GitHub.

Although this is a simple plugin to use, there is one nagging question that kept coming back whilst researching for this book: how useful is this plugin in reality? For some, you may think I need my head examined, but there is a reason behind this—let me explain.

If you've spent any time developing with jQuery, then you should be aware that it has dropped support for IE6-8 from jQuery 2.x, and will only develop support for it within the 1.x branch. Much of the baggage within jQuery within the 1.x branch is based on catering for browser hacks; the biggest culprit for these hacks is IE!

At the time of writing, Microsoft have publicly stated that support for IE10 and below is being dropped (at least for Windows 8)—they will only support IE11 on the Windows 8.1 platform, plus their new browser, MS Edge.

Given that many of the hacks found in CSS are for IE (and that this also applies to jQuery), one can't but wonder if the stylehacks plugin will still remain useful within the not too distant future! We should also be asking ourselves if using hacks is a good design decision. Are we designing a problem for ourselves at a later date, or should we be reconsidering our original design, and perhaps revisiting whether we need to support older browsers with a dedicated style sheet, rather than introducing hacks in amongst code designed for more recent browsers?