Skip to content

Instantly share code, notes, and snippets.

@futuraprime
Created August 11, 2010 15:38
Show Gist options
  • Save futuraprime/519174 to your computer and use it in GitHub Desktop.
Save futuraprime/519174 to your computer and use it in GitHub Desktop.
A minimal solution to the "Holy Grail" CSS layout
<!DOCTYPE html>
<html>
<head>
<title>The Holy Grail - 3 Column CSS layout</title>
<style type="text/css" media="screen">
/* #content is the bounding box. It must have position: relative to anchor it for the background
* columns, and it must have overflow: hidden so it expands to contain floated content within it
* (you could also use a clear div below the columns, if you want to avoid hiding the overflow) */
#content { width:960px; overflow: hidden; position: relative; zoom: 1;}
/* each content column is floated left */
.column, .shadow-column { float: left; padding: 10px; }
/* each shadow column is given absolute position, a z-index of -1 (so it runs behind the content
* columns) and is anchored to top and bottom, which forces them to adopt the full height of the
* nearest position:relative container */
.shadow-column { position: absolute; z-index: -1; top: 0; bottom: 0; left: 0; zoom: 1;}
/* we set the widths of the columns and their shadow counterparts, and the backgrounds. there is
* no real reason to give background color to the content columns, other than to be sure they do
* not have a different color from other CSS. */
#col1, #shadow-col1 { width: 140px; background-color: #faa; }
#col2, #shadow-col2 { width: 540px; background-color: #afa; }
#col3, #shadow-col3 { width: 220px; background-color: #aaf; }
/* finally, we offset the shadow columns so they line up with their corresponding content column
* and presto! holy grail layout accomplished */
#shadow-col2 { margin-left: 160px; }
#shadow-col3 { margin-left: 720px; }
/* this technique works equally well with liquid layouts - just substitute percentages for pixel
* values in the margin and width calls and you'll have a liquid layout */
</style>
<!--[if lte IE 6]>
<style type="text/css" media="screen">
/* if you absolutely, positively MUST have this work in older versions of IE, here's the fix */
.shadow-column { height: expression(this.parentNode.clientHeight + "px"); }
</style>
<![endif]-->
</head>
<body>
<div id="content">
<div id="shadow-col1" class="shadow-column"></div>
<div id="shadow-col2" class="shadow-column"></div>
<div id="shadow-col3" class="shadow-column"></div>
<div id="col1" class="column">
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.
</div>
<div id="col2" class="column">
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.
</div>
<div id="col3" class="column">
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.
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment