Dynamic CSS layouts in 5 minutes
I released a plugin last week for Cacheer that allowed for quick and easy CSS layouts. I think much of its awesomeness was lost on people because they couldn't see the benefits. So I've written a quick tutorial to explain it. It only takes 5 minutes.
If you’ve read my previous post about my Grid Plugin for CSS Cacheer, you may still be wondering just how you can use it in practice. I won’t go through the installation, it’s pretty straight forward, I just want to show you what its capable of. You’ll be creating a usable layout, ready for design, in only 5 mins.
How does it work?
CSS Cacheer by Shaun Inman, is a script that intercepts requests made to CSS files on the server, processes the CSS, then caches it. This enables us to use constants, compressors and even a dynamic grid. The grid is a plugin I wrote that enables users to define a grid within your css. You'll never need a grid.css again. This is a tutorial to show you how to use it.
Essentially, you can create a layout then change it from within the CSS, so there's no extra markup needed like in Blueprint (eg span-1, span-2 etc).
The only thing you’ll need to get this running is a local web server. If you’re on a Mac I’d recommend MAMP, and if you’re on Windows, WAMP. Hopefully you know how to get a simple web server running. I’m not going to explain this part, so make sure you know how it works.
I built this in Safari, but it should work fine in any browser. The only thing that may change is the images in the bottom right box, they might not display as fillers like in Safari
Step 1 - Installation
To save you any trouble of getting it set up, and to move directly into the layout, I’ve prepared some files earlier for you to work from
Included in the work files is Shaun Inman's CSS Cacheer script, a CSS file, and my grid plugin already installed. This tutorial aims to show you what is possible with this plugin thanks to Shaun Inman's script. Make sure you pay him a visit.
Step 2 - Create your grid
Open master.css in the css directory, and add this code.
@grid {
column-count: 24;
gutter-width: 18;
grid-width: 966;
}
This is the parameters for your grid. It’s fairly self-explanatory. Column count is the number of columns you want to work with. Gutter width is the width between your columns, and grid width is the width of your page. You can also define your grid using ‘column-width’ and leaving out the grid width. So the grid is fairly flexible.
Step 3 - Add the Markup
Now open index.html and add the following code.
<div class="container"></div>
This container will wrap the page and hold the grid. It functions exactly the same as the Blueprint container. Now we need to add the actual grid elements. Add these divs inside the container:
<div id="header">
</div>
<div id="promo">
</div>
<div id="info-1">
</div>
<div id="info-2">
</div>
<div id="info-3">
</div>
Step 4 - Give them style
We’ll make a basic layout. It’s really easy to change this around, so it’s good for quick mock ups. I could make a grid mock up quick with CSS than with Photoshop.
Add this code to master.css:
.container div {
background: #eee;
margin-bottom: grid(gutter);
}
#header {
columns: 24;
height: 75px;
}
#promo {
columns: 24;
height: 300px;
}
#info-1 {
columns: 8;
height: 300px;
}
#info-2 {
columns: 8;
height: 300px;
}
#info-3 {
columns: 8;
height: 300px;
margin-right: 0;
}
You should get something that looks like this:
No floats or widths. It’s all done dynamically. So lets add some content. Remove all the divs inside your .container, and add this new code.
<div id="header">
<h1>Logo</h1>
</div>
<div id="promo">
<img height="100%" width="100%" />
</div>
<div id="info-1">
<h2>Subheading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div id="info-2">
<h3>Little heading</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
<h3>Another heading</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div id="info-3">
<img height="80" width="100%" />
<img height="80" width="100%" />
<img height="80" width="100%" />
</div>
Before we look at this in a browser, lets add some quick styles. I’ve already created some basic typography. So just add this code to the top of your master.css:
@server import url(base.css);
Now have a look at it. It should look something like this:
Obviously, the grid segments don’t have any padding. Looks a little bit stupid. So lets do a couple of things to tidy it up. Firstly, lets add some padding and borders to some of the blocks.
Change #info-1 to:
#info-1 {
columns: 8;
height: 300px;
border-right: 2px solid #eee;
padding: 0 18px 0 0;
}
A couple of things should be noted. The columns property must always come first in your list of properties. Notice that I used shorthand for the padding. This is the best way to ensure your padding is calculated. You could run into problems if your padding is declared in other selectors, as this won’t be factored in. So this way is your best bet. Also, with borders, you can declare the standard border property, but you must specify the border-right and border-left if you want the border calculated as well.
You won’t have any problems if your CSS is well-formed.
Now change #info-3 to:
#info-3 {
columns: 8;
height: 280px;
padding: 10px;
margin-right: 0;
}
There’s not much to note here, except that I’ve given it a 0 margin on the right. This is because it is the last block in a row of columns. You need to always specify this for a column on the end, otherwise it will drop down.
We’ve kept the background colour on the divs up till now so that we can see the grid elements. You can go ahead and remove this.
So now we’ve created a working layout in a couple of minutes. But lets see just how flexible it is. The client now thinks the page is too wide, as the majority of people viewing their site are still on 800 x 600 (it could happen), so now we have to resize our whole layout! No we don’t. Simply change grid-width to 770. You can even go ahead and change the height of the promo div. So now we have something that looks like this:
So we created a fully working css layout in around 5 mins. You could get this layout working with only 1 line of code for each div if you wanted to. eg #info-1 {columns:8;}. So you could even get this layout to around 10 or so lines.
I'm looking at adding even more features in the near future. So stayed tuned. You can download Shaun Inman's CSS Cacheer from his blog, and you can download and stay updated with the grid plugin over at my previous post.
Leave a Comment
This form has simple XHTML enabled, so go for it. It also uses Akismet to prevent spam.
Your email will never be displayed, and is used for verification. This form is also Gravatar Enabled.


13 Comments
Pablo Impallari
Website
Looks nice. Will try it.
Neil MacLean
Website
Anthony, this is really, really good. I have enjoyed playing with it for the last few minutes and I am beginning to grasp the possibilities. Plenty to explore.
Many thanks for sharing.
Anthony Short
Thanks Neil.
There’s still a bunch I want to do with the script. But it’s definitely going to make layouts much quicker to create. Especially if you KNOW they are going to work in IE. (Next on my to-do).
Eystein Mack Alnæs
Website
Color me impressed!
I knew there was something good with Cacheer, but wouldn’t have known exactly where to begin with it. I’ve put together your 5-minute page here, and it works fine in IE6 and 7, while IE5.5 has the typical box-model issues. But who cares?!
I’m gonna keep playing with and hopefully be able to use it for a live site. Are there any issues I should be aware of?
BTW - if this is the kind of stuff you learn at uni now I’m truly envious.
Anthony Short
The only major issue with it at the moment is the way it calculates border and padding. You need to be precise in the way you do it.
It will only use the padding and border you specify in the element with the column property. So no inheritance just yet.
Apart from that, you shouldn’t have any issues, as long as you know what it’s actually doing and what classes its putting in there. Not too tricky.
p.s Uni is still teaching 3rd years how to make ‘basic html pages’ with tables. I’m not expecting miracles to happen in that place, its all about the piece of paper. They don’t get anywhere near teaching this much about web or design :)
MIchaelT
Website
Hi Anthony,
Not so much about the CSS rather about the write–up. This was clear, coherent and easy to follow along. Great!
“I’m not expecting miracles to happen in that place, its all about the piece of paper. “ – a healthy attitude I say.
I hope to see more articles.
– MichaelT
Neil MacLean
Website
Anthony, would you advise setting border and padding for every element then to avoid surprises?
The inheritance issue is an interesting - and potentially frustrating - one. It took me a while to realise the gap below an image was due to the initial line-height setting.
I laughed at your comment in the base.css “Simple reset, can’t be bothered doing a good one”
But seriously - would there be any point in something more substantial by way of a reset?
Just wondering.
And still having fun…
Anthony Short
@Neil - It would probably be best to set the border and padding for the element which contains the columns property. For now, the only way it knows it should even calculate the padding & border into the width is via the column property. I can see how it would be a little frustrating. It’s not terribly bad, but its annoying.
I’m working on a new way to process the properties of elements, and I’ll probably do it over the weekend.
The issue you were having with the images was in the base.css, where I gave it a margin-bottom: grid(gutter) for the sake of the tutorial. Maybe I should have just include that :)
For resets, I usually use a variation of Eric Meyers reset This gives a complete reset, and I’ve never had a problem with it. The * {margin:0;padding:0;} method can apparently bog down a browser when you have a fairly complex site.
Glad you’re enjoying it :)
Neil MacLean
Website
Thanks very much Anthony
This will just show-up my ignorance of php but…
I had a version of this working well at a test address but when I moved the same files to another site (different url, otherwise same set up on the same shared server) which already had wordpress installed, it seemed to lose the ability to create a grid. I’ve retried it several times. Very odd. The background colours I set in the master css were there but no grid.
Anthony Short
I actually had someone emailing me with a similar problem with textpattern. It’s not actually processing the CSS at all due to Wordpress rewriting the URL. (I’m guesing)
I just did a quick WP install and put the test files in there. I linked to master.css file using the full path eg. “/wp-content/themes/default/css/master.css” and it worked fine. I was able to create a grid.
You also need to make sure you’ve got a container. I’m guessing you do though :)
Neil MacLean
Website
Yes, I had the full path in there - and in fact it was taking the background colours from master.css so it had found it ok
I must be doing something else wrong…
Pwhndvve
Website
Thousands and legate left <a href=http://bebo.com/CytotecB9>buy cytotec</a> meat steamed held.
Niels
Website
This is a valuable piece of work, Anthony. It is an element not to be missed when using Shaun’s Cacheer.