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 6. Creating Grids

There are several different routes to take when creating basic site layouts, and in many cases, developers may decide to use CSS grids.

A classic example for those using CSS pre-processors, is of course, the SASS grid system, Bourbon Neat—a great package, spoiled by the need to install Ruby. We can easily fix this in PostCSS, by using one of several plugins available, without the need for extra dependencies. In this chapter, we'll take a look at what's available, and work through some examples, using a plugin for creating grids within PostCSS.

We will cover a number of topics throughout this chapter, which will include:

  • Introducing the basic principles of using CSS grids
  • Exploring the grid plugins available for use within PostCSS
  • Working through some simple examples using Bourbon Neat
  • Replicating pure SCSS examples using the PostCSS plugin, PostCSS-Neat
  • Adding responsive capabilities using the PostCSS-media-minmax plugin

Let's get cracking…!

Introducing grid design

The principles of using grids in design are not new, they date from the Second World War, with a number of graphic designers questioning the design of conventional page layouts, in favor of designing a system that provided a flexible, yet coherent, layout.

The same principles have been transferred to the web, starting with plain HTML, and CSS-based designs, before newer frameworks took over and helped to make construction easier.

It doesn't matter how the design is constructed, we can of course use HTML and CSS, or we might favor the image template approach (using packages such as PhotoShop), particularly if responsibility for designing the front end falls with a different team.

These are perfectly acceptable methods, but require a lot of manual effort—in this age of web design, time is critical; we can instead make use of newer frameworks (such as SASS, Bourbon Neat, or Bootstrap) to create our grids, as shown in this example (which uses plain SASS):

Introducing grid design

We can see this type of layout in action, if we go ahead and extract the Tutorial28 folder from the code download that accompanies this book, then review it using a browser. We will see this grid appear, the style.css file used by this demo was created using the online SASS playground, Sassmeister at: http://www.sassmeister.com.

Much of the code used in this demo centers around each column width and the overall .wrapper container; if you take a look at the code, you will notice that there are no static values for column widths. There are a couple of static values, but their sizes are not critical to the overall effect.

The key to our demo working centers around this block of CSS styling:

Introducing grid design

Here, we're using SASS's interpolation to first build our media query (to make it responsive), followed by styles for a series of columns that form our grid. When compiled, it creates a number of styles that apply to each part of our grid:

Introducing grid design

It's a simple matter of matching up the style with the number shown on the grid. If we want to change the widths, we simply need to increase the number of columns, and our for statement will automatically calculate a new set of values at the next compilation.

Okay, enough chitchat: time, I think, for a demo! Throughout this chapter, we will work through the principles of migrating from some basic examples using SASS, through to using Bourbon Neat, before converting to using PostCSS plugins. We always have to start somewhere, so let's begin with automating our compilation process using SASS.

Automating the compilation process

"Installing SASS?" I hear you ask Why, when this book is about PostCSS?

I hear you, it's a good question: there is logic, though, in this madness—let me explain all:

While we are installing SASS, we're not going to use the standard route to installing it; instead, we're going to use the gulp-sass plugin. This allows us to make the initial switch to using a gulp file; this puts us one step further on down the route to converting our processes to use PostCSS. The use of a gulp file provides a convenient framework where we can switch components in, or out, while we transition to using PostCSS.

Note

In Chapter 12, Mixing Preprocessors, we will see how PostCSS works well with other preprocessors, as a basis for adopting a consistent approach to compiling code.

So, without further ado, let's make a start on installing the gulp-sass plugin, before putting it to work:

  1. We'll start by firing up a Node.js command prompt session, then changing the working folder to our project area.
  2. At the prompt, go ahead and enter this command, then press Enter:
    npm install gulp-sass --save-dev
    

    Don't close the window, we will need it shortly!

  3. Node will go away and install gulp-sass; it returns to the prompt when the installation is completed.
  4. With the plugin installed, we now need to compile our code—go ahead and extract a copy of the Tutorial29 folder to our project area.
  5. Copy the contents of the sass – pre-compile folder to the src folder at the root of our project area.
  6. We also need to add the gulpfile.js and package.json files from the Tutorial29 folder to the root of our project area.
  7. Revert back to the Node.js window we had earlier, then at the prompt, enter gulp and press Enter.
  8. The files will now compile—once completed, copy them into the css folder within the Tutorial29 folder.
  9. Try previewing the results of our work in a browser; if all is well, we should see something akin to this screenshot:
    Automating the compilation process

Right, we now have automatic support for compiling in place; "What next?" I hear you ask. We're one step closer, in that our code can now be compiled automatically:

Automating the compilation process

However, manual effort is still required to construct our grid! Let's start to change that now, there are several frameworks available that we can use, but in my view, one of the cleanest is SASS's Bourbon Neat. We'll use this as the basis for our next few exercises, before migrating to use the PostCSS version of this framework.

Adding support for Bourbon Neat

For the uninitiated, SASS's grid capability is provided by the Bourbon Neat add-on (available from http://neat.bourbon.io/). For the purposes of our exercise, we're going to use the Node versions of the framework—this requires two installations to be completed, so let's go ahead and do that now:

  1. If you still have it open, revert back to the Node.js command prompt session from the previous demo; otherwise, open a new one and change the working folder to our project area.
  2. At the prompt, enter these two commands in turn, pressing Enter after each:
    npm install node-bourbon --save-dev
    npm install node-neat --save-dev
    
  3. Both plugins will have installed correctly when we see a result akin to this screenshot:
    Adding support for Bourbon Neat
  4. With the plugins now installed, we need to modify our gulp file—go ahead and add this at line 5:
    var neat = require('node-neat').includePaths;
  5. Next, leave a line, then add the code as shown, at line 7:
    var paths = {
      scss: 'src/*.scss'
    };
  6. The original SASS task can be replaced with this new task:
    gulp.task('styles', function () {
      return gulp.src(paths.scss)
      .pipe(sass({
        includePaths: require('node-neat').includePaths
      }))
      .pipe(gulp.dest('dest/'));
    });
  7. The reference to SASS in the default task is now incorrect—go ahead and change it to: gulp.task('default', ['styles']);
  8. Finally, change this line as indicated:
    var watcher = gulp.watch('src/*.scss', ['styles']);
  9. We're now ready to test our compilation process—go ahead and extract a copy of the contents of style – pre-compile.scss from the code download and save it to the src folder.
  10. At this point, we can run gulp from a Node.js command prompt. If this works okay, we should get a style.css file appear in the dest folder. If we open it up, we should see some compiled styles, as follows, that prove Neat is installed and working:
    @media only screen and (min-width: 30rem) {
      .wrapper {
        width: 95%;
        max-width: 72rem; }
      .col-1 {
        width: 8.33333%; }

At this point, we now have a working compilation process, and we're good to go with building a working site! For now, don't worry too much about the individual styles in the compiled test.css file, we will cover this in more detail over the next few pages. Let's put our new compilation process into practice and assemble a working example, so that we can see the grid facility in action.