Tableless layouts have generated some heated debates around whether or not they are worth the effort. Many argue why suffer when tables do a good enough job? Well, I talked about standards and semantics in a previous article so I’ll leave that for you to read later. Putting semantic correctness aside, SEO plays a big part in why I made the switch a few years ago. I want to squeeze every last drop of advantage out of my markup as I can. It is not easy getting a top ranking so why make it harder but refusing to optimize your code?
Alright, enough of that, in reality tableless design really isn’t that hard once you have some practice and identify possible cross-browser issues that may arise like clearing floated divs and width differences.
So let’s lay down some structure. You can find the complete source for this article at the end. I am going to only focus on the pieces that apply to tableless design and assume you know the basics of layout a page.
Figure 1
<div id="content">
<div id="left">
<!-- use a container to avoid using padding and border on #left
which causes cross-browser inconsistencies with width -->
<div class="left-container">LEFT</div>
</div><!-- end #left -->
<div id="right">
<!-- use a container to avoid using padding and border on #right
which causes cross-browser inconsistencies with width -->
<div class="right-container">RIGHT</div>
</div><!-- end #right -->
<div class="clear"/>
</div><!-- end #content -->
This is the section we will have to float 2 divs side by side to create our 2 column layout. Before continuing I will explain some pieces of this code. I have used and extra container inside each column of this layout so that I can apply and padding or borders directly to the text and not to the column itself. The reason for this is Firefox, Opera, and other standards compliant browsers add margin and padding to the overall width of the div which will break our float because the 2 columsn width’s will equal greater than the width of the layout wrapper. Internet Explorer doesn’t add margins, etc to the overall width but subtracts them from it.
You’ll notice the div with class=”clear”, this will call the CSS class that will clear our floats. Again Internet Explorer 6 doesn’t require this because of it’s box model bug but other standards browers do need it. Using a break (br) tag is a common practice instead of a div tag but I discovered that Internet Explorer 7 seems to ignore a clearing break but works fine with a clearing div. I am not sure if it is only in select situations but I feel it is best to just avoid trouble altogether.
Now let’s look at our CSS
Figure 2
#wrapper
{
width: 790px;
margin: 0 auto;
}
#left
{
width: 590px;
background: #dedede;
float: left;
height: 500px;
}
.left-container
{
margin: 20px;
margin-bottom: 0; /* IE6 pushes footer down without this */
}
#right
{
width: 200px;
background: #b8b8b8;
float: left;
height: 500px;
}
.right-container
{
margin: 20px;
margin-bottom: 0; /* IE6 pushes footer down without this */
}
.clear
{
clear: left;
}
Notice the class clear, this clears the floats in our div, pretty simple. see how I add the margins to the left and right containers so we avoid any width problems. the width for #left and #right add up to the width of #wrapper (this contains the while layout) and #left and #right are floated left. I have set the background of each section to a different color so they will be easy to spot.
That’s pretty much it. That wasn’t too painful was it? Copy the source code below and make an HTML page and take a look at the finsihed product. A valid, hack free, 2 column, tableless layout. I didn’t even need a conditional comment.
Article Source
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tableless layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<style type="text/css">
body
{
margin: 0;
font-family: Verdana, Arial, sans-serif;
}
#wrapper
{
width: 790px;
margin: 0 auto;
}
#header
{
padding: 25px;
background: #eeeeee;
}
#content
{
}
#left
{
width: 590px;
background: #dedede;
float: left;
height: 500px;
}
.left-container
{
margin: 20px;
margin-bottom: 0; /* IE6 pushes footer down without this */
}
#right
{
width: 200px;
background: #b8b8b8;
float: left;
height: 500px;
}
.right-container
{
margin: 20px;
margin-bottom: 0; /* IE6 pushes footer down without this */
}
#footer
{
padding: 10px;
background: #eeeeee;
}
.clear
{
clear: left;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
HEADER
</div><!-- end #header -->
<div id="content">
<div id="left">
<!-- use a container to avoid using padding and border on #left
which causes cross-browser inconsistencies with width -->
<div class="left-container">LEFT</div>
</div><!-- end #left -->
<div id="right">
<!-- use a container to avoid using padding and border on #right
which causes cross-browser inconsistencies with width -->
<div class="right-container">RIGHT</div>
</div><!-- end #right -->
<div class="clear"/>
</div><!-- end #content -->
<div id="footer">
FOOTER
</div><!-- end #footer -->
</div><!-- end #wrapper -->
</body>
</html>
Sitting the
VCP-310 exams can be very helpful in running an online business. Advices on stocks can be given online. But for this a proper site has to be setup. To develop a site one can get help from
70-290, a Microsoft training module. Next a good hosting service is needed;
bluehost is probably the most recommended one out there today. Their
colocation hosting is a catch. The
advertising of the site is also important. Many strategies can be carried out but
affiliate marketing will probably have the most impact. To increase the visibility of the domain seo can be carried out.
Recent Comments