Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Table-free CSS layouts in 10 minutes

DZone's Guide to

Table-free CSS layouts in 10 minutes

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

Before switching to CSS frameworks due to the nightmare of aligning columns and build a working layout, take a look at this examples.

All code is in the form of an .html file with style attributes, in order for the samples to be self-contained and didactic. Of course you can easily extract classes and IDs and use a real style sheet, embedded or linked to the page. Each block has a strong background color to be visible even in browsers without Firebug or a console for analyzing the DOM.

As always all the code is in a GitHub repository. Unfortunately GitHub does not serve files from the repository as text/html, but you can view the result just by cloning the repo on your local machine.

Horizontal division

The box model of CSS applied to some divs is all you need to setup horizontal divisions. I include this example for completeness, but note that it's orthogonal to other divisions and you can add horizontal divisions inside a vertical bar, and viceversa.
<div style="height: 100px; background-color: red;">
Header
</div>
<div style="min-height: 200px; background-color: lightblue;">
Content
</div>
The box model gives you complete control over heights and widths, as well as over margin, padding and borders of divs.

Vertical division: two columns, one fixed

The fixed column is removed from the normal flow of the page and floated to the left (or to the right, if you want). Note that floated content must appear before static content.
<div style="float: left; height: 400px; width: 100px; background-color: red;">
Menu
</div>
<div style="height: 600px; margin-left: 100px; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
Float also the second column if you want to add a third, and so on; however floated columns won't need the *margin* property anymore, as they will stack one on each other until the width of the container box is depleted.

Vertical division: two columns, percentual (liquid)

One column is floated, but the dimensions for margins and widths are expressed in relative proportions. Percentual values refer to the width of the containing element.
<div style="float: left; height: 400px; width: 20%; background-color: red;">
Navigation
</div>
<div style="height: 400px; width: 80%; margin-left: 20%; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
Note that in both vertical cases if you add borders, or margin, you'll have to subtract that from one of the widths or the second <div> will go under the first due to lack of horizontal space.

Footer after vertical division

Normally a footer large enough would simply go under everything.
<div style="float: left; height: 400px; width: 20%; background-color: red;">
Navigation
</div>
<div style="height: 400px; width: 80%; margin-left: 20%; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
<div style="height: 200px; background-color: lightgreen;">Footer<div>
However there is a problem with floating elements: they don't count as content, so their container will not expand to comprehend them. If it has a background, or other content, or even just a border like in this case, the result will be puzzling.
<div style="border: 1px solid black;">
<div style="float: left; height: 400px; width: 20%; background-color: red;">
Navigation
</div>
<div style="float: left; height: 400px; width: 80%; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
</div>
<div style="height: 200px; background-color: lightgreen;">Footer<div>

If we add an element with the clear property after the floats, we get the border to the right dimensions.

<div style="border: 1px solid black;">
<div style="float: left; height: 400px; width: 20%; background-color: red;">
Navigation
</div>
<div style="float: left; height: 400px; width: 80%; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
<br style="clear: both;" />
</div>
<div style="height: 200px; background-color: lightgreen;">Footer<div>

Another technique is called Float Nearly Everything: you float also the container, and what contains the container, and so on (floated elements expand to contain other floated elements). The advantage is that you don't need to add HTML code to manage the layout.

<div style="border: 1px solid black; float: left; width: 100%;">
<div style="float: left; height: 400px; width: 20%; background-color: red;">
Navigation
</div>
<div style="float: left; height: 400px; width: 80%; background-color: lightblue;">
Content
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
<p>Lorem ipsum dolor amet</p>
</div>
</div>
<div style="height: 200px; width:100%; background-color: lightgreen; float: left;">Footer<div>

The real problem when it comes to divide a layout into columns is the relative height of the columns, which can't influence each other. You can specify a fixed height for them or for the containing block element, or resort to tables (brrr). Unfortunately, most of the solutions for this case involve adding empty divs which only serve as a target for styles.

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}