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
Let's take a look at the base layout we now have.
<div id="hd"><!--- HEADER ---></div>
<div id="bd"><!--- BODY --->
<div id="yui-main"><!--- PRIMARY CONTAINER --->
<!--- CONTENT --->
<div class="yui-b"><!--- MENU ---></div>
<div id="ft"><!--- FOOTER ---></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?
|<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:
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?
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:
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:
- 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".
- I set the default font-size to 1em to override the default (smaller) pixel size.
- Any definitions specific to the original DIV ID values have to be updated the YUI ID values as stated earlier.
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.
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.
- Ben Forta has a series of Coldfusion 8 Ajax tutorials.
- Nathan Mische has an example of how to add context menus to the new AJAX version ColdFuison 8's cftree.