Cast your mind back to Chapter 3, Nesting Rules, where we explored the concepts behind BEM, or the Block, Element, Modifier way of writing CSS. The key benefit of using this method is to help reduce CSS specificity, or where we might otherwise end up using something such as the following to style a simple button:
#maincontent .button .red.large:hover
Okay, it's a little contrived, but you get the idea: the level of specificity makes it awkward to manage and potentially reuse in future projects.
We took a look at BEM as a possible alternative—it has the benefit of reducing styles down to one or two classes, but can be awkward to remember which conventions to use:
.component {
/* represents a component */
}
.component__element {
/* represents a small part that is used to make a component */
}
'.component--modifier {
/* represents a state modifier for the component */
}Okay, so how can we get around this? Well, here's an option we can consider using: the CSStyle library. There are several reasons why this can help us—let's take a look in more detail.
The key behind CSStyle (available from http://csstyle.io/) is that it is made up of modular blocks, in a similar fashion to BEM. The difference, though, is that instead of having to remember a set of conventions that aren't the most intuitive, we can use a simpler set to create cleaner code.
The real beauty, though, is that we can use either SASS or PostCSS to create our site—we can begin with SASS, but we can also begin to transition over to using PostCSS with minimal changes. Let's put this into practice, and explore a quick demo to see how easy it is to make these changes—before we do so, take a look at http://codepen.io/alibby251/pen/pgmqjJ; this is a Pen that illustrates what we're going to create:

It won't win any style awards, but the purpose of this demo is to show you the process and not necessarily produce anything that is stunning! With that in mind, let's make a start:
T56 - using csstyle with sass folder from the code download that accompanies this book; save the folder to the root of our project area.src folder within T56 - using csstyle with sass to the src folder at the root of our project area.gulpfile.js and package.json files at the root of our project area with copies from within the T56 - using csstyle with sass folder.style.css file appear in the dest folder in our project area.dest folder back to the css folder within the T56 - using csstyle with sass folder.At this point, try previewing the results in a browser. If all is well, we will see the three buttons appear, just as they show in the Pen we mentioned at the start of this exercise.
All looks good…we have a working demo, with a compiled style sheet—but hold on…in SASS? Yes, if you look carefully, the demo was indeed set to use SASS, but with good reason: we're going to see how easy it is to change to using PostCSS without making material changes to our style sheet or our compilation process. Let's make a start:
gulpfile.js that is at the root of our project area, comment out line 5, and uncomment lines 6 and 7; this switches our task file from using SASS to PostCSS.[sass] task [style], on line 9.gulp.src call is looking for SASS files; change it to src/*.css..pipe(postcss([nested, csstyle]))—this removes the dependency on SASS and switches to using PostCSS.[sass] task; change [sass] to [style].var watcher = gulp.watch('src/*.css', ['style']);src folder at the root of our project area—rename the file as style.css.style.css, go ahead and remove the @import 'csstyle' line at the top of our style sheet.@include—remove all instances in our style sheet.That's it for our demo, sorry to disappoint if you were expecting more! All that remains is to replace the gulpfile.js and package.json files at the root of the project area with copies from the T57 –
using csstyle with postcss folder, and compile as normal.
Making the transition from SASS to PostCSS can be as easy or as complex as we make it. Using the CSStyle library can go a long way to easing the transition away from existing processors such as SASS.
Although our demo was just a quick whistle-stop tour through using CSStyle (and we will revisit it in Chapter 12, Mixing Preprocessors), it nevertheless illustrates a few important points of interest:
Ultimately though, the use of this library is about helping to ease the process of making the transition to using PostCSS. There are different ways to approach this—using CSSStyle means that we have to completely redesign our HTML, but can easily alter the processor with minimal fuss. The flip side to this is that we can use PostCSS plugins that mimic SASS coding standards, or create our own custom syntax—we will explore these concepts in the next two chapters.