Throughout this chapter, we've explored using PostCSS to compile our media queries; while there are plenty of options open to us in terms of what we create, we should be mindful of what we create, to ensure that we're not creating a monster that slows our site down!
PostCSS has a couple of plugins available to help us here. They are:
postcss-mq-keyframes: Available at https://github.com/TCotton/postcss-mq-keyframes), this is a simple plugin that moves all keyframes out of existing queries, to the bottom of a style sheet. This allows us to rationalize our keyframe rules—in the event that we have multiple media queries, we can apply the same rule to each of these media queries.For example, the highlighted code below would be moved out of the query, and become a rule in its own right:
@media only screen and (min-width: 415px) {
.pace {
animation: pace-anim 5s;
}
@keyframes pace-anim {
100% {
opacity
css-mqpacker: Available at https://github.com/hail2u/node-css-mqpacker), this plugin parses our code and merges any identical rules into one media query rule. It is arguable how much benefit we are likely to get from this plugin; we will likely only see any significant benefit from using it on larger, more complex sites!Both plugins can be installed using the same process as all of the plugins we've used to date; it's worth noting that we should not overuse our queries. Instead of designing for specific platforms, try designing for instances where content clearly breaks and becomes unusable. The fewer queries we have, the easier it becomes to manage our code; simplicity is absolutely key to a successful site.
Looking further afield, there is one area we should not forget when optimizing our code—what browsers should we support? I'm all for pushing the proverbial browser boat out and using modern browsers where possible. However, some of you may still have to support older browsers (and please don't tell me that includes IE6!). Let's explore an option available in PostCSS to help those of you still having to support applications that really should be put out to pasture, so to speak.