Flexible floated two column CSS layout
There are many tutorials about how to make two column CSS layouts, and you have maybe seen one that looks similar. Anyway, here is a solution which is quite flexible when it comes to switching between four different placements of the menu; left, right, top and above the footer. Do you also want a design with 100% height you said? It's prepared for that also.
Furthermore, the content comes first in the HTML code (after the header, before the menu/columns) which may benefit search engines and screen/speech readers.
The different layouts are made without absolute positioning, only changes in floating and margins are done.
Our HTML
This is our HTML that we are going to make four total different layouts with.
- <div id="mainContainer">
- <div id="header"><p>My header</p></div>
<div id="content">- <p>My two column CSS content.</p>
- </div>
- <div id="column"><p>My column</p></div>
- </div> <!-- Closing mainContainer -->
- <div id="footer"><p>My footer text</p></div>
Our CSS for a two column layout with column aligned right
This is the CSS that is our basis for creating the different layouts. In this example the column is going to be right aligned. Later CSS codes in this article only shows differences from our "base" CSS.
* /* Our "universal rule" */- {
- padding: 0;
- margin: 0;
- border: 0;
- }
- body { text-align: center;}
- #mainContainer {
- width: 760px;
- margin: 0 auto; /* We are going to center this design */
- text-align: left; /* Since we used text-align: center in the body to be sure some older IE versions are going to center our design, we now align the text left */
- }
- #header {
- background: #000;
- color: #fff;
- }
- #content {
- float: left; /* Our content is going to be left aligned */
- width: 500px; /* And it's going to have a 500px width */
- background: #D9D9D9;
- }
- #column {
- float: right; /* Our column is going to be right aligned */
- width: 260px; /* Our total width - content width is 260px */
- background: #8A8AE6;
- }
- #footer {
- clear: both; /* We have to clear our floats */
- width: 760px; /* We need to set the width on the footer since it's outside the mainContainer, and therefor isn't controlled by it */
- margin: 0 auto; /* We need to center the footer also */
- background: yellow;
- }
Our CSS for a two column layout with column aligned left
This is the CSS for our left aligned column. Differences from the "base" CSS are bolded.
- #content {
- float: right; /* Our content is going to be right aligned. */
- width: 500px; /* And it's going to have a 500px width */
- background: #D9D9D9;
- }
- #column {
- float: left; /* Our column is going to be left aligned. */
- width: 260px; /* Our total width - content width is 260px */
- background: #8A8AE6;
- }
Our CSS for a one column layout with top/header menu/text
This is the CSS for one column design with a top/header menu (or text as in our example). Differences from the "base" CSS are bolded.
- #content {
- float: left; /* Just float it left */
- width: 760px; /* The same width as the mainContainer */
- margin-top: 40px; /* We add some space for our 40px high top menu */
- background: #D9D9D9;
- }
- #column {
- float: left; /* Just float it left */
- width: 760px; /* The same width as the mainContainer */
- margin-left: -760px; /* Now we move the column 760px (the same amount as the mainContainer width) to the left */
- background: #8A8AE6;
- }
Our CSS for a one column layout with footer menu
This is the CSS for one column design with a top/header menu (or text as in our example). Differences from the "base" CSS are bolded. And why would I place my navigation in/above my footer you said? Embrace your bottom was the inspiration for that.
- #content {
- float: left; /* Just float it left */
- width: 760px; /* The same width as the mainContainer */
- background: #D9D9D9;
- }
- #column {
- float: left; /* Just float it left */
- width: 760px; /* The same width as the mainContainer */
- background: #8A8AE6;
- }
Two column layout with right aligned column
My header
My content
Our two-column CSS layout with the column aligned right.
My column
Two column layout with left aligned column
My header
My content
Our two-column CSS layout with the column aligned left.
My column
Two column transferred to a one column layout with header menu
My header
My content
Our two-column CSS layout is transferred into a one column design. Our column is now our header menu.
My column
Two column transferred to a one column layout with footer menu
My header
My content
Our two-column CSS layout is transferred into a one column design. Our column is now our footer menu. This layout DO NOT support 100% height.
My column
Some final words about this flexible two column CSS layout
Because the footer is outside the mainContainer, this solution is prepared for a 100% height with footer layout also.
The source code to this article is different from the explanations because all four layouts are presented within this article.
Publisert: 09-12-2006 |