CSS grids can improve performance? How so?
The Importance of Page Weight
The weightier your page the slower the user experience. There are a few notable ways you can ease this correlation, but for the most part keeping your pages snappy is about being absolutely relentless when reducing and optimizing code. CSS is no exception.
On the other hand, a blank white page with unstyled black text and blue links would be very fast — but no one would care to visit. When we accept that we want sites which are both graphically interesting and fast we can begin to find ways to achieve what I think of as a one to many relationship between the amount of CSS we write and the potential layouts we can achieve.
The cure for bloat
Finding common denominators in our site will allow us to standardize the way we group related content, and the classes we use to style that content. You can think of these common denominators as the semantic building blocks of a high performance website. On a basic level that means that most sites have a particular way of displaying, for instance, a product. Perhaps with an image of the product to the left and a description of the product to the right. If that configuration appears throughout the site it should not be rewritten each time or we’ll have a 1:1 correlation between the size of the CSS and the complexity or number of pages in the site. These are the kind of sites that might start off fast but over the course of their lifetime become slower and slower. Once clean CSS becomes bloated with unnecessary recoding of semantically and visually identical elements.
Where do grids fit into the mix?
Grids are one of the simplest examples (to see, not to code) of patterns that can be abstracted across a site. The same basic three unit pattern which divides grouped content into three semantically linked chunks can be seen inside the main column content and also inside a module, defining different groups of content. In the next two pictures, I’ve outlined in orange the same grid being used two ways. Not coding this again and again is a huge performance win when multiplied over all the components and pages of a large site.
Image 1: Grids can be used to define the structure of the main column of a page.
Image 2: The same grids being used to break up the content within the body of a module.
It’s easy to see each piece of your site as a one-off, unique, something which will never vary — but performance dictates that we take a more sage approach. We need to abstract objects, the simple building blocks of your pages. The objects can then be combined to create pages and the pages combined to create entire sites.
Do not reinvent the wheel, grasshopper (at least not for a major client)
You may be tempted to write your own grids framework rather than use one that is already out there. The not invented here mentality is really hard to get around. If you really can’t resist the urge to write your own grids, do it for your blog, not a major international company. It’s a fun exercise, but the pitfalls are treacherous.
The Requirements & Constraints
- Grids and units can be nested inside each other to achieve complex layouts. The logic is very simple; any grid can be nested inside any other. Keeping constraints low simplifies CMS development. You should not need to write any additional CSS to make this possible or you start getting n! css rules. Yes, this is bad.
- Each unit controls itâ€™s own destiny, uh, width that is. In CMS design, one of the most costly operations is when you need to make changes elsewhere in the DOM in order to generate display or behavior in the current location. Keep all the classes necessary for the unit to function on the unit itself. This will make page building much quicker and code easier to navigate.
- Unit width can be any fraction of the total width. Generally I create fractions up to fifths, more than that is probably overkill.
- Fewer templates. Templates are unique starting points for building pages. They complicate using a CMS because generally they arenâ€™t designed so that you can go from one to another easily. Developers love to create new templates, while CMS users hate them. Try to refrain.
- No JS required. Using JS for basic layout is wasteful. We need JS to do other important things, thus we need to solve layout problems with CSS.
Redefining the template
Traditionally, a new template is created for each page type. Separate templates might be defined for one column, two column, and three column layouts, as well as home pages, main product pages, etc. Grids allow you to base all of these pages on the same template, which might include only the basic frame, header, body and footer. The template stays simple because grids allow you to break up any one of these regions into convenient units of content. You should then include a â€œsave as a templateâ€ option. Your users can then define templates as they build pages in your CMS, saving at convenient points from which they can build pages. Having a common starting point, or one single base template will mean that users can undo any choice theyâ€™ve made.
More about templates in another article.
So now that you know a bit more about how to choose a grids solution, go check out these frameworks to find the one that will work for you:
|nest||Size (kb)||fixed or liquid||columns||units||License||gutter|
|960s||No||5.4||fixed||12 & 16||40 & 60px||GPL||20px|
|YUI2||No||2.8||liquid||1 to 3||1/2 1/3||BSD||1-4%|
|OOCSS||Yes||0.7||liquid||1 to 5||1/2 1/3 1/4 1/5||BSD||0px|
|YUI3||Yes||1.5||liquid||1 to 24||1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/12 1/24||BSD||10px*|
* YUI3 requires an extra sub-node wrapper for the gutter.
Any other grids frameworks I should have included?
Note: I wrote most of this article years ago, but never got around to publishing it because it wasn’t “perfect”. (Oh how annoying it can be to be me). If you notice anything out-of-date, please do bring it to my attention.