The Pragmatic Craftsman         :: Simplicity from complexity :: | About Me |

You are here: The Pragmatic Craftsman > Web Development

Go to: « 10 Ways to "Stay on Top" | Book Review: stylin' with CSS »

CSS Notes: Layouts

I'm reading an excellent book on CSS - Stylin' with CSS (highly recommended). I finished reading roughly half of it and I'd like to summarize some of the things I learned.

CSS Layouts
I am used to creating page layouts with HTML tables. It's easy to do and you can get any layout you want. But doing it that way is no longer recommended, and it's an ugly way of doing layouts. The "new" way of doing layouts is with pure CSS. It's cleaner and more concise. However, it's more complicated for a novice html guy like me. To really get this, you need to have some experience.

So how do you do it? Let's look an an actual example first.

<div id="main_wrapper">
<div id="header">
    <div id="header_inner">Header</div>
</div>
<div id="nav">
    <div id="nav_inner">Left nav</div>
</div>
<div id="content">
    <div id="content_inner">Main content...</div>
</div>
</div>

and here's the style for it:

<style>
#main_wrapper {
    width: 840px;
}
#header {
    
}
#nav {
    width: 20%;
    float: left;
}
#content {
    width: 80%;
    float: left;
}
#footer {
    clear: both;
}
</style>

Nice and simple. With this technique you can put together any layout you want.

Why the inner divs? Because CSS is weird. Even though you have a div with a certain size, by adding padding, margins, and border, the width actually changes! So, sure,you can go with just a top-level div, but you have to keep in mind that the size will change when you add those settings. How do you get around that? By using an inner div. The outer div controls the sizing and floating. Inside the inner div, you can then add padding, margins and border and the size of the outer div will not change: this is really how it should work. By using an inner div, you actually simplify your life!

Reference
Stylin' with CSS: A Designer's Guide, Charles Wyke-Smith


Comments

Post a comment









Remember personal info?







Go to:
   « previous entry: 10 Ways to "Stay on Top"
   » next entry: Book Review: stylin' with CSS

Random Quote

Search

 

Topics

Architecture & Design :12
Better Coder :30
Books :43
Books Recommended :18
Buzzwords :5
Career :25
Craftsmanship :15
Java :15
Quotes :25
Recommended :9
Software Engineering :3
Uncategorized :33
Web Development :1

Archive

May 2008 (1)
April 2008 (2)
March 2008 (1)
February 2008 (1)
January 2008 (2)
November 2007 (1)
October 2007 (3)
August 2007 (3)

...since January 2002

Currently Reading


:: See list of books I finished reading

Info

© 2001-2008 Stanley Kubasek About me :: Contact me