Single Image CSS Layouts

Table of Contents


Explaination and Introduction

[ Top ]

A single image CSS layout (for lack of a better term) is a layout that calls upon and uses a single image containing multiple images, instead of individually calling upon several images. By using a single combo image (again, for lack of a better term), your website layout will be more efficient. See the pros and cons.


I originally wrote this article because I wanted to offer a complete guide on how to use single images for CSS layouts, how to do it most efficiently, and why it works that way. Although the process explained is simple, very few people actually use it. I've tried to put everything into simplified terms to make it easy for anyone to grasp.



Advantages and Disadvantages

[ Top ]
Advantages:
Disadvantages:

Getting Started

[ Top ]

You generally want to compile all your layout images into a single image as the last step towards finishing your layout. So I've created a layout that uses five images called via css, so that I can demonstrate step-by-step how to do it.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>website.title</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
html, body {
margin : 0px;
padding : 0px;
font-family : Verdana, Arial;
font-size : 12px;
background : #f2f2f2;
}
.body {
background : #ffffff;
margin-left : 10%;
margin-right : 10%;
border : 1px solid #c0c0c0;
}
.nav {
color : #000000;
padding-top : 6px;
padding-bottom : 6px;
padding-left : 4px;
padding-right : 4px;
font-size : 11px;
border-bottom : 1px solid #c0c0c0;
background-color: #f9f9f9;
margin-bottom: 2px;
}
a.nav-link:link {
color : #000000;
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
}
a.nav-link:visited {
color : #000000;
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
}
a.nav-link:hover {
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
background : #ffffff;
padding-top : 6px;
padding-bottom : 6px;
color : #85add6;
border-bottom : 1px solid #6d92b9;
}
.content {
padding-left : 4px;
padding-right : 4px;
padding-bottom : 4px;
padding-top : 2px;
}
.edge-top-right {
background : url(images/etr.png);
height : 5px;
width : 5px;
float : right;
margin-right : -1px;
margin-top : -1px;
}
.edge-top-left {
background : url(images/etl.png);
height : 5px;
width : 5px;
float : left;
margin-left : -1px;
margin-top : -1px;
}
.edge-bottom-right {
background : url(images/ebr.png);
height : 5px;
width : 5px;
float : right;
margin-right : -1px;
margin-top : -4px;
}
.edge-bottom-left {
background : url(images/ebl.png);
height : 5px;
width : 5px;
float : left;
margin-left : -1px;
margin-top : -4px;
}
.title {
font-family : Arial;
font-size : 22px;
color : #6d92b9;
}
.header {
font-family : Arial;
font-size : 16px;
color : #a5a5a5;
}
.footer {
text-align : right;
font-size : 11px;
margin-top : 4px;
padding-right : 5px;
height : 17px;
background : url(images/b.png);
}
a:link, a:visited {
color : #6d92b9;
}
a:hover {
color : #85add6;
text-decoration : none;
}
.body span {
margin-left : 16px;
line-height : 1.5em;
}
</style>
</head>
<body>

<div class="body">
<div class="edge-top-left"><!-- --></div>
<div class="edge-top-right"><!-- --></div>
<div class="nav">
<a href="/welcome" class="nav-link">Home</a>
<a href="/projects" class="nav-link">Projects</a>
<a href="/tutorials" class="nav-link">Tutorials</a>
<a href="/articles" class="nav-link">Articles</a>
<a href="board/" class="nav-link">Community</a>
</div>
<div class="content">
<div class="title">website.title</div>
<div class="header">header</div>
<span>Nunc nobis videntur parum clari. Parum claram anteposuerit litterarum formas. Mutationem consuetudium lectorum Mirum est notare quam littera gothica quam nunc putamus parum claram anteposuerit.</span>
</div>

<div class="footer">Copyright © 2006 <a href="http://www.quate.net/" target="_blank">Quate</a> Development.</div>
<div class="edge-bottom-left"><!-- --></div>
<div class="edge-bottom-right"><!-- --></div>
</div>

</body>
</html>

The above code should look like this in Mozilla Firefox. It will look slightly different in IE due to IE's rendering issues.



Step 1. Combine the images.

[ Top ]

The layout uses the following five images:



145B

131B

135B

131B

130B



Each image is between 130 and 145 bytes, for a total of 672 bytes. If we were to combine these images into one image using a simplistic image editor (Microsoft's Paint works just fine), you might end up with one of these:



274B

273B

274B

254B

257B

261B

274B




You should have noticed a few things from the above images:


We'll keep the image with the smallest file size (the fourth image above), and put it in the directory images, with the name layout.png. If one wished, they could reduce the file size even more by converting the image to png8 from png24. In some cases, making extra whitespace transparent may helps. (See the how to compress images with tools side note for more information about this.)



Step 2. Modifying the CSS code for the single image.

[ Top ]

CSS has a background property called background-position. With it, you can specify the starting point of the background image from two given points (x and y). Using it, the class edge-top-right from the layout would be like this:


.edge-top-right {
background-image : url(images/layout.png);
background-position : 0px -27px;
/* ... */
}

The above can also be written in shorthand syntax, like so:


.edge-top-right {
background : url(images/layout.png) 0px -27px;
/* ... */
}

The background-position of the class edge-top-right is 0px, 27px because if you were to open a image editor, you'd find the coordinates of the image for class edge-top-right starts at point (0, 27). When applying the image positioning, remember to replace the background image url with the new layout.png image url.



The complete source code.

[ Top ]

The following is the new source code of the layout, with the background-position attribute properly added to each of the classes using background images. The code below uses the shorthand syntax.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>website.title</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
html, body {
margin : 0px;
padding : 0px;
font-family : Verdana, Arial;
font-size : 12px;
background : #f2f2f2;
}
.body {
background : #ffffff;
margin-left : 10%;
margin-right : 10%;
border : 1px solid #c0c0c0;
}
.nav {
color : #000000;
padding-top : 6px;
padding-bottom : 6px;
padding-left : 4px;
padding-right : 4px;
font-size : 11px;
border-bottom : 1px solid #c0c0c0;
background-color: #f9f9f9;
margin-bottom: 2px;
}
a.nav-link:link {
color : #000000;
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
}
a.nav-link:visited {
color : #000000;
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
}
a.nav-link:hover {
text-decoration : none;
padding-right : 4px;
padding-left : 4px;
background : #ffffff;
padding-top : 6px;
padding-bottom : 6px;
color : #85add6;
border-bottom : 1px solid #6d92b9;
}
.content {
padding-left : 4px;
padding-right : 4px;
padding-bottom : 4px;
padding-top : 2px;
}
.edge-top-right {
background : url(images/layout.png) 0px -27px;
height : 5px;
width : 5px;
float : right;
margin-right : -1px;
margin-top : -1px;
}
.edge-top-left {
background : url(images/layout.png) 0px -32px;
height : 5px;
width : 5px;
float : left;
margin-left : -1px;
margin-top : -1px;
}
.edge-bottom-right {
background : url(images/layout.png) 0px -17px;
height : 5px;
width : 5px;
float : right;
margin-right : -1px;
margin-top : -4px;
}
.edge-bottom-left {
background : url(images/layout.png) 0px -22px;
height : 5px;
width : 5px;
float : left;
margin-left : -1px;
margin-top : -4px;
}
.title {
font-family : Arial;
font-size : 22px;
color : #6d92b9;
}
.header {
font-family : Arial;
font-size : 16px;
color : #a5a5a5;
}
.footer {
text-align : right;
font-size : 11px;
margin-top : 4px;
padding-right : 5px;
height : 17px;
background : url(images/layout.png) 0px 0px;
}
a:link, a:visited {
color : #6d92b9;
}
a:hover {
color : #85add6;
text-decoration : none;
}
.body span {
margin-left : 16px;
line-height : 1.5em;
}
</style>
</head>
<body>

<div class="body">
<div class="edge-top-left"><!-- --></div>
<div class="edge-top-right"><!-- --></div>
<div class="nav">
<a href="/welcome" class="nav-link">Home</a>
<a href="/projects" class="nav-link">Projects</a>
<a href="/tutorials" class="nav-link">Tutorials</a>
<a href="/articles" class="nav-link">Articles</a>
<a href="board/" class="nav-link">Community</a>
</div>
<div class="content">
<div class="title">website.title</div>
<div class="header">header</div>
<span>Nunc nobis videntur parum clari. Parum claram anteposuerit litterarum formas. Mutationem consuetudium lectorum Mirum est notare quam littera gothica quam nunc putamus parum claram anteposuerit.</span>
</div>

<div class="footer">Copyright © 2006 <a href="http://www.quate.net/" target="_blank">Quate</a> Development.</div>
<div class="edge-bottom-left"><!-- --></div>
<div class="edge-bottom-right"><!-- --></div>
</div>

</body>
</html>

When rendered, the above code looks exactly the same as the original, but will load slightly faster, and will require fewer server requests.



Side Notes

[ Top ]
How image combination works (in simple terms). [ Top ]
An image contains data about itself that identifies itself as an image. Therefore images A and B will have a larger file size than image C which contains images A and B, because image C only has the identification data about itself once.

How image compression works (in simple terms). [ Top ]
There is bits of data for each pixel in an image. A 10px by 10px image with a pure white background would normally have data for each pixel, except it can be compressed: when an image is saved, it will look for patterns, such as lines of just one color, so that when it saves, instead of saving each pixel individually, it can save data about just that line of color.
When creating your combo images think about image compression, as it may help you find the smallest image size possible. You might trigger a compression pattern by randomly changing the order of appearance of images in the combo image.

How to compress images with tools. [ Top ]
With exception to jpegs (and tiffs?), all images are compressions of themselves, as explained above. But to further compress them, you may need to convert images to a weaker, simpler image format. From a 24bit png, converting to a 8bit png, a gif or jpeg may help. To do this, you may need a better program like GIMP, Photoshop, or Snico Edit, where you can "Export" or "Save for Web As" to a different image type.










-----------------------------------------------
This space intentionally left blank.
-----------------------------------------------