My company recently launched a new version of our site. The original CSS layout has some issues and YUI Grids saved the day in no time flat. Using Ray Camden's BlogCFC, this is an example of how quickly you can change your site's layout with YUI Grids.

What is YUI Grids?

From the Yahoo UI website:

The foundational YUI Grids CSS file offers three preset page widths, seven core templates, and the ability to nest subdivided regions of one to four columns. All told, the file offers almost 200 preset layouts and an unlimited number of custom permutations that work across all A-grade browsers, all for less than 2.5kb of minimized file weight.

My company recently released a new version of our site. Not only did we overhaul the entire site using Object Oriented Programming with Coldfusion, we implemented a CSS based layout. We went through more than a few versions of our CSS files as the project evolved.

Just as we had ironed out most of the cross-browser issues between Firefox and MSIE 6, users began automatically updating to MSIE 7 and things got worse.

We were a week away from a Production launch. We needed a solution and we needed it fast.

That's when I found YUI Grids.

Get the files

Start with the default installtion of Ray Camden's BlogCFC. This example is using version 5.8.001.

Next, download the latest version of YUI. Place the /yui/build/ folder in the root of your blog installation. You probably shouldn't put the /docs or /examples folders on a production site.

The basic 2 column layout

The first file we're going to alter is /tags/layout.cfm

/tags/layout.cfm

Add the YUI Grids CSS file:

view plain print about
1<link rel="stylesheet" href="#application.rooturl#/yui/build/grids/grids-min.css" type="text/css" />
2<link rel="stylesheet" href="#application.rooturl#/includes/layout.css" type="text/css" />
3<link rel="stylesheet" href="#application.rooturl#/includes/style.css" type="text/css" />

Now update DIV IDs and orders.

Original layout
view plain print about
1<div id="page">
2 <div id="banner"><a href="#application.rootURL#">#htmlEditFormat(application.blog.getProperty("blogTitle"))#</a></div>
3    <div id="content">
4    
5        <div id="blogText">
6</cfoutput>        
7<cfelse>
8<cfoutput>
9        </div>
10    </div>
11    <div id="menu">
12        </cfoutput>
13        
14        <cfinclude template="getpods.cfm">
15        
16        <cfoutput>
17        </div>        
18<div class="footerHeader"> <a href="http://ray.camdenfamily.com/projects/blogcfc">BlogCFC</a> was created by <a href="http://ray.camdenfamily.com/">Raymond Camden</a>. This blog is running version #application.blog.getVersion()#. </div>
19    </div>
YUI layout
view plain print about
1<div id="doc" class="yui-t4">
2    <div id="hd"><a href="#application.rootURL#">#htmlEditFormat(application.blog.getProperty("blogTitle"))#</a></div>
3    <div id="bd">
4        <div id="yui-main">
5            <div class="yui-b">
6                <div class="yui-g">
7</cfoutput>
8<cfelse>
9<cfoutput>
10                </div>
11            </div>
12        </div>
13        <div class="yui-b">
14            <cfinclude template="getpods.cfm">
15        </div>
16    </div>
17    <div id="ft"> <a href="http://ray.camdenfamily.com/projects/blogcfc">BlogCFC</a> was created by <a href="http://ray.camdenfamily.com/">Raymond Camden</a>. This blog is running version #application.blog.getVersion()#. </div>
18</div>

Let's take a look at the base layout we now have.

view plain print about
1<div id="doc" class="yui-t1">
2    <div id="hd"><!--- HEADER ---></div>
3    <div id="bd"><!--- BODY --->
4        <div id="yui-main"><!--- PRIMARY CONTAINER --->
5            <div class="yui-b">
6                <div class="yui-g">
7                <!--- CONTENT --->
8                </div>
9            </div>
10        </div>
11        <div class="yui-b"><!--- MENU ---></div>
12    </div>
13    <div id="ft"><!--- FOOTER ---></div>
14</div>

There's a header, a body and a footer. The body has a container for content and a container for a menu. That's pretty much all we need. Short and sweet.

So what changed?

Original YUI
<div id="page"> <div id="doc" class="yui-t4">
<div id="banner"> <div id="hd">
<div id="content"> <div id="bd">
<div id="blogText"> <div id="yui-main">
<div id="menu"> <div class="yui-b">
<div class="footerHeader"> <div id="ft">

The only "big" change was that the side menu DIV was moved inside of the body DIV (id="bd"), placed after the main DIV (id="yui-main").

In the CSS files (layout.css and styles.css), you'll have to replace any reference to the original IDs with the YUI IDs. For instance:

/includes/layout.css
view plain print about
1/*
2#page{
3    width:90%;
4    min-width: 800px;
5    margin: 0 auto 0 auto;
6    padding: 0 0 10px 0;
7}
8*/

9#doc{
10    width:90%;
11    min-width: 800px;
12    margin: 0 auto 0 auto;
13    padding: 0 0 10px 0;
14}

Some styles may no longer be used. I'll elaborate on that later.

Scratch that, reverse it

With just those few lines of code, we now have the default BlogCFC layout in YUI Grids: left-hand content, right-hand menu.

What if we want to swap them? Place the menu on the left and the content on the right? Take a guess at how much code we have to change?

/tags/layout.cfm
Right-hand menu
view plain print about
1<div id="doc" class="yui-t4">
Left-hand menu
view plain print about
1<div id="doc" class="yui-t1">

One class name! How cool is that?

By changing the value of the ID and CLASS in the root DIV, you can implement static or liquid pages, content and navigation menus. By altering the DIV attributes in the main content DIV (id="yui-main") you can add multiple columns of various widths to your layout. Check out the cheatsheet and the YUI Grids Builder on the YUI site for more.

Optimizing the CSS files

Grids.css takes care of many cross-browser issues by managing margins and padding. Notice when I added the YUI Grids CSS file, I used grids-min.css instead of grids.css. The "-min" file is optimized to reduce whitespace.

Open up the default layout.css file and you'll see just a few lines of CSS. It's very good and works well cross-browser, but with grids.css, most of it isn't needed anymore. I've reduced this file down to the following:

/includes/layout.css
view plain print about
1/*Layout*/
2#doc.yui-t1 #yui-main { padding-right: 8px; }
3#doc.yui-t4 #yui-main { padding-left: 8px; }

These just pad the main content area away from the edge of the display area depending on which side they're occupying.

There are only a few changes I felt were needed in the default styles.css file:

  1. The grids.css file sets the default text alignment to "center". Why, I have no idea, but it will mess up your debug output. It gets set back to "left".
  2. I set the default font-size to 1em to override the default (smaller) pixel size.
  3. Any definitions specific to the original DIV ID values have to be updated the YUI ID values as stated earlier.

/includes/styles.css
view plain print about
1/*
2body {
3    background-color: #c0c0c0;
4}
5#page{
6    background-color: #FFF;
7}
8#banner, #banner a {
9    font-family: "Trebuchet MS", verdana,arial, sans-serif;
10    font-size: 1.4em;
11    font-weight: bold;
12    color: white;
13    background-color: #FFCC00;    
14    padding:5px;
15}
16*/

17body { background-color: #c0c0c0; font-size: 1em; text-align: left; }
18#doc{ background-color: #FFF; }
19#hd, #hd a {
20    font-family: "Trebuchet MS", verdana,arial, sans-serif;
21    font-size: 1.4em;
22    font-weight: bold;
23    color: white;
24    background-color: #FFCC00;    
25    padding:5px;
26}

The final change I made to the default styles.css file is to bump up the text in the main content area. Maybe it's because my screen resolution is 1600x1200, but 0.7em has always seemed very hard to read.

/includes/styles.css
view plain print about
1.body {
2    font-family: "Trebuchet MS", verdana, arial, sans-serif;
3    /* font-size: .7em; */
4    font-size: .8em;
5    color: #000000;
6    margin-left: 0px;
7}

The Return on Investment (ROI)

Working with YUI Grids for 45 minutes, I had the new Unitrin Specialty site's layout templates converted. Along with that, we implemented an entirely new design. Afterwards, it was a matter of tweaking the application-specific CSS files.

We put the new design into QA, then into Beta and the users loved it.

Had we started with YUI Grids, we could have saved ourselves a lot of time and headaches.

The future of Coldfusion and YUI

One unexpected benefit is that many of the new Ajax features in Coldfusion 8 implement YUI. I'll update a list of examples as I find them. If you want to try them out for yourself, download the Coldfusion 8 Beta now.