About Me

nerd.of.steel: Rocco Augsuto

I am a Web Developer, Consultant, and Microsoft MVP (Most Valuable Professional) based out of the City of Roses - Portland, Oregon. I have over 15 years of education and experience in the art of Web Design and Development and have no plans to stop my quest for knowledge anytime soon. Over the past year I have been moving into the mobile Internet space and started developing applications for the Google Android platform.

Designing Your CSS with A Grid Overlay

Posted: Aug 31, 03:09 PM

A few weeks ago I was sitting around the table with a few of my friends discussing random tidbits about our jobs in the web development world. One of my friends who mostly does graphic design and a bit off CSS styling was complaining about the lack of a grid overlay for CSS programming like you could find available in Adobe’s Fireworks and Photoshop suites.

After sitting around and thinking about it for a second or two, I realized there was no reason why there could not be such a solution available. With a little bit of CSS and PNG magic, anyone working with CSS could have that nifty grid in no time at all!

Creating The Graphics

First thing that needed to be accomplished creating the actual graphic that would be used as our grid element. To do this, I opened up Adobe Fireworks and created a new document with the following settings:

  1. Width: 9px
  2. Height: 9px
  3. Resolution: 72px/in
  4. Canvas Color: Transparent

Figure 1
(Figure 1: New document set up screen in Adobe Fireworks 8)

On your shiny brand new blank canvas, select the rectangle tool and create an 11×11px square. Move this square over to the bottom left of your canvas so the top and right hand side of the square are floating off the edge of the canvas.

Figure 2
(Figure 2: After creating and positioning your new square, your canvas should look like this)

With your item selected, go to your properties panel and change your transparency to 20%. You should now be able to see your canvas through your square. Save this item out as a PNG with the name of "grid", we will need this later! Now you can close Fireworks. Congratulations, you’re done with the graphics portions of this tutorial!

Figure 3
(Figure 3: You can save your transparency as whatever you want, but for the sake of this tutorial we will save it out as 20%)

The Magnificent Code

Now that the easy part is done, we will tackle the code. To be honest, this part is pretty easy as well. For starters, I created a basic HTML page and added some lovely Lorim Ipsum content into it. Since this isn’t a tutorial about working with CSS, I am just going to post the basic HTML and CSS markup without explaining what it does. If you’re familiar with CSS, this should be a piece cake for you!

The HTML

<div id="wrapper">
<div id="head">
<h1>CSS Grid Test</h1>
</div>
<div id="content">
<h2>Test Content</h2>
<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer tempor ultricies sapien. Nunc eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean gravida lacus id ante. Morbi nunc orci, adipiscing et, convallis blandit, bibendum at, velit. Suspendisse libero enim, commodo id, luctus sed, hendrerit eu, lectus. Vivamus nonummy imperdiet quam. Morbi malesuada libero vel enim. Fusce tristique justo semper nisi. Aenean eget metus eu enim cursus tincidunt. Suspendisse vitae lacus nec neque dictum elementum. Cras arcu diam, vehicula sed, condimentum quis, blandit in, nulla. Aliquam erat volutpat. Nunc euismod consectetuer nisl. Nulla purus. Fusce nunc nisl, pretium vel, porttitor at, commodo a, lorem. Vestibulum sit amet quam. </p>
</div>
</div>

As you can see here, all the code consist of is a div container with an ID of "wrapper" to hold all of the content. Inside of the wrapper container you have two more containers, one with an ID of "head" for the header content and one with an ID of "content" the actual content.

The CSS

body {
margin:0;
padding:0;
height:100%;
}
#wrapper {
width: 495px;
padding:0 10px;
margin:0 auto;
position:relative;
border:1px solid black;
background:#FFFF99;
}
#content {
border:1px solid green;
}

The CSS is as basic as the HTML. Not much to see here really. All we are doing is centering the container named wrapper so that it lies in the middle of the page and adding an ugly yellow background to it. Around the content container we are adding an equally ugly green border to help the content stand out a little. I have included a screenshot below so you can see the loveliness of the current page! You can also check out this example page.

Figure 4
(Figure 4: The content in all of its glory!)

Now that we have the basic page setup we can create the fun part — The grid. This is as simple as creating the graphics. What we are doing is wrapping the wrapper container in a div with a class called "grid", like below. The bolded areas are the areas where changes in the code took place.

Grid HTML

<div class="grid">
<div id="wrapper">
<div id="head">
<h1>CSS Grid Test</h1>
</div>
<div id="content">
<h2>Test Content</h2>
</div>
</div>
</div>

The CSS is really were the real changes take place. Everything here is pretty basic. If you look at all the bolded elements you will see that I gave the body a z-index of 0 (zero) and I gave #wrapper a z-index of -1 (negative one). I then created a class named grid and gave it a z-index of 1000 (one thousand) as well as taking the image we made previously and making that a repeating background image.

The CSS

body {
margin:0;
padding:0;
z-index:0;
position: relative;

height:100%;
}
.grid{
z-index :1000;
position: relative;
background: url(images/grid.png) repeat;
margin:0;
padding:0;
height:100%;
}

#wrapper {
width: 495px;
padding:0 10px;
margin:0 auto;
border:1px solid black;
z-index:-1; position:relative;
background:#FFFF99;
}
#content {
border:1px solid green;
}

Figure 5
(Figure 5: The finished product! A nifty page with a nice grid overlay)

Internet Explorer 7 Support

For those of you using Microsoft’s Internet Explorer 7, it is possible to use this method to have IE7 display the grid over your your content. I have used a quick method to accomplish this by using conditional comments. Once you have your conditional comments, you really only have to change one line of code and you can have Internet Explorer 7 up and running in no time!

<!—[if IE 7]>
<style type="text/css">
.grid{ position:static; }
</style>
<![endif]—>

Voila! You now have a grid overlay on top of the page you are designing! You can view the finished work by checking out example page 2. I know, I know, fancy! Now if you prefer larger images for your fancy new grid, have no fear! I already went through the trouble of creating various sized PNGs that are ready to be uploaded and used. The included grid sizes are 10px, 20px, 25px, 30px, 40px and 50px. You can download them, along with this tutorial, from here. or from the link below.

DOWNLOAD TUTORIAL

8 Responses to "Designing Your CSS with A Grid Overlay"

  1. kng said:

    Neat would. How about creating a bookmarklet version of this to enable grids for any page?

    Syncotype-Alt might provide some inspiration.

  2. martinsc said:

    Awesome!
    but like kng said, a bookmarklet version of this would be very very usefull and I think would help a lot!

  3. Rocco said:

    From what I’ve read so far, it looks like something that could be accomplished. I’ll see what I can do!

  4. fluminis said:

    thanks for the nice tutorial.
    (This seems not working on IE7)

  5. Sam said:

    Doesn’t work for me in IE7

  6. Rocco said:

    I have added IE7 support! Enjoy! :)

  7. 3kolone said:

    great article!

  8. Darren said:

    I think the grid is a tried and tested method. It can be used to create great websites that are understandable to use and navigate. I mentioned the importance of utilizing grids in my article 5 Simple Design Guidelines

    Thanks for validating my opinion.