So this is a mini tutorial that will help you (I hope) to achieve the dreaded CSS thingy in just… 10 lines of CSS. And to make it more fun, I’ll use some techniques that will help you to achieve things that most people is afraid of: same height columns!
So, you can see the sample at here
The sample has a header, a 3 columns body and a footer. If you want more or less columns, simply add/delete the column as you want it (and be sure to change the width for each column). In 99% of cases, you won’t want anything more complicated like this.
Are you seeing it? Cool. The html code for the page (without all the lipsum stuff) is as simple as this:
let’s analyze this: first of all, there’s something that jumps to the view at first sight: COMMENTS! . Yes, they jump to the view because I wanted that and because that’s what comments are for: to help you visualize everything at first glimpse. USE COMMENTS, LOVE COMMENTS, EMBRACE COMMENTS. If you think the 2kb they may add to a page are a bandwidth problem, well… you’re in the wrong business.
Then you have the header (with h1 tag) the cols (named in a way you’ll recognize what they are at first sight) and a footer. You’ll also see some “strange” code, and styled HR, but don’t worry, I’ll go to it later.
Once you have your html, you need to start define elements. I’ll make it sweet and simple: using px instead of ems or percentage, this one uses a container with 960px width, side columns are half of what the main column width is. Here goes the CSS code:
Code:
<div style="text-align: left;" dir="ltr">* {margin:0px; padding:0px;}
body{background:#def5f9; font-family:verdana, helvetica, sans-serif; font-size:0.8em}
#container{position:relative; width:900px; background:#f9f9f9; border:1px solid #900; margin:auto auto; padding: 10px 5px; overflow:hidden; text-align:center; z-index:1;}
#header{width:890px; background:#333; margin:auto auto; padding:16px; margin:-16px; margin-bottom:10px;}
#header h1{color:#fff; text-align:left;}
#leftcol{width:240px; float:left; background:#f6d4d4; padding: 10px 5px; margin:0px 5px; padding-bottom:4000px; margin-bottom:-4000px; }
#midcol{width:360px; float:left; background:#a3f9d8; padding: 10px 5px; margin:0px 5px; padding-bottom:4000px; margin-bottom:-4000px; text-align:left;}
#midcol p{margin-bottom:10px;}
#rightcol{ width:240px; float:left; background:#fa0; padding: 10px 5px; overflow:hidden; margin:0px 5px; padding-bottom:4000px; margin-bottom:-4000px;}
#footer{position:relative; width:890px; background:#333; margin:auto auto; padding:16px; margin:-16px; margin-bottom:10px; height:100px; clear:both; z-index:20; margin-bottom:-20px; margin-top:30px; color:#fff;}</div>
let’s go line by line (or 2 at most)
Code:
<div style="text-align: left;" dir="ltr">* {margin:0px; padding:0px;}
body{background:#def5f9; font-family:verdana, helvetica, sans-serif; font-size:0.8em}</div>
Nothing strange here, just defining the colors and font and reseting all margins and paddings
Code:
<div style="text-align: left;" dir="ltr">#container{position:relative; width:900px; background:#f9f9f9; border:1px solid #900; margin:auto auto; padding: 10px 5px; overflow:hidden; text-align:center; z-index:1;}</div>
now, we add some flavor. Everything is quite straight, but we’ll use position:relative, z-index:1 and overflow:hidden. You won’t normally need them most of the times, but we need it for the 100% height columns and the footer.
Code:
<div style="text-align: left;" dir="ltr">#header{width:890px; background:#333; margin:auto auto; padding:16px; margin:-16px; margin-bottom:10px;}
#header h1{color:#fff; text-align:left;}</div>
The header uses another of my tricks: padding:16px; margin:-16px; This makes sure the width is 100% overriding any margin or padding I may add to the container box. Tricky but effective.
LET’S GO WITH THE COLUMNS!!!!
The columns are extremely simple in their layout, but you’ll see those 4000px values and will ask yourself WTF! . That’s another of my tricks. By applying a huge padding value to the column and then a huge negative margin, the overflow:hidden we used in the container makes sure all columns are 100% height. Neat, huh? Everything else is just styling (width, bg, color, whatever)
and finally, the footer:
Code:
<div style="text-align: left;" dir="ltr">#footer{position:relative; width:890px; background:#333; margin:auto auto; padding:16px; margin:-16px; margin-bottom:10px; height:100px; clear:both; z-index:20; margin-bottom:-20px; margin-top:30px; color:#fff;}</div>
Here, the overflow:hidden I used in the container box, makes its work again and avoids anything goes “out of the box”. The only special thing I had to keep in mind is to add a z-index higher than the container, or it would be below the columns.
Finally, the misterious HR thing: I use that to add a little space when going wild with positive/negative values and clearing all floats. Dirty, but lovely.
And that’s it! The “impossible to make” in just 10 lines of CSS code. Now you can add divs inside the existing divs or whatever, try new things, experiment and learn!